From b6e01bf6ff2aa410baa5182f56256533239e48a1 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:16:42 +0100 Subject: [PATCH 1/3] + Option.zip3 --- src/FSharpPlus/Extensions/Option.fs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/FSharpPlus/Extensions/Option.fs b/src/FSharpPlus/Extensions/Option.fs index 25f3d55cc..cdf108560 100644 --- a/src/FSharpPlus/Extensions/Option.fs +++ b/src/FSharpPlus/Extensions/Option.fs @@ -29,6 +29,16 @@ module Option = | Some x, Some y -> Some (x, y) | _ -> None + /// If all 3 value are Some, returns them tupled. Otherwise it returns None. + /// The first value. + /// The second value. + /// The third value. + /// The resulting option. + let zip3 x y z : option<'T * 'U * 'V> = + match x, y, z with + | Some x, Some y, Some z -> Some (x, y, z) + | _ -> None + /// Converts an option to a Result. /// The option value. /// The resulting Result value. From cc19ae4b32d84936f947e9eb5fb4581ff8255e53 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:17:16 +0100 Subject: [PATCH 2/3] + unzip, zip and zip3 to Result --- src/FSharpPlus/Extensions/Result.fs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/FSharpPlus/Extensions/Result.fs b/src/FSharpPlus/Extensions/Result.fs index b11e6c2b3..fb40e88e5 100644 --- a/src/FSharpPlus/Extensions/Result.fs +++ b/src/FSharpPlus/Extensions/Result.fs @@ -17,7 +17,26 @@ module Result = /// An Ok of the function applied to the value, or the first Error if either the function or the value is Error. let apply f (x: Result<'T,'Error>) : Result<'U,'Error> = match f, x with Ok a, Ok b -> Ok (a b) | Error e, _ | _, Error e -> Error e + /// If value is Ok, returns both of them tupled. Otherwise it returns the Error value twice in a tuple. + /// The value. + /// The resulting tuple. + let unzip (source: Result<'T * 'U, 'Error>) : Result<'T, 'Error> * Result<'U, 'Error> = match source with Ok (x, y) -> Ok x, Ok y | Error e -> Error e, Error e + /// Creates a Result value from a pair of Result values. + /// The first Result value. + /// The second Result value. + /// + /// The tupled value, or the first Error. + let zip (x: Result<'T, 'Error>) (y: Result<'U, 'Error>) : Result<'T * 'U, 'Error> = match x, y with Ok a, Ok b -> Ok (a, b) | Error e, _ | _, Error e -> Error e + + /// Creates a Result value from a three Result values. + /// The first Result value. + /// The second Result value. + /// The third Result value. + /// + /// The tupled value, or the first Error. + let zip3 (x: Result<'T, 'Error>) (y: Result<'U, 'Error>) (z: Result<'V, 'Error>) : Result<'T * 'U * 'V, 'Error> = match x, y, z with Ok a, Ok b, Ok c -> Ok (a, b, c) | Error e, _, _ | _, Error e, _ | _, _, Error e -> Error e + /// Creates a Result value from a pair of Result values, using a function to combine them. /// The mapping function. /// The first Result value. From 2db5b928d8e9789972e870e31c81b8bca36e5691 Mon Sep 17 00:00:00 2001 From: gusty <1261319+gusty@users.noreply.github.com> Date: Tue, 8 Nov 2022 20:18:44 +0100 Subject: [PATCH 3/3] + Result as ZipFunctor --- src/FSharpPlus/Control/Functor.fs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/FSharpPlus/Control/Functor.fs b/src/FSharpPlus/Control/Functor.fs index 809817102..b412e3898 100644 --- a/src/FSharpPlus/Control/Functor.fs +++ b/src/FSharpPlus/Control/Functor.fs @@ -158,7 +158,7 @@ type Unzip = #endif static member Unzip ((source: Async<'T * 'U> , _output: Async<'T> * Async<'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source - static member Unzip ((source: Result<'T * 'U, 'E> , _output: Result<'T,'E> * Result<'U,'E> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source + static member Unzip ((source: Result<'T * 'U, 'E> , _output: Result<'T,'E> * Result<'U,'E> ) , _mthd: Unzip ) = Result.unzip source static member Unzip ((source: Choice<'T * 'U, 'E> , _output: Choice<'T,'E> * Choice<'U,'E> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source static member Unzip ((source: KeyValuePair<'Key, 'T * 'U> , _output: KeyValuePair<_, 'T> * KeyValuePair<_, 'U> ) , _mthd: Unzip ) = Map.Invoke fst source, Map.Invoke snd source static member Unzip ((source: Map<'Key, 'T * 'U> , _output: Map<_, 'T> * Map<_, 'U> ) , _mthd: Unzip ) = Map.unzip source @@ -203,6 +203,7 @@ type Zip = static member Zip ((x: 'T [] , y: 'U [] , _output: ('T*'U) [] ), _mthd: Zip) = Array.zipShortest x y static member Zip ((x: ResizeArray<'T> , y: ResizeArray<'U> , _output: ResizeArray<'T*'U> ), _mthd: Zip) = ResizeArray.zipShortest x y static member Zip ((x: option<'T> , y: option<'U> , _output: option<'T*'U> ), _mthd: Zip) = Option.zip x y + static member Zip ((x: Result<'T, 'Error> , y: Result<'U, 'Error> , _output: Result<'T * 'U, 'Error> ), _mthd: Zip) = Result.zip x y static member Zip ((x: Async<'T> , y: Async<'U> , _output: Async<'T*'U> ), _mthd: Zip) = Async.zip x y #if !FABLE_COMPILER static member Zip ((x: Task<'T> , y: Task<'U> , _output: Task<'T*'U> ), _mthd: Zip) = Task.zip x y