-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathabstraction-comonad.fsx
122 lines (81 loc) · 2.18 KB
/
abstraction-comonad.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
(*** hide ***)
// This block of code is omitted in the generated HTML documentation. Use
// it to define helpers that you do not want to show in the documentation.
#r @"../../src/FSharpPlus/bin/Release/net8.0/FSharpPlus.dll"
(**
Comonad
=======
Comonads are the categorical dual of monads.
___
Minimal complete definition
---------------------------
* ``extract s``
* ``extend g s`` / ``(=>>) s g``
*)
(**
static member Extract (s: 'Comonad<'T>) : 'T
static member (=>>) (s: 'Comonad<'T>, f: 'Comonad<'T> -> 'U) : 'Comonad<'U>
*)
(**
Other operations
----------------
* ``duplicate x``
*)
(**
static member Duplicate (x : 'Comonad<'T>) : 'Comonad<'Comonad<'T>>
*)
(**
Rules
-----
*)
(**
extend extract = id
extract << extend f = f
extend f << extend g = extend (f << extend g)
*)
(**
Related Abstractions
--------------------
- [Monad](abstraction-monad.html): Comonads are the categorical dual of monads.
Concrete implementations
------------------------
From .Net/F#
- ``Async<'T>``
- ``Lazy<'T>``
- ``Id<'T>``
- ``('W * 'T)``
- ``struct ('W * 'T)``
- ``'Monoid -> 'T``
- ``ValueTask<'T>``
From F#+
- [``Reader<'R,'T>``](type-reader.html)
- [``Writer<'Monoid,'T>``](type-writer.html)
[Suggest another](https://github.com/fsprojects/FSharpPlus/issues/new) concrete implementation
Examples
--------
*)
(**
```f#
#r @"nuget: FSharpPlus"
```
*)
open FSharpPlus
open FSharpPlus.Data
// A non-empty list
let lst = {Head = 1; Tail = [2;3;4;5]}
// Get the head
let elem1 = extract lst
// Get ALL tails
let tails = duplicate lst
// This should return the original list
let lst' = extend extract lst
let ct1 = duplicate [1;2;3;4] // val it : List<List<int>> = [[1; 2; 3; 4]; [2; 3; 4]; [3; 4]; [4]]
let ct2 = duplicate ("a", 10) // val it : string * (string * int) = ("a", ("a", 10))
let ct3 = duplicate (fun (x:string) -> System.Int32.Parse x)
let r80100 = ct3 "80" "100"
let ct1' = extend id [1;2;3;4]
let ct2' = extend id ("a", 10)
let ct3' = extend id (fun (x:string) -> System.Int32.Parse x)
let ct1'' = (=>>) [1;2;3;4] id
let ct2'' = (=>>) ("a", 10) id
let ct3'' = (=>>) (fun (x:string) -> System.Int32.Parse x) id