@@ -57,24 +57,6 @@ interface Foldable<in F> : Typeclass {
57
57
fun <A , B > foldMap (mb : Monoid <B >, fa : HK <F , A >, f : (A ) -> B ): B =
58
58
foldL(fa, mb.empty(), { b, a -> mb.combine(b, f(a)) })
59
59
60
- /* *
61
- * Left associative monadic folding on F.
62
- *
63
- * The default implementation of this is based on foldL, and thus will always fold across the entire structure.
64
- * Certain structures are able to implement this in such a way that folds can be short-circuited (not traverse the
65
- * entirety of the structure), depending on the G result produced at a given step.
66
- */
67
- fun <G , A , B > foldM (MG : Monad <G >, fa : HK <F , A >, z : B , f : (B , A ) -> HK <G , B >): HK <G , B > =
68
- foldL(fa, MG .pure(z), { gb, a -> MG .flatMap(gb) { f(it, a) } })
69
-
70
- /* *
71
- * Monadic folding on F by mapping A values to G<B>, combining the B values using the given Monoid<B> instance.
72
- *
73
- * Similar to foldM, but using a Monoid<B>.
74
- */
75
- fun <G , A , B > foldMapM (MG : Monad <G >, bb : Monoid <B >, fa : HK <F , A >, f : (A ) -> HK <G , B >) : HK <G , B > =
76
- foldM(MG , fa, bb.empty(), { b, a -> MG .map(f(a)) { bb.combine(b, it) } })
77
-
78
60
/* *
79
61
* Traverse F<A> using Applicative<G>.
80
62
*
@@ -84,7 +66,7 @@ interface Foldable<in F> : Typeclass {
84
66
* not otherwise needed.
85
67
*/
86
68
fun <G , A , B > traverse_ (ag : Applicative <G >, fa : HK <F , A >, f : (A ) -> HK <G , B >): HK <G , Unit > =
87
- foldR(fa, always { ag.pure(Unit ) }, { a, acc -> ag.map2Eval(f(a), acc) { Unit } }).value()
69
+ foldR(fa, always { ag.pure(Unit ) }, { a, acc -> ag.map2Eval(f(a), acc) { Unit } }).value()
88
70
89
71
/* *
90
72
* Sequence F<G<A>> using Applicative<G>.
@@ -137,5 +119,23 @@ interface Foldable<in F> : Typeclass {
137
119
}
138
120
}
139
121
122
+ /* *
123
+ * Monadic folding on F by mapping A values to G<B>, combining the B values using the given Monoid<B> instance.
124
+ *
125
+ * Similar to foldM, but using a Monoid<B>.
126
+ */
127
+ inline fun <F , reified G , A , reified B > Foldable<F>.foldMapM (fa : HK <F , A >, noinline f : (A ) -> HK <G , B >, MG : Monad <G > = monad(), bb : Monoid <B > = monoid()): HK <G , B > =
128
+ foldM(fa, bb.empty(), { b, a -> MG .map(f(a)) { bb.combine(b, it) } }, MG )
129
+
130
+ /* *
131
+ * Left associative monadic folding on F.
132
+ *
133
+ * The default implementation of this is based on foldL, and thus will always fold across the entire structure.
134
+ * Certain structures are able to implement this in such a way that folds can be short-circuited (not traverse the
135
+ * entirety of the structure), depending on the G result produced at a given step.
136
+ */
137
+ inline fun <F , reified G , A , B > Foldable<F>.foldM (fa : HK <F , A >, z : B , crossinline f : (B , A ) -> HK <G , B >, MG : Monad <G > = monad()): HK <G , B > =
138
+ foldL(fa, MG .pure(z), { gb, a -> MG .flatMap(gb) { f(it, a) } })
139
+
140
140
inline fun <reified F > foldable (): Foldable <F > =
141
141
instance(InstanceParametrizedType (Foldable ::class .java, listOf (F ::class .java)))
0 commit comments