Skip to content

Commit 714aa5a

Browse files
committed
+ Instances for Validation
1 parent a5a3a74 commit 714aa5a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/FSharpPlus/Control/Monoid.fs

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ type Plus =
3434
static member ``+`` (x: AggregateException, y: AggregateException, [<Optional>]_mthd: Plus ) = new AggregateException (seq {yield! x.InnerExceptions; yield! y.InnerExceptions})
3535
static member ``+`` (x: exn , y: exn , [<Optional>]_mthd: Plus ) =
3636
let f (e: exn) = match e with :? AggregateException as a -> a.InnerExceptions :> seq<_> | _ -> Seq.singleton e
37-
new AggregateException (seq {yield! f x; yield! f y}) :> exn
37+
let left = f x
38+
new AggregateException (seq { yield! left; yield! Seq.except left (f y) }) :> exn
3839
#else
3940
static member ``+`` (x: StringBuilder , y: StringBuilder , [<Optional>]_mthd: Plus ) = StringBuilder().Append(string x).Append(string y)
4041
static member ``+`` (_: Id0 , _: Id0 , [<Optional>]_mthd: Plus ) = Id0 ""
4142
static member ``+`` (x: exn , y: exn , [<Optional>]_mthd: Plus ) : exn =
4243
let f (e: exn) = match e with :? AggregateException as a -> a.Data0 :> seq<_> | _ -> Seq.singleton e
43-
AggregateException (seq {yield! f x; yield! f y})
44+
let left = f x
45+
AggregateException (seq { yield! left; yield! Seq.except left (f y) }) :> exn
4446
#endif
4547

4648
static member inline Invoke (x: 'Plus) (y: 'Plus) : 'Plus =

src/FSharpPlus/Data/Validation.fs

+26
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ module Validation =
4949
| Success _ , Failure e2 -> Failure e2
5050
| Success f , Success a -> Success (f a)
5151

52+
let inline zip x y : Validation<'Error, 'T *'U> =
53+
match (x: Validation<'Error, 'T>), (y: Validation<'Error, 'U>) with
54+
#if !FABLE_COMPILER
55+
| Failure e1, Failure e2 -> Failure (plus e1 e2)
56+
#else
57+
| Failure e1, Failure e2 -> Failure (e1 + e2)
58+
#endif
59+
| Failure e1, Success _ -> Failure e1
60+
| Success _ , Failure e2 -> Failure e2
61+
| Success x , Success y -> Success (x, y)
62+
5263
let inline map2 f x y : Validation<'Error,'V> =
5364
match (x: Validation<'Error,'T>), (y: Validation<'Error,'U>) with
5465
#if !FABLE_COMPILER
@@ -281,6 +292,21 @@ type Validation<'error, 't> with
281292
[<EditorBrowsable(EditorBrowsableState.Never)>]
282293
static member inline Lift3 (f, x: Validation<'Error, 'T>, y: Validation<_, 'U>, z: Validation<_, 'V>) : Validation<_, 'W> = Validation.map3 f x y z
283294

295+
// as ZipApplicative (same behavior)
296+
[<EditorBrowsable(EditorBrowsableState.Never)>]
297+
static member inline Zip (x: Validation<'Error, 'T>, y: Validation<'Error, 'U>) : Validation<'Error, 'T * 'U> = Validation.zip x y
298+
299+
[<EditorBrowsable(EditorBrowsableState.Never)>]
300+
static member Pure x = Success x
301+
302+
static member inline (<.>) (f: Validation<'Error, 'T -> 'U>, x: Validation<_, 'T>) : Validation<_, _> = Validation.apply f x
303+
304+
[<EditorBrowsable(EditorBrowsableState.Never)>]
305+
static member inline Map2 (f, x: Validation<'Error, 'T>, y: Validation<'Error, 'U>) : Validation<'Error, 'V> = Validation.map2 f x y
306+
307+
[<EditorBrowsable(EditorBrowsableState.Never)>]
308+
static member inline Map3 (f, x: Validation<'Error, 'T>, y: Validation<_, 'U>, z: Validation<_, 'V>) : Validation<_, 'W> = Validation.map3 f x y z
309+
284310
// as Alternative (inherits from Applicative)
285311
#if (!FABLE_COMPILER || FABLE_COMPILER_3) && !FABLE_COMPILER_4
286312
static member inline get_Empty () = Failure (getEmpty ())

0 commit comments

Comments
 (0)