Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gusty committed Feb 22, 2023
1 parent 9844fcc commit d80a4ad
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 107 deletions.
33 changes: 22 additions & 11 deletions docsrc/content/abstraction-alternative.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ Applicative Functors which also have a monoid structure.
___
Minimal complete definition
---------------------------
* ``return x``/``result x``
* ``return x``   /   ``result x``
* ``(<*>) f x``
* ``empty``
* ``append x y``/``(<|>) x y``
* ``append x y`` &nbsp; / &nbsp; ``(<|>) x y``
*)
(**
static member Return (x:'T) : 'Alternative<'T>
static member (<*>) (f:'T->'U, x:Alternative<'T>) : Alternative<'U>
static member get_Empty () :'Alternative
static member (<|>) (x:'Alternative<'T>, y:'Alternative<'T>) :'Alternative<'T>
static member Return (x: 'T) : 'Alternative<'T>
static member (<*>) (f: 'T -> 'U, x: 'Alternative<'T>) : 'Alternative<'U>
static member get_Empty () : 'Alternative
static member (<|>) (x: 'Alternative<'T>, y: 'Alternative<'T>) : 'Alternative<'T>
*)
(**
Note: ``return`` can't be used outside computation expressions, use ``result`` instead.
Expand All @@ -28,11 +28,14 @@ Other operations
* ``mfilter``
*)
(**
static member MFilter (x:seq<'Alternative>) :'Alternative
static member MFilter (x: seq<'Alternative>) : 'Alternative
*)
(**
* ``choice``
*)
(**
static member inline Choice (source: 'Foldable<'Alt<'T>>) : 'Alt<'T>
*)
(**
Rules
-----
Expand All @@ -56,13 +59,16 @@ Concrete implementations
From .Net/F#
- ``list<'T>``
- ``option<'T>``
- ``array<'T>``
- ``seq<'T>``
- ``option<'T>``
- ``voption<'T>``
- ``Result<'T, 'Monoid>``
- ``Choice<'T, 'Monoid>``
- ``'T -> 'Alternative``
From F#+
- [``ReaderT<'R, 'MonadPlus<'T>>``](type-readert.html)
- [``WriterT<'MonadPlus<'T * 'Monoid>>``](type-writert.html)
- [``StateT<'S,'MonadPlus<'T * 'S>>``](type-statet.html)
Expand All @@ -73,7 +79,12 @@ From F#+
- [``Compose<'AlternativeF<'AlternativeG<'T>>>``](type-compose.html)
- [``DList<'T>``](type-dlist.html)
- [``ZipList<'S>``](type-ziplist.html)
- [``NonEmptySeq<'T>``](type-nonemptyseq.html) ``*``
- [``Validation<'Error, 'T>``](type-validation.html) ``*``
``*`` Only ``<|>`` operation
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
Examples
Expand Down
30 changes: 20 additions & 10 deletions docsrc/content/abstraction-applicative.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ A functor with application, providing operations to embed pure expressions (``re
___
Minimal complete definition
---------------------------
* ``return x``/``result x``
* ``return x`` &nbsp; / &nbsp; ``result x``
* ``(<*>) f x``
*)
(**
static member Return (x:'T) : 'Applicative<'T>
static member (<*>) (f: 'Applicative<'T->'U>, x: 'Applicative<'T>) : 'Applicative<'U>
static member Return (x: 'T) : 'Applicative<'T>
static member (<*>) (f: 'Applicative<'T -> 'U>, x: 'Applicative<'T>) : 'Applicative<'U>
*)
(**
Note: ``return`` can't be used outside computation expressions, use ``result`` instead.
Expand All @@ -27,7 +27,7 @@ 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>
*)
(**
Expand Down Expand Up @@ -131,13 +131,13 @@ open FSharpPlus
open FSharpPlus.Data

// Apply +4 to a list
let lst5n6 = map ((+) 4) [ 1;2 ]
let lst5n6 = map ((+) 4) [ 1; 2 ]

// Apply +4 to an array
let arr5n6 = map ((+) 4) [|1;2|]
let arr5n6 = map ((+) 4) [|1; 2|]

// I could have written this
let arr5n6' = (+) <!> [|4|] <*> [|1;2|]
let arr5n6' = (+) <!> [|4|] <*> [|1; 2|]

// Add two options
let opt120 = (+) <!> Some 20 <*> tryParse "100"
Expand Down Expand Up @@ -198,7 +198,7 @@ open FSharpPlus.Math.Applicative
let opt121' = Some 21 .+. tryParse "100"
let optTrue = 30 >. tryParse "29"
let optFalse = tryParse "30" .< 29
let m1m2m3 = -.[1;2;3]
let m1m2m3 = -.[1; 2; 3]


// Using applicative computation expression
Expand Down Expand Up @@ -245,7 +245,17 @@ let person6 = applicative2 {
// A Monad is automatically an Applicative

type MyList<'s> = MyList of 's seq with
static member Return (x:'a) = MyList (Seq.singleton x)
static member Return (x: 'a) = MyList (Seq.singleton x)
static member (>>=) (MyList x: MyList<'T>, f) = MyList (Seq.collect (f >> (fun (MyList x) -> x)) x)

let mappedMyList : MyList<_> = (MyList [(+) 1;(+) 2;(+) 3]) <*> (MyList [1;2;3])
let mappedMyList : MyList<_> = (MyList [(+) 1; (+) 2; (+) 3]) <*> (MyList [1; 2; 3])


(**
Recommended reading
-------------------
- Highly recommended Matt Thornton's blog [Grokking Applicatives](https://dev.to/choc13/grokking-applicatives-44o1).
It contains examples using F#+ and an explanation from scratch.
*)
8 changes: 4 additions & 4 deletions docsrc/content/abstraction-bifoldable.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ From .Net/F#
- ``'T * 'U``
- ``struct ('T * 'U)``
- ``Result<'T,'U>``
- ``Choice<'T,'U>``
- ``Result<'T, 'U>``
- ``Choice<'T, 'U>``
From F#+
- [``Const<'C,'T>``](type-const.html)
- [``Validation<'err,'a>``](type-validation.html)
- [``Const<'C, 'T>``](type-const.html)
- [``Validation<'Error, 'T>``](type-validation.html)
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
Expand Down
10 changes: 5 additions & 5 deletions docsrc/content/abstraction-bifunctor.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ From .Net/F#
- ``'T1 * 'T2``
- ``struct ('T1 * 'T2)``
- ``Result<'T2,'T1>``
- ``Choice<'T2,'T1>``
- ``KeyValuePair<'T1,'T2>``
- ``Result<'T2, 'T1>``
- ``Choice<'T2, 'T1>``
- ``KeyValuePair<'T1, 'T2>``
From F#+
- [``Const<'C,'T>``](type-const.html)
- [``Validation<'Error,'T>``](type-validation.html)
- [``Const<'C, 'T>``](type-const.html)
- [``Validation<'Error, 'T>``](type-validation.html)
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
Expand Down
8 changes: 4 additions & 4 deletions docsrc/content/abstraction-bitraversable.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ From .Net/F#
- ``'T * 'U``
- ``struct ('T * 'U)``
- ``Result<'T,'U>``
- ``Choice<'T,'U>``
- ``Result<'T, 'U>``
- ``Choice<'T, 'U>``
From F#+
- [``Const<'C,'T>``](type-const.html)
- [``Validation<'Error,'T>``](type-validation.html)
- [``Const<'C, 'T>``](type-const.html)
- [``Validation<'Error, 'T>``](type-validation.html)
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
Expand Down
4 changes: 2 additions & 2 deletions docsrc/content/abstraction-category.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ Concrete implementations
From .Net/F#
- ``'T->'U``
- ``Func<'T,'U>``
- ``'T -> 'U``
- ``Func<'T, 'U>``
From F#+
Expand Down
73 changes: 41 additions & 32 deletions docsrc/content/abstraction-functor.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ The Functor abstraction is used for types that can be mapped over.
___
Minimal complete definition
---------------------------
* ``map f x``/``(|>>) x f``/``(<<|) f x``/``(<!>) f x``
* ``map f x`` &nbsp; / &nbsp; ``(|>>) x f`` &nbsp; / &nbsp; ``(<<|) f x`` &nbsp; / &nbsp; ``(<!>) f x``
*)
(**
static member Map (x:'Functor<'T>, f:'T->'U) :'Functor<'U>
static member Map (x: 'Functor<'T>, f: 'T -> 'U) : 'Functor<'U>
*)
(**
Other operations
----------------
* ``unzip x``
*)
(**
static member Unzip (x:Functor<'T * 'U>) :'Functor<'T> * 'Functor<'U>
static member Unzip (x: 'Functor<'T * 'U>) : 'Functor<'T> * 'Functor<'U>
*)
(**
Rules
Expand Down Expand Up @@ -54,49 +54,49 @@ From F#
- ``Async<'T>``
- ``Result<'T,'U>``
- ``Choice<'T,'U>``
- ``KeyValuePair<'Key,'T>``
- ``KeyValuePair<'Key, 'T>``
- ``Map<'Key,'T>``
- ``'Monoid * 'T``
- ``'struct ('Monoid * 'T)``
- ``Task<'T>``
- ``ValueTask<'T>``
- ``'R->'T``
- ``Expr<'T>``
- ``Dictionary<'Key,'T>``
- ``IDictionary<'Key,'T>``
- ``IReadOnlyDictionary<'Key,'T>``
- ``Dictionary<'Key, 'T>``
- ``IDictionary<'Key,' T>``
- ``IReadOnlyDictionary<'Key, 'T>``
- ``ResizeArray<'T>``
From F#+
- [``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<option<'T>>``](type-optiont.html)
- [``ValueOptionT<'Monad<voption<'T>>``](type-valueoptiont.html)
- [``SeqT<'Monad<seq<'T>>``](type-seqt.html)
- [``ListT<'Monad<list<'T>>``](type-listt.html)
- [``ResultT<'Monad<Result<'T,'TError>>``](type-resultt.html)
- [``ChoiceT<'Monad<Choice<'T,'TError>>``](type-choicet.html)
- [``Free<'Functor<'T>,'T>``](type-free.html)
- [``ResultT<'Monad<Result<'T, 'TError>>``](type-resultt.html)
- [``ChoiceT<'Monad<Choice<'T, 'TError>>``](type-choicet.html)
- [``Free<'Functor<'T>, 'T>``](type-free.html)
- [``NonEmptyList<'T>``](type-nonempty.html)
- [``NonEmptySet<'T>``](type-nonempty-set.html)
- [``NonEmptyMap<'Key, 'T>``](type-nonempty-map.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<'AlternativeF<'AlternativeG<'T>>>``](type-compose.html)
- [``DList<'T>``](type-dlist.html)
- [``Kleisli<'T, 'Monad<'U>>``](type-kleisli.html)
- [``Coproduct<'FunctorL<'T>,'FunctorR<'T>>``](type-coproduct.html)
- [``Vector<'T,'Dimension>``](type-vector.html)
- [``Matrix<'T,'Rows,'Columns>``](type-matrix.html)
- [``Coproduct<'FunctorL<'T>, 'FunctorR<'T>>``](type-coproduct.html)
- [``Vector<'T, 'Dimension>``](type-vector.html)
- [``Matrix<'T, 'Rows, 'Columns>``](type-matrix.html)
Restricted:
- ``string``
Expand All @@ -118,7 +118,7 @@ Examples
open FSharpPlus
open FSharpPlus.Math.Generic

let getLine = async { return System.Console.ReadLine() }
let getLine = async { return System.Console.ReadLine () }
let putStrLn x = async { printfn "%s" x}
let print x = async { printfn "%A" x}

Expand All @@ -141,8 +141,8 @@ let noValue = map minus3 None
let lstTimes2 = map times2 [1;2;3;4]
let fTimes2minus3 = map minus3 times2
let res39 = fTimes2minus3 21G
let getChars = map (fun (x:string) -> x.ToCharArray() |> Seq.toList ) action
let quot7 = map ((+)2) <@ 5 @>
let getChars = map (fun (x: string) -> x.ToCharArray () |> Seq.toList) action
let quot7 = map ((+) 2) <@ 5 @>


// try -> runIO getChars ;;
Expand All @@ -153,11 +153,11 @@ type Tree<'a> =
| Leaf of 'a
static member map f (t:Tree<'a> ) =
match t with
| Leaf x -> Leaf (f x)
| Tree(x,t1,t2) -> Tree(f x, Tree.map f t1, Tree.map f t2)
| Leaf x -> Leaf (f x)
| Tree (x, t1, t2) -> Tree (f x, Tree.map f t1, Tree.map f t2)

// add instance for Functor class
static member Map (x:Tree<_>, f) = Tree.map f x
static member Map (x: Tree<_>, f) = Tree.map f x

let myTree = Tree(6, Tree(2, Leaf 1, Leaf 3), Leaf 9)
let mappedTree = map fTimes2minus3 myTree
Expand All @@ -167,10 +167,10 @@ let mappedTree = map fTimes2minus3 myTree
// An Applicative is automatically a Functor

type ZipList<'s> = ZipList of 's seq with
static member Return (x:'a) = ZipList (Seq.initInfinite (konst x))
static member (<*>) (ZipList (f:seq<'a->'b>), ZipList x) = ZipList (Seq.zip f x |> Seq.map (fun (f, x) -> f x)) : ZipList<'b>
static member Return (x: 'a) = ZipList (Seq.initInfinite (konst x))
static member (<*>) (ZipList (f :seq<'a->'b>), ZipList x) = ZipList (Seq.zip f x |> Seq.map (fun (f, x) -> f x)) : ZipList<'b>

let mappedZipList = map string (ZipList [1;2;3])
let mappedZipList = map string (ZipList [1; 2; 3])


// A Monad is automatically a Functor
Expand All @@ -179,4 +179,13 @@ type MyList<'s> = MyList of 's seq with
static member Return (x:'a) = MyList x
static member (>>=) (MyList x: MyList<'T>, f) = MyList (Seq.collect (f >> (fun (MyList x) -> x)) x)

let mappedMyList = map string (MyList [1;2;3])
let mappedMyList = map string (MyList [1; 2; 3])


(**
Recommended reading
-------------------
- Highly recommended Matt Thornton's blog [Grokking Functors](https://dev.to/choc13/grokking-functors-bla).
It contains examples using F#+ and an explanation from scratch.
*)
Loading

0 comments on commit d80a4ad

Please sign in to comment.