You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`MonadFilter` is a type class that abstracts away the option of interrupting computation if a given predicate is not satisfied.
10
+
11
+
All instances of `MonadFilter` provide syntax over their respective data types to comprehend monadically over their computation:
12
+
13
+
##continueWith
14
+
15
+
Binding over `MonadFilter` instances with `bindingFilter` brings into scope the `continueIf` guard that requires a `Boolean` predicate as value. If the predicate is `true` the computation will continue and if the predicate returns `false` the computation is short-circuited returning monad filter instance `empty()` value.
16
+
17
+
In the example below we demonstrate monadic comprehension over the `MonadFilter` instances for both `Option` and `ListKW` since both data types can provide a safe `empty` value.
18
+
19
+
When `continueIf` is satisfied the computation continues
20
+
21
+
```kotlin:ank
22
+
import kategory.*
23
+
24
+
Option.monadFilter().bindingFilter {
25
+
val a = Option(1).bind()
26
+
val b = Option(1).bind()
27
+
val c = a + b
28
+
continueIf(c > 0)
29
+
yields(c)
30
+
}
31
+
```
32
+
33
+
```kotlin:ank
34
+
ListKW.monadFilter().bindingFilter {
35
+
val a = listOf(1).k().bind()
36
+
val b = listOf(1).k().bind()
37
+
val c = a + b
38
+
continueIf(c > 0)
39
+
yields(c)
40
+
}
41
+
```
42
+
43
+
When `continueIf` returns `false` the computation is interrupted and the `empty()` value is returned
44
+
45
+
```kotlin:ank
46
+
Option.monadFilter().bindingFilter {
47
+
val a = Option(1).bind()
48
+
val b = Option(1).bind()
49
+
val c = a + b
50
+
continueIf(c < 0)
51
+
yields(c)
52
+
}
53
+
```
54
+
55
+
```kotlin:ank
56
+
ListKW.monadFilter().bindingFilter {
57
+
val a = listOf(1).k().bind()
58
+
val b = listOf(1).k().bind()
59
+
val c = a + b
60
+
continueIf(c < 0)
61
+
yields(c)
62
+
}
63
+
```
64
+
65
+
##bindWithFilter
66
+
67
+
Binding over `MonadFilter` instances with `bindingFilter` brings into scope the `bindWithFilter` guard that requires a `Boolean` predicate as value getting matched on the monad capturing inner value. If the predicate is `true` the computation will continue and if the predicate returns `false` the computation is short-circuited returning the monad filter instance `empty()` value.
68
+
69
+
When `bindWithFilter` is satisfied the computation continues
70
+
71
+
```kotlin:ank
72
+
Option.monadFilter().bindingFilter {
73
+
val a = Option(1).bind()
74
+
val b = Option(1).bindWithFilter { it == a } //continues
75
+
val c = a + b
76
+
yields(c)
77
+
}
78
+
```
79
+
80
+
```kotlin:ank
81
+
ListKW.monadFilter().bindingFilter {
82
+
val a = listOf(1).k().bind()
83
+
val b = listOf(1).k().bindWithFilter { it == a } //continues
84
+
val c = a + b
85
+
yields(c)
86
+
}
87
+
```
88
+
89
+
When `bindWithFilter` returns `false` the computation short circuits yielding the monad's empty value
90
+
91
+
```kotlin:ank
92
+
Option.monadFilter().bindingFilter {
93
+
val a = Option(0).bind()
94
+
val b = Option(1).bindWithFilter { it == a } //short circuits because a is 0
95
+
val c = a + b
96
+
yields(c)
97
+
}
98
+
```
99
+
100
+
```kotlin:ank
101
+
ListKW.monadFilter().bindingFilter {
102
+
val a = listOf(0).k().bind()
103
+
val b = listOf(1).k().bindWithFilter { it == a } //short circuits because a is 0
0 commit comments