diff --git a/docsrc/content/abstraction-applicative.fsx b/docsrc/content/abstraction-applicative.fsx index 94eb91cac..acdc554b7 100644 --- a/docsrc/content/abstraction-applicative.fsx +++ b/docsrc/content/abstraction-applicative.fsx @@ -26,9 +26,11 @@ Other operations * ``lift2`` *) + (** - static member Lift2 (f: 'T1 -> 'T2 -> 'T, x1: 'Applicative<'T1>, x2: 'Applicative<'T2>) : 'Applicative<'T> + static member Lift2 (f: 'T1 -> 'T2 -> 'T, x1: 'Applicative<'T1>, x2: 'Applicative<'T2>) : 'Applicative<'T> *) + (** @@ -62,44 +64,44 @@ From F# - ``IObservable<'T>`` - ``Lazy<'T>`` - ``Async<'T>`` - - ``Result<'T,'U>`` - - ``Choice<'T,'U>`` - - ``KeyValuePair<'Key,'T>`` + - ``Result<'T, 'U>`` + - ``Choice<'T, 'U>`` + - ``KeyValuePair<'Key, 'T>`` - ``'Monoid * 'T`` - ``ValueTuple<'Monoid, 'T>`` - ``Task<'T>`` - ``ValueTask<'T>`` - - ``'R->'T`` + - ``'R -> 'T`` - ``Expr<'T>`` - ``ResizeArray<'T>`` From F#+ - [``Identity<'T>``](type-identity.html) - - [``Cont<'R,'T>``](type-cont.html) - - [``ContT<'R,'T>``](type-contt.html) - - [``Reader<'R,'T>``](type-reader.html) - - [``ReaderT<'R,'Monad<'T>>``](type-readert.html) - - [``Writer<'Monoid,'T>``](type-writer.html) + - [``Cont<'R, 'T>``](type-cont.html) + - [``ContT<'R, 'T>``](type-contt.html) + - [``Reader<'R, 'T>``](type-reader.html) + - [``ReaderT<'R, 'Monad<'T>>``](type-readert.html) + - [``Writer<'Monoid, 'T>``](type-writer.html) - [``WriterT<'Monad<'T * 'Monoid>>``](type-writert.html) - - [``State<'S,'T * 'S>``](type-state.html) - - [``StateT<'S,'Monad<'T * 'S>>``](type-statet.html) + - [``State<'S, 'T * 'S>``](type-state.html) + - [``StateT<'S, 'Monad<'T * 'S>>``](type-statet.html) - [``OptionT<'Monad>``](type-optiont.html) - [``ValueOptionT<'Monad>``](type-valueoptiont.html) - [``SeqT<'Monad>``](type-seqt.html) - [``ListT<'Monad>``](type-listt.html) - - [``ResultT<'Monad>``](type-resultt.html) - - [``ChoiceT<'Monad>``](type-choicet.html) - - [``Free<'Functor<'T>,'T>``](type-free.html) + - [``ResultT<'Monad>``](type-resultt.html) + - [``ChoiceT<'Monad>``](type-choicet.html) + - [``Free<'Functor<'T>, 'T>``](type-free.html) - [``NonEmptyList<'T>``](type-nonempty.html) - - [``Validation<'Error,'T>``](type-validation.html) + - [``Validation<'Error, 'T>``](type-validation.html) - [``ZipList<'T>``](type-ziplist.html) - [``ParallelArray<'T>``](type-parallelarray.html) - - [``Const<'C,'T>``](type-const.html) + - [``Const<'C, 'T>``](type-const.html) - [``Compose<'Applicative1<'Applicative2<'T>>>``](type-compose.html) - [``DList<'T>``](type-dlist.html) - - [``Vector<'T,'Dimension>``](type-vector.html) - - [``Matrix<'T,'Rows,'Columns>``](type-matrix.html) + - [``Vector<'T, 'Dimension>``](type-vector.html) + - [``Matrix<'T, 'Rows, 'Columns>``](type-matrix.html) Restricted: - ``string`` diff --git a/docsrc/content/abstraction-misc.fsx b/docsrc/content/abstraction-misc.fsx index bd103b0b3..21c1d1ad4 100644 --- a/docsrc/content/abstraction-misc.fsx +++ b/docsrc/content/abstraction-misc.fsx @@ -32,11 +32,6 @@ let res113 = foldi (fun s i t -> t * s - i) 2 [|3;4;5|] let resSomeId20 = traversei (fun k t -> Some (10 + t)) (Tuple 10) -// ZipFunctor - -let (x, y) = zip (async { return 1 }) (async { return '2' }) |> Async.RunSynchronously - - // Collection let a = skip 3 [1..10] diff --git a/docsrc/content/abstraction-traversable.fsx b/docsrc/content/abstraction-traversable.fsx index 85068e440..b6441d6a4 100644 --- a/docsrc/content/abstraction-traversable.fsx +++ b/docsrc/content/abstraction-traversable.fsx @@ -30,7 +30,7 @@ Minimal complete definition Other operations ---------------- - * ``gather f x`` | ``transpose x`` (same as traverse and sequence but operating on ZipApplicatives) + * ``gather f x`` | ``transpose x`` (same as ``traverse`` and ``sequence`` but operating on [ZipApplicatives](abstraction-zipapplicative.html) ) *) (** static member Gather (t: 'Traversable<'T>, f: 'T -> 'ZipApplicative<'U>) : 'ZipApplicative<'Traversable<'U>> @@ -55,7 +55,7 @@ Related Abstractions -------------------- - [Functor](abstraction-functor.html): A traversable is generic on the Traversable type parameter and the (Applicative) Functor inner type parameter. - - [Applicative](abstraction-applicative.html): An applicative is a functor whose ``map`` operation can be splitted in ``return`` and ``(<*>)`` operations. + - [Applicative](abstraction-applicative.html): An applicative is a functor whose ``map`` operation can be splitted in ``return`` and ``(<*>)`` operations. - [Foldable](abstraction-foldable.html) : All traversables are foldables. diff --git a/docsrc/content/abstraction-zipapplicative.fsx b/docsrc/content/abstraction-zipapplicative.fsx index 4911991bd..2b58b2589 100644 --- a/docsrc/content/abstraction-zipapplicative.fsx +++ b/docsrc/content/abstraction-zipapplicative.fsx @@ -4,8 +4,8 @@ #r @"../../src/FSharpPlus/bin/Release/netstandard2.0/FSharpPlus.dll" (** -ZipApplicative -============== +Zip Applicative (aka Non-sequential Applicative) +================================================ A functor with application, providing operations to embed pure expressions (``pur``), run computations pointwise and/or paralell and combine their results (``<.>``). ___ Minimal complete definition @@ -29,6 +29,12 @@ Other operations static member Zip (x1: 'ZipApplicative<'T1>, x2: 'ZipApplicative<'T2>) : 'ZipApplicative<'T1 * 'T2> *) (** +* ``unzip`` +*) +(** + static member Unzip (x: 'ZipApplicative<'T1 * 'T2>) : 'ZipApplicative<'T1> * 'ZipApplicative<'T2> +*) +(** * ``map2`` *) (** @@ -47,19 +53,32 @@ Other operations Rules ----- + *) (** + +Since ZipApplicatives are Applicatives they obey the same applicative rules: + pur id <.> v = v pur (<<) <.> u <.> v <.> w = u <.> (v <.> w) pur f <*> pur x = pur (f x) u <*> pur y = pur ((|>) y) <.> u *) +(** + +But they have some additional rules: + + zip x y = tuple2 x <.> y + unzip = map fst &&& map snd + id = unzip >> (<||) zip = (<||) zip >> unzip +*) + (** Related Abstractions -------------------- - [Functor](abstraction-functor.html): A zipApplicative is a functor whose ``map`` operation can be splitted in ``pur`` and ``(<.>)`` operations, - - [ZipApplicative](abstraction-applicative.html) : ZipApplicatives are applicatives which usually don't form a [Monad](abstraction-monad.html). + - [Applicative](abstraction-applicative.html) : ZipApplicatives are applicatives which usually don't form a [Monad](abstraction-monad.html), therefore the Applicative instance normally is not the same. Concrete implementations ------------------------ @@ -88,7 +107,7 @@ From F#+ - [``NonEmptyList<'T>``](type-nonempty.html) - [``Compose<'ZipApplicative1<'ZipApplicative2<'T>>>``](type-compose.html) - (*) The operation is the same as that for the normal applicative + (*) The operations are the same as those for the normal applicative Only for <*> operation: @@ -115,8 +134,11 @@ Examples open FSharpPlus +let (x, y) = zip (async { return 1 }) (async { return '2' }) |> Async.RunSynchronously +// val y: char = '2' + -// pointwise operations +// crossproduct vs pointwise operations let arr1 = (+) [|1;2;3|] <*> [|10;20;30|] let arr2 = (+) [|1;2;3|] <.> [|10;20;30|] @@ -129,10 +151,10 @@ let arr2 = (+) [|1;2;3|] <.> [|10;20;30|] let validated = applicative2' { let! x = async { return Ok 1 } - and! y = async { return Ok 2 } - and! z = async { return Error ["Error"] } + and! y = async { return Error ["Error1"] } + and! z = async { return Error ["Error2"] } return x + y + z } validated |> Async.RunSynchronously -// val it: Result = Error ["Error"] +// val it: Result = Error ["Error1"; "Error2"] diff --git a/docsrc/content/computation-expressions.fsx b/docsrc/content/computation-expressions.fsx index 6f125148d..d34684df9 100644 --- a/docsrc/content/computation-expressions.fsx +++ b/docsrc/content/computation-expressions.fsx @@ -12,9 +12,11 @@ Computations Expressions This library allows to use some common computation expressions without writing any boiler plate code. -For applicatives there is single computation expression: ``applicative { .. }``. Additionally ``applicative2 { .. }`` and ``applicative3 { .. }`` exists for composed (aka layered) applicatives. +For [Applicatives](abstraction-applicative.html) there is single computation expression: ``applicative { .. }``. Additionally ``applicative2 { .. }`` and ``applicative3 { .. }`` exists for composed (aka layered) applicatives. -For monadic code there is a single computation expression: ``monad { .. }`` but it comes in 4 flavours: +For [ZipApplicatives](abstraction-zipapplicative.html) there is a counterpart set of computation expressions: ``applicative' { .. }``, ``applicative2' { .. }`` and ``applicative3' { .. }``. + +For [monadic](abstraction-monad.html) code there is a single computation expression: ``monad { .. }`` but it comes in 4 flavours: - Delayed or strict diff --git a/docsrc/tools/templates/abstractions.plantuml b/docsrc/tools/templates/abstractions.plantuml index 0f8b4a579..af7b01b0f 100644 --- a/docsrc/tools/templates/abstractions.plantuml +++ b/docsrc/tools/templates/abstractions.plantuml @@ -48,6 +48,14 @@ abstract class Alternative [[http://fsprojects.github.io/FSharpPlus/abstraction- + mfilter p x } +abstract class ZipApplicative [[http://fsprojects.github.io/FSharpPlus/abstraction-zipapplicative.html]] { + + pur x + + (<.>) f x + == + + map f x + + map2 f x y +} + abstract class Monad [[http://fsprojects.github.io/FSharpPlus/abstraction-monad.html]] { + return x + (>>=) x f @@ -117,6 +125,7 @@ Functor --|> Bifunctor Functor --|> Applicative Applicative --|> Monad Applicative --|> Alternative +ZipApplicative --|> Alternative Monoid ..|> Monad Monoid ..|> Foldable Monoid ..|> Bifoldable @@ -124,6 +133,7 @@ Functor --|> Comonad Functor --|> Traversable Foldable --|> Traversable Bifoldable --|> Bitraversable +Applicative ..|> ZipApplicative Applicative ..|> Bitraversable Applicative ..|> Traversable Monoid ..|> Alternative