Skip to content

Commit a447343

Browse files
authored
Merge branch 'master' into gusty-dict-applicative
2 parents 221911c + aec9a19 commit a447343

File tree

4 files changed

+63
-47
lines changed

4 files changed

+63
-47
lines changed

src/FSharpPlus/Control/Numeric.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ type Zero with
201201
#endif
202202
static member inline Zero (_: 'T->'Monoid , _: Zero) = (fun _ -> Zero.Invoke ()) : 'T->'Monoid
203203
static member inline Zero (_: Async<'a> , _: Zero) = let (v: 'a) = Zero.Invoke () in async.Return v
204+
static member inline Zero (_: Result<'T, 'Error> , _: Zero) = let (v: 'Error) = Zero.Invoke () in Error v
205+
static member inline Zero (_: Choice<'T1, 'T2> , _: Zero) = let (v: 'T2) = Zero.Invoke () in Choice2Of2 v
204206
#if !FABLE_COMPILER
205207
static member inline Zero (_: Expr<'a> , _: Zero) = let (v: 'a) = Zero.Invoke () in Expr.Cast<'a>(Expr.Value (v))
206208
#endif

src/FSharpPlus/Parsing.fs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,10 @@ module Parsing =
179179
/// Gets a tuple with the result of parsing each element of a formatted text from the Console. Returns None in case of failure.
180180
let inline tryScanfn pf : '``(T1 * T2 * ... * Tn)`` option = trySscanf pf (Console.ReadLine ())
181181

182+
/// <summary>
183+
/// An active recognizer for scanning a formatted string of generic values.
184+
/// </summary>
185+
/// <category index="23">Additional Functions</category>
186+
let inline (|Scanned|_|) (pf: PrintfFormat<_,_,_,_,'``(T1 * T2 * ... * Tn)``>) : string -> '``(T1 * T2 * ... * Tn)`` option = trySscanf pf
187+
182188
#endif

tests/FSharpPlus.Tests/Monoid.fs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ module Monoid =
7474
let! y = struct ("Ten", 10)
7575
return y - x }
7676
Assert.AreEqual (str, "FourTen")
77-
Assert.AreEqual (num, 6)
77+
Assert.AreEqual (num, 6)
78+
79+
[<Test>]
80+
let resultAndChoice () =
81+
let r1 = sum [Ok 1; Error "This is an error"; Ok 3]
82+
let r2 = sum [Choice1Of2 1; Choice2Of2 "This is an error"; Choice1Of2 3]
83+
Assert.IsInstanceOf<Option<Result<int, string>>> (Some r1)
84+
Assert.IsInstanceOf<Option<Choice<int, string>>> (Some r2)
85+
Assert.AreEqual (Result<int, string>.Ok 4, r1)
86+
Assert.AreEqual (Choice<int, string>.Choice1Of2 4, r2)

tests/FSharpPlus.Tests/Parsing.fs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -107,67 +107,66 @@ module Parsing =
107107
let _zzz1 = sscanf "%%(%s)" "%(hello)"
108108
let (_x1,_y1,_z1) = sscanf "%s--%s-%s" "test--this-string"
109109

110-
let inline (|Parsedf|_|) pf = trySscanf pf
111-
match "ab" with Parsedf "%c" _ -> failwith "wrong match" | Parsedf "%c%c" ('a', 'b') -> () | _ -> failwith "didn't match"
112-
match "abc" with Parsedf "%c%c" ('a', 'b') -> failwith "wrong match" | Parsedf "%c%c%c%s" ('a', 'b', 'c', "") -> () | _ -> failwith "didn't match"
110+
match "ab" with Scanned "%c" _ -> failwith "wrong match" | Scanned "%c%c" ('a', 'b') -> () | _ -> failwith "didn't match"
111+
match "abc" with Scanned "%c%c" ('a', 'b') -> failwith "wrong match" | Scanned "%c%c%c%s" ('a', 'b', 'c', "") -> () | _ -> failwith "didn't match"
113112
match "(%hello)" with
114-
| Parsedf "%d" _ | Parsedf "%f" _ | Parsedf "%x" _ -> failwith "wrong match"
115-
| Parsedf "%%(%%%s)" _ | Parsedf "(%%%sa" _ | Parsedf "(%%hel%c" _ | Parsedf "%%h%cllo)" _ -> failwith "wrong match"
116-
| Parsedf "(%%%s)" "hello" -> ()
113+
| Scanned "%d" _ | Scanned "%f" _ | Scanned "%x" _ -> failwith "wrong match"
114+
| Scanned "%%(%%%s)" _ | Scanned "(%%%sa" _ | Scanned "(%%hel%c" _ | Scanned "%%h%cllo)" _ -> failwith "wrong match"
115+
| Scanned "(%%%s)" "hello" -> ()
117116
| _ -> failwith "didn't match"
118-
match " 3" with Parsedf "% d" 3 -> () | _ -> failwith "didn't match"
119-
match " 3" with Parsedf "% d" 3 -> () | _ -> failwith "didn't match"
120-
match " 3" with Parsedf "% d" 3 -> () | _ -> failwith "didn't match" // em space
121-
match "3 " with Parsedf "%-d" 3 -> () | _ -> failwith "didn't match"
122-
match "3 " with Parsedf "%-d" 3 -> () | _ -> failwith "didn't match"
123-
match "3 " with Parsedf "%-d" 3 -> () | _ -> failwith "didn't match" // em space
124-
match " 3 " with Parsedf "% -d" 3 -> () | _ -> failwith "didn't match"
125-
match " 3 " with Parsedf "% -d" 3 -> () | _ -> failwith "didn't match"
126-
match " 3 " with Parsedf "% -d" 3 -> () | _ -> failwith "didn't match" // em space
127-
match "test--this-gg" with Parsedf "%s--%s-%s" ("test", "this", "gg") -> () | _ -> failwith "didn't match"
128-
match "1 2.1 3.4 .3 43.2e32 0 f f" with Parsedf "%f %F %g %G %e %E %c %c" (1f, 2.1, 3.4m, 0.3, 43.2e32, 0., 'f', 'f') -> () | _ -> failwith "didn't match"
129-
match "1 2.1 3.4 .3 43.2e32 0 f f f" with Parsedf "%f% F %g %G %e %E %c %c %c" (1m, 2.1, 3.4, 0.3m, 43.2e32, 0., 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
130-
match "1 2.1 3.4.3 43.2e32 0 f f ff" with Parsedf "%B %F %-g%G %e %E %c %c %c%c" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
131-
match "1 2.1 3.4.3 43.2e32 0 f f fff" with Parsedf "%o %F % g%-G %e %E %c %c %c%c%c" (1y, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
132-
match "1 2.1 3.4.3 43.2e32 0 f f fff16" with Parsedf "%x %F %- g%- G %e %E %c %c %c%c%c%i" (1us, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16) -> () | _ -> failwith "didn't match"
133-
match "1 2.1 3.4.3 43.2e32 0 f f fff16 17" with Parsedf "%X %F %g% G %e %E %c %c %c%c%c%i %f" (1s, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16L, 17.) -> () | _ -> failwith "didn't match"
134-
match "13 43 AA 77A" with Parsedf "%x %X %x %o%X" (0x13, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
135-
match "13 43 AA 77A" with Parsedf "%B%x %X %x %o%X" (0b1, 0x3, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
136-
match "111AAA" with Parsedf "%B%s" (0b111, "AAA") -> () | _ -> failwith "didn't match"
137-
match "100700 100 100" with Parsedf "%B%o %x %X" (0b100, 0o700, 0x100, 0x100) -> () | _ -> failwith "didn't match"
117+
match " 3" with Scanned "% d" 3 -> () | _ -> failwith "didn't match"
118+
match " 3" with Scanned "% d" 3 -> () | _ -> failwith "didn't match"
119+
match " 3" with Scanned "% d" 3 -> () | _ -> failwith "didn't match" // em space
120+
match "3 " with Scanned "%-d" 3 -> () | _ -> failwith "didn't match"
121+
match "3 " with Scanned "%-d" 3 -> () | _ -> failwith "didn't match"
122+
match "3 " with Scanned "%-d" 3 -> () | _ -> failwith "didn't match" // em space
123+
match " 3 " with Scanned "% -d" 3 -> () | _ -> failwith "didn't match"
124+
match " 3 " with Scanned "% -d" 3 -> () | _ -> failwith "didn't match"
125+
match " 3 " with Scanned "% -d" 3 -> () | _ -> failwith "didn't match" // em space
126+
match "test--this-gg" with Scanned "%s--%s-%s" ("test", "this", "gg") -> () | _ -> failwith "didn't match"
127+
match "1 2.1 3.4 .3 43.2e32 0 f f" with Scanned "%f %F %g %G %e %E %c %c" (1f, 2.1, 3.4m, 0.3, 43.2e32, 0., 'f', 'f') -> () | _ -> failwith "didn't match"
128+
match "1 2.1 3.4 .3 43.2e32 0 f f f" with Scanned "%f% F %g %G %e %E %c %c %c" (1m, 2.1, 3.4, 0.3m, 43.2e32, 0., 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
129+
match "1 2.1 3.4.3 43.2e32 0 f f ff" with Scanned "%B %F %-g%G %e %E %c %c %c%c" (1, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
130+
match "1 2.1 3.4.3 43.2e32 0 f f fff" with Scanned "%o %F % g%-G %e %E %c %c %c%c%c" (1y, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f') -> () | _ -> failwith "didn't match"
131+
match "1 2.1 3.4.3 43.2e32 0 f f fff16" with Scanned "%x %F %- g%- G %e %E %c %c %c%c%c%i" (1us, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16) -> () | _ -> failwith "didn't match"
132+
match "1 2.1 3.4.3 43.2e32 0 f f fff16 17" with Scanned "%X %F %g% G %e %E %c %c %c%c%c%i %f" (1s, 2.1, 3.4, 0.3, 43.2e32, 0., 'f', 'f', 'f', 'f', 'f', 16L, 17.) -> () | _ -> failwith "didn't match"
133+
match "13 43 AA 77A" with Scanned "%x %X %x %o%X" (0x13, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
134+
match "13 43 AA 77A" with Scanned "%B%x %X %x %o%X" (0b1, 0x3, 0x43, 0xAA, 0o77, 0xA) -> () | _ -> failwith "didn't match"
135+
match "111AAA" with Scanned "%B%s" (0b111, "AAA") -> () | _ -> failwith "didn't match"
136+
match "100700 100 100" with Scanned "%B%o %x %X" (0b100, 0o700, 0x100, 0x100) -> () | _ -> failwith "didn't match"
138137

139138
match "1+1-2+2-8+8" with
140-
| Parsedf "%s%o" _ -> failwith "wrong match"
141-
| Parsedf "%+u%+u%+d%+u%+u%+u" _ -> failwith "wrong match"
142-
| Parsedf "%+u%+u%+d%+u%+d%o" _ -> failwith "wrong match"
143-
| Parsedf "%+u%+u%+d%+u%-d%u" _ -> failwith "wrong match"
144-
| Parsedf "%+u%+u%+d%+u%u%+d" _ -> failwith "wrong match"
145-
| Parsedf "%+u%+u%+d%+u%+o%+u" _ -> failwith "wrong match"
146-
| Parsedf "%+u%+u%+d%+u%+B%+u" _ -> failwith "wrong match"
147-
| Parsedf "%+u%+u%+d%+u%+x%+X" _ -> failwith "wrong match"
148-
| Parsedf "%+u%+u%+d%+u%+d%+X" (a, b, c, d, e, f) ->
139+
| Scanned "%s%o" _ -> failwith "wrong match"
140+
| Scanned "%+u%+u%+d%+u%+u%+u" _ -> failwith "wrong match"
141+
| Scanned "%+u%+u%+d%+u%+d%o" _ -> failwith "wrong match"
142+
| Scanned "%+u%+u%+d%+u%-d%u" _ -> failwith "wrong match"
143+
| Scanned "%+u%+u%+d%+u%u%+d" _ -> failwith "wrong match"
144+
| Scanned "%+u%+u%+d%+u%+o%+u" _ -> failwith "wrong match"
145+
| Scanned "%+u%+u%+d%+u%+B%+u" _ -> failwith "wrong match"
146+
| Scanned "%+u%+u%+d%+u%+x%+X" _ -> failwith "wrong match"
147+
| Scanned "%+u%+u%+d%+u%+d%+X" (a, b, c, d, e, f) ->
149148
areEqual (a |> box |> unbox<int>) 1
150149
areEqual (b |> box |> unbox<int>) 1
151150
areEqual (c |> box |> unbox<int>) -2
152151
areEqual (d |> box |> unbox<int>) 2
153152
areEqual (e |> box |> unbox<int>) -8
154153
areEqual (f |> box |> unbox<int>) 8
155154
| _ -> failwith "didn't match"
156-
match "1+1-2+2-8+8" with Parsedf "%+-d%+d%+-d%+d%+-d%+d" (1,1,-2,2,-8,8) -> () | _ -> failwith "didn't match"
157-
match "1+1-2+2-8+8" with Parsedf "%d+%d%d%+d%d%+d" (1,1,-2,2,-8,8) -> () | _ -> failwith "didn't match"
158-
match "1+1-2+2-8+8" with Parsedf "%+B%+B-%+o%+o-%+X%+X" (1,1,2,2,8,8) -> () | _ -> failwith "didn't match"
159-
match "1+1-2+2-8+8e" with Parsedf "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,8M,0xE) -> () | _ -> failwith "didn't match"
160-
match "1+1-2+2-8+8e1a" with Parsedf "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,80M,0xA) -> () | _ -> failwith "didn't match"
161-
match "1+1-2+2-8+8e-1a" with Parsedf "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,0.8M,0xA) -> () | _ -> failwith "didn't match"
162-
match "1+1-2+2-8+8ea" with Parsedf "%+-f%+-F%+-e%+-E%+-g%+-G%+-X" (1f,1.,-2m,2f,-8.,8M,0xEA) -> () | _ -> failwith "didn't match"
155+
match "1+1-2+2-8+8" with Scanned "%+-d%+d%+-d%+d%+-d%+d" (1,1,-2,2,-8,8) -> () | _ -> failwith "didn't match"
156+
match "1+1-2+2-8+8" with Scanned "%d+%d%d%+d%d%+d" (1,1,-2,2,-8,8) -> () | _ -> failwith "didn't match"
157+
match "1+1-2+2-8+8" with Scanned "%+B%+B-%+o%+o-%+X%+X" (1,1,2,2,8,8) -> () | _ -> failwith "didn't match"
158+
match "1+1-2+2-8+8e" with Scanned "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,8M,0xE) -> () | _ -> failwith "didn't match"
159+
match "1+1-2+2-8+8e1a" with Scanned "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,80M,0xA) -> () | _ -> failwith "didn't match"
160+
match "1+1-2+2-8+8e-1a" with Scanned "%+f%+F%+e%+E%+g%+G%+X" (1f,1.,-2m,2f,-8.,0.8M,0xA) -> () | _ -> failwith "didn't match"
161+
match "1+1-2+2-8+8ea" with Scanned "%+-f%+-F%+-e%+-E%+-g%+-G%+-X" (1f,1.,-2m,2f,-8.,8M,0xEA) -> () | _ -> failwith "didn't match"
163162

164163
let _date: (DayOfWeek * string * uint16 * int) option = trySscanf "%A %A %A %A" "Saturday March 25 1989"
165164
let _date1: DateTime option = trySscanf "%A" "Saturday March 25 1989"
166165

167-
match "12:34" with Parsedf "%A" (x: TimeSpan) -> areEqual (TimeSpan(12, 34, 0)) x | _ -> failwith "Pattern match failed"
168-
match "12:34:56" with Parsedf "%O" (x: TimeSpan) -> areEqual (TimeSpan(12, 34, 56)) x | _ -> failwith "Pattern match failed"
169-
match "9876-5-4 3:2:1" with Parsedf "%A" (x: DateTime) -> areEqual (DateTime(9876,5,4,3,2,1)) x | _ -> failwith "Pattern match failed"
170-
match "9876-5-4 3:2:1 a" with Parsedf "%O %x" (x: DateTime, y) -> areEqual (DateTime(9876,5,4,3,2,1)) x; areEqual 0xA y | _ -> failwith "Pattern match failed"
166+
match "12:34" with Scanned "%A" (x: TimeSpan) -> areEqual (TimeSpan(12, 34, 0)) x | _ -> failwith "Pattern match failed"
167+
match "12:34:56" with Scanned "%O" (x: TimeSpan) -> areEqual (TimeSpan(12, 34, 56)) x | _ -> failwith "Pattern match failed"
168+
match "9876-5-4 3:2:1" with Scanned "%A" (x: DateTime) -> areEqual (DateTime(9876,5,4,3,2,1)) x | _ -> failwith "Pattern match failed"
169+
match "9876-5-4 3:2:1 a" with Scanned "%O %x" (x: DateTime, y) -> areEqual (DateTime(9876,5,4,3,2,1)) x; areEqual 0xA y | _ -> failwith "Pattern match failed"
171170

172171
let x = trySscanf "%X %x" "13 43"
173172
let o = trySscanf "%o" "10"

0 commit comments

Comments
 (0)