Skip to content

Commit

Permalink
Apply global fix to enforce lambda syntactic sugar (#985)
Browse files Browse the repository at this point in the history
* Apply global fix to enforce lambda syntactic sugar.

* Fix detekt
  • Loading branch information
pakoito committed Aug 12, 2018
1 parent 298171f commit f30946d
Show file tree
Hide file tree
Showing 118 changed files with 960 additions and 975 deletions.
Expand Up @@ -61,7 +61,7 @@ class HigherKindsFileGenerator(
val appliedTypeArgs = hk.typeArgs.dropLast(1)
val expandedAppliedTypeArgs = appliedTypeArgs.joinToString(", ")
val hkimpl = if (appliedTypeArgs.size == 1) "arrow.Kind" else "arrow.Kind${appliedTypeArgs.size}"
return "typealias ${hk.name.replace("Of$".toRegex(), { "PartialOf" })}<$expandedAppliedTypeArgs> = $hkimpl<${hk.markerName}, $expandedAppliedTypeArgs>"
return "typealias ${hk.name.replace("Of$".toRegex()) { "PartialOf" }}<$expandedAppliedTypeArgs> = $hkimpl<${hk.markerName}, $expandedAppliedTypeArgs>"
}

private fun genKindedJTypeAliases(hk: HigherKind): String =
Expand Down
Expand Up @@ -56,7 +56,7 @@ fun <A, B, C> PartialFunction<A, B>.andThen(f: (B) -> C): PartialFunction<A, C>
}

private class Lifted<A, B>(val pf: PartialFunction<A, B>) : (A) -> Option<B> {
override fun invoke(x: A): Option<B> = pf.andThen { Some(it) }.invokeOrElse(x, { None })
override fun invoke(x: A): Option<B> = pf.andThen { Some(it) }.invokeOrElse(x) { None }
}

fun <A, B> ((A) -> B).toPartialFunction(definedAt: (A) -> Boolean): PartialFunction<A, B> = PartialFunction(definedAt, this)
Expand Down
18 changes: 9 additions & 9 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/Cokleisli.kt
Expand Up @@ -11,28 +11,28 @@ typealias CoreaderT<F, A, B> = Cokleisli<F, A, B>
@higherkind
data class Cokleisli<F, A, B>(val MM: Comonad<F>, val run: CokleisliFun<F, A, B>) : CokleisliOf<F, A, B>, CokleisliKindedJ<F, A, B>, Comonad<F> by MM {

inline fun <C, D> bimap(noinline g: (D) -> A, crossinline f: (B) -> C): Cokleisli<F, D, C> = Cokleisli(MM, { f(run(it.map(g))) })
inline fun <C, D> bimap(noinline g: (D) -> A, crossinline f: (B) -> C): Cokleisli<F, D, C> = Cokleisli(MM) { f(run(it.map(g))) }

fun <D> lmap(g: (D) -> A): Cokleisli<F, D, B> = Cokleisli(MM, { run(it.map(g)) })
fun <D> lmap(g: (D) -> A): Cokleisli<F, D, B> = Cokleisli(MM) { run(it.map(g)) }

inline fun <C> map(crossinline f: (B) -> C): Cokleisli<F, A, C> = Cokleisli(MM, { f(run(it)) })
inline fun <C> map(crossinline f: (B) -> C): Cokleisli<F, A, C> = Cokleisli(MM) { f(run(it)) }

inline fun <C> contramapValue(crossinline f: (Kind<F, C>) -> Kind<F, A>): Cokleisli<F, C, B> = Cokleisli(MM, { run(f(it)) })
inline fun <C> contramapValue(crossinline f: (Kind<F, C>) -> Kind<F, A>): Cokleisli<F, C, B> = Cokleisli(MM) { run(f(it)) }

fun <D> compose(a: Cokleisli<F, D, A>): Cokleisli<F, D, B> = Cokleisli(MM, { run(it.coflatMap(a.run)) })
fun <D> compose(a: Cokleisli<F, D, A>): Cokleisli<F, D, B> = Cokleisli(MM) { run(it.coflatMap(a.run)) }

@JvmName("andThenK")
fun <C> andThen(a: Kind<F, C>): Cokleisli<F, A, C> = Cokleisli(MM, { run { a.extract() } })
fun <C> andThen(a: Kind<F, C>): Cokleisli<F, A, C> = Cokleisli(MM) { run { a.extract() } }

fun <C> andThen(a: Cokleisli<F, B, C>): Cokleisli<F, A, C> = a.compose(this)

inline fun <C> flatMap(crossinline f: (B) -> Cokleisli<F, A, C>): Cokleisli<F, A, C> = Cokleisli(MM, { f(run(it)).run(it) })
inline fun <C> flatMap(crossinline f: (B) -> Cokleisli<F, A, C>): Cokleisli<F, A, C> = Cokleisli(MM) { f(run(it)).run(it) }

companion object {
inline operator fun <F, A, B> invoke(MF: Comonad<F>, noinline run: (Kind<F, A>) -> B): Cokleisli<F, A, B> = Cokleisli(MF, run)

inline fun <F, A, B> just(MF: Comonad<F>, b: B): Cokleisli<F, A, B> = Cokleisli(MF, { b })
inline fun <F, A, B> just(MF: Comonad<F>, b: B): Cokleisli<F, A, B> = Cokleisli(MF) { b }

inline fun <F, B> ask(MF: Comonad<F>): Cokleisli<F, B, B> = Cokleisli(MF, { MF.run { it.extract() } })
inline fun <F, B> ask(MF: Comonad<F>): Cokleisli<F, B, B> = Cokleisli(MF) { MF.run { it.extract() } }
}
}
Expand Up @@ -13,8 +13,8 @@ data class Coproduct<F, G, A>(val run: Either<Kind<F, A>, Kind<G, A>>) : Coprodu

fun <B> coflatMap(CF: Comonad<F>, CG: Comonad<G>, f: (Coproduct<F, G, A>) -> B): Coproduct<F, G, B> =
Coproduct(run.bimap(
{ CF.run { it.coflatMap({ f(Coproduct(Left(it))) }) } },
{ CG.run { it.coflatMap({ f(Coproduct(Right(it))) }) } }
{ CF.run { it.coflatMap { f(Coproduct(Left(it))) } } },
{ CG.run { it.coflatMap { f(Coproduct(Right(it))) } } }
))

fun extract(CF: Comonad<F>, CG: Comonad<G>): A =
Expand All @@ -31,9 +31,9 @@ data class Coproduct<F, G, A>(val run: Either<Kind<F, A>, Kind<G, A>>) : Coprodu

fun <H, B> traverse(GA: Applicative<H>, FT: Traverse<F>, GT: Traverse<G>, f: (A) -> Kind<H, B>): Kind<H, Coproduct<F, G, B>> = GA.run {
run.fold({
FT.run { it.traverse(GA, f) }.map({ Coproduct<F, G, B>(Left(it)) })
FT.run { it.traverse(GA, f) }.map { Coproduct<F, G, B>(Left(it)) }
}, {
GT.run { it.traverse(GA, f) }.map({ Coproduct<F, G, B>(Right(it)) })
GT.run { it.traverse(GA, f) }.map { Coproduct<F, G, B>(Right(it)) }
})
}

Expand Down
24 changes: 12 additions & 12 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt
Expand Up @@ -55,44 +55,44 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,
}

inline fun <C> fold(FF: Functor<F>, crossinline l: (A) -> C, crossinline r: (B) -> C): Kind<F, C> = FF.run {
value.map({ either -> either.fold(l, r) })
value.map { either -> either.fold(l, r) }
}

fun <C> flatMap(MF: Monad<F>, f: (B) -> EitherT<F, A, C>): EitherT<F, A, C> =
flatMapF(MF, { it -> f(it).value })
flatMapF(MF) { it -> f(it).value }

fun <C> flatMapF(MF: Monad<F>, f: (B) -> Kind<F, Either<A, C>>): EitherT<F, A, C> = MF.run {
EitherT(value.flatMap({ either -> either.fold({ MF.just(Left(it)) }, { f(it) }) }))
EitherT(value.flatMap { either -> either.fold({ MF.just(Left(it)) }, { f(it) }) })
}

fun <C> cata(FF: Functor<F>, l: (A) -> C, r: (B) -> C): Kind<F, C> = fold(FF, l, r)

fun <C> liftF(FF: Functor<F>, fa: Kind<F, C>): EitherT<F, A, C> = FF.run {
EitherT(fa.map({ Right(it) }))
EitherT(fa.map { Right(it) })
}

fun <C> semiflatMap(MF: Monad<F>, f: (B) -> Kind<F, C>): EitherT<F, A, C> = flatMap(MF, { liftF(MF, f(it)) })
fun <C> semiflatMap(MF: Monad<F>, f: (B) -> Kind<F, C>): EitherT<F, A, C> = flatMap(MF) { liftF(MF, f(it)) }

fun <C> map(FF: Functor<F>, f: (B) -> C): EitherT<F, A, C> = FF.run {
EitherT(value.map({ it.map(f) }))
EitherT(value.map { it.map(f) })
}

fun <C> mapLeft(FF: Functor<F>, f: (A) -> C): EitherT<F, C, B> = FF.run {
EitherT(value.map({ it.mapLeft(f) }))
EitherT(value.map { it.mapLeft(f) })
}

fun exists(FF: Functor<F>, p: (B) -> Boolean): Kind<F, Boolean> = FF.run {
value.map({ it.exists(p) })
value.map { it.exists(p) }
}

fun <C, D> transform(FF: Functor<F>, f: (Either<A, B>) -> Either<C, D>): EitherT<F, C, D> = FF.run {
EitherT(value.map({ f(it) }))
EitherT(value.map { f(it) })
}

fun <C> subflatMap(FF: Functor<F>, f: (B) -> Either<A, C>): EitherT<F, A, C> = transform(FF, { it.flatMap(f = f) })
fun <C> subflatMap(FF: Functor<F>, f: (B) -> Either<A, C>): EitherT<F, A, C> = transform(FF) { it.flatMap(f = f) }

fun toOptionT(FF: Functor<F>): OptionT<F, B> = FF.run {
OptionT(value.map({ it.toOption() }))
OptionT(value.map { it.toOption() })
}

fun combineK(MF: Monad<F>, y: EitherTOf<F, A, B>): EitherT<F, A, B> = MF.run {
Expand All @@ -104,5 +104,5 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,
})
}

fun <C> ap(MF: Monad<F>, ff: EitherTOf<F, A, (B) -> C>): EitherT<F, A, C> = ff.fix().flatMap(MF, { f -> map(MF, f) })
fun <C> ap(MF: Monad<F>, ff: EitherTOf<F, A, (B) -> C>): EitherT<F, A, C> = ff.fix().flatMap(MF) { f -> map(MF, f) }
}
2 changes: 1 addition & 1 deletion modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt
Expand Up @@ -145,7 +145,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
fold({ lc }, { f(it, lc) }, { _, b -> f(b, lc) })

fun <G, C> traverse(GA: Applicative<G>, f: (B) -> Kind<G, C>): Kind<G, Ior<A, C>> = GA.run {
fold({ just(Left(it)) }, { f(it).map({ Right<A, C>(it) }) }, { _, b -> f(b).map({ Right<A, C>(it) }) })
fold({ just(Left(it)) }, { f(it).map { Right<A, C>(it) } }, { _, b -> f(b).map { Right<A, C>(it) } })
}

/**
Expand Down
12 changes: 6 additions & 6 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt
Expand Up @@ -64,9 +64,9 @@ class Kleisli<F, D, A>(val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, Kleis
* @param MF [Monad] for the context [F].
*/
fun <B> zip(MF: Monad<F>, o: Kleisli<F, D, B>): Kleisli<F, D, Tuple2<A, B>> =
flatMap(MF, { a ->
o.map(MF, { b -> Tuple2(a, b) })
})
flatMap(MF) { a ->
o.map(MF) { b -> Tuple2(a, b) }
}

/**
* Compose this arrow with another function to transform the input of the arrow.
Expand Down Expand Up @@ -99,7 +99,7 @@ class Kleisli<F, D, A>(val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, Kleis
* @param fb the new end of the arrow.
* @param MF [Monad] for the context [F].
*/
fun <B> andThen(MF: Monad<F>, fb: Kind<F, B>): Kleisli<F, D, B> = andThen(MF, { fb })
fun <B> andThen(MF: Monad<F>, fb: Kind<F, B>): Kleisli<F, D, B> = andThen(MF) { fb }

/**
* Handle error within context of [F] given a [MonadError] is defined for [F].
Expand All @@ -108,7 +108,7 @@ class Kleisli<F, D, A>(val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, Kleis
* @param ME [MonadError] for the context [F].
*/
fun <E> handleErrorWith(ME: MonadError<F, E>, f: (E) -> KleisliOf<F, D, A>): Kleisli<F, D, A> = Kleisli {
ME.run { run(it).handleErrorWith({ e: E -> f(e).fix().run(it) }) }
ME.run { run(it).handleErrorWith { e: E -> f(e).fix().run(it) } }
}

companion object {
Expand All @@ -128,7 +128,7 @@ class Kleisli<F, D, A>(val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, Kleis
* @param MF [Monad] for the context [F].
*/
fun <F, D, A, B> tailRecM(MF: Monad<F>, a: A, f: (A) -> KleisliOf<F, D, Either<A, B>>): Kleisli<F, D, B> =
Kleisli { b -> MF.tailRecM(a, { f(it).fix().run(b) }) }
Kleisli { b -> MF.tailRecM(a) { f(it).fix().run(b) } }

/**
* Create an arrow for a value of [A].
Expand Down
Expand Up @@ -37,7 +37,7 @@ data class ListK<out A>(val list: List<A>) : ListKOf<A>, List<A> by list {
}.fix()

fun <B> mapFilter(f: (A) -> Option<B>): ListK<B> =
flatMap({ a -> f(a).fold({ empty<B>() }, { just(it) }) })
flatMap { a -> f(a).fold({ empty<B>() }, { just(it) }) }

companion object {

Expand Down
4 changes: 2 additions & 2 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/MapK.kt
Expand Up @@ -43,9 +43,9 @@ data class MapK<K, out A>(val map: Map<K, A>) : MapKOf<K, A>, Map<K, A> by map {
this.map.foldLeft(b) { m, (k, v) -> f(m.k(), Tuple2(k, v)) }.k()

fun <G, B> traverse(GA: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, MapK<K, B>> = GA.run {
(Foldable.iterateRight(map.iterator(), Eval.always { just(emptyMap<K, B>().k()) }))({ kv, lbuf ->
(Foldable.iterateRight(map.iterator(), Eval.always { just(emptyMap<K, B>().k()) })) { kv, lbuf ->
f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).k() }
}).value()
}.value()
}

companion object
Expand Down
Expand Up @@ -47,9 +47,9 @@ class NonEmptyList<out A> private constructor(
fun <G, B> traverse(AG: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, NonEmptyList<B>> = with(AG) {
f(fix().head).map2Eval(Eval.always {
tail.k().traverse(AG, f)
}, {
}) {
NonEmptyList(it.a, it.b.fix().list)
}).value()
}.value()
}

fun <B> coflatMap(f: (NonEmptyListOf<A>) -> B): NonEmptyList<B> {
Expand Down
34 changes: 17 additions & 17 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt
Expand Up @@ -44,52 +44,52 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
}

inline fun <B> fold(FF: Functor<F>, crossinline default: () -> B, crossinline f: (A) -> B): Kind<F, B> = FF.run {
value.map({ option -> option.fold(default, f) })
value.map { option -> option.fold(default, f) }
}

fun <B> cata(FF: Functor<F>, default: () -> B, f: (A) -> B): Kind<F, B> = fold(FF, default, f)

fun <B> ap(MF: Monad<F>, ff: OptionTOf<F, (A) -> B>): OptionT<F, B> = ff.fix().flatMap(MF, { f -> map(MF, f) })
fun <B> ap(MF: Monad<F>, ff: OptionTOf<F, (A) -> B>): OptionT<F, B> = ff.fix().flatMap(MF) { f -> map(MF, f) }

fun <B> flatMap(MF: Monad<F>, f: (A) -> OptionT<F, B>): OptionT<F, B> = flatMapF(MF, { it -> f(it).value })
fun <B> flatMap(MF: Monad<F>, f: (A) -> OptionT<F, B>): OptionT<F, B> = flatMapF(MF) { it -> f(it).value }

fun <B> flatMapF(MF: Monad<F>, f: (A) -> Kind<F, Option<B>>): OptionT<F, B> = MF.run {
OptionT(value.flatMap({ option -> option.fold({ just(None) }, f) }))
OptionT(value.flatMap { option -> option.fold({ just(None) }, f) })
}

fun <B> liftF(FF: Functor<F>, fa: Kind<F, B>): OptionT<F, B> = FF.run {
OptionT(fa.map({ Some(it) }))
OptionT(fa.map { Some(it) })
}

fun <B> semiflatMap(MF: Monad<F>, f: (A) -> Kind<F, B>): OptionT<F, B> = flatMap(MF, { option -> liftF(MF, f(option)) })
fun <B> semiflatMap(MF: Monad<F>, f: (A) -> Kind<F, B>): OptionT<F, B> = flatMap(MF) { option -> liftF(MF, f(option)) }

fun <B> map(FF: Functor<F>, f: (A) -> B): OptionT<F, B> = FF.run {
OptionT(value.map({ it.map(f) }))
OptionT(value.map { it.map(f) })
}

fun getOrElse(FF: Functor<F>, default: () -> A): Kind<F, A> = FF.run { value.map({ it.getOrElse(default) }) }
fun getOrElse(FF: Functor<F>, default: () -> A): Kind<F, A> = FF.run { value.map { it.getOrElse(default) } }

fun getOrElseF(MF: Monad<F>, default: () -> Kind<F, A>): Kind<F, A> = MF.run {
value.flatMap({ it.fold(default, { just(it) }) })
value.flatMap { it.fold(default) { just(it) } }
}

fun filter(FF: Functor<F>, p: (A) -> Boolean): OptionT<F, A> = FF.run {
OptionT(value.map({ it.filter(p) }))
OptionT(value.map { it.filter(p) })
}

fun forall(FF: Functor<F>, p: (A) -> Boolean): Kind<F, Boolean> = FF.run {
value.map({ it.forall(p) })
value.map { it.forall(p) }
}

fun isDefined(FF: Functor<F>): Kind<F, Boolean> = FF.run {
value.map({ it.isDefined() })
value.map { it.isDefined() }
}

fun isEmpty(FF: Functor<F>): Kind<F, Boolean> = FF.run {
value.map({ it.isEmpty() })
value.map { it.isEmpty() }
}

fun orElse(MF: Monad<F>, default: () -> OptionT<F, A>): OptionT<F, A> = orElseF(MF, { default().value })
fun orElse(MF: Monad<F>, default: () -> OptionT<F, A>): OptionT<F, A> = orElseF(MF) { default().value }

fun orElseF(MF: Monad<F>, default: () -> Kind<F, Option<A>>): OptionT<F, A> = MF.run {
OptionT(value.flatMap {
Expand All @@ -101,10 +101,10 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
}

fun <B> transform(FF: Functor<F>, f: (Option<A>) -> Option<B>): OptionT<F, B> = FF.run {
OptionT(value.map({ f(it) }))
OptionT(value.map { f(it) })
}

fun <B> subflatMap(FF: Functor<F>, f: (A) -> Option<B>): OptionT<F, B> = transform(FF, { it.flatMap(f) })
fun <B> subflatMap(FF: Functor<F>, f: (A) -> Option<B>): OptionT<F, B> = transform(FF) { it.flatMap(f) }

fun <R> toLeft(FF: Functor<F>, default: () -> R): EitherT<F, A, R> =
EitherT(cata(FF, { Right(default()) }, { Left(it) }))
Expand All @@ -114,7 +114,7 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
}

fun <F, A, B> OptionTOf<F, A>.mapFilter(FF: Functor<F>, f: (A) -> Option<B>): OptionT<F, B> = FF.run {
OptionT(fix().value.map({ it.flatMap(f) }))
OptionT(fix().value.map { it.flatMap(f) })
}

fun <F, A> OptionTOf<F, A>.value(): Kind<F, Option<A>> = this.fix().value
Expand Up @@ -48,9 +48,9 @@ data class SortedMapK<A : Comparable<A>, B>(val map: SortedMap<A, B>) : SortedMa
this.map.foldLeft(c) { m: SortedMap<A, C>, (a, b) -> f(m.k(), Tuple2(a, b)) }.k()

fun <G, C> traverse(GA: Applicative<G>, f: (B) -> Kind<G, C>): Kind<G, SortedMapK<A, C>> = GA.run {
(Foldable.iterateRight(map.iterator(), Eval.always { just(sortedMapOf<A, C>().k()) }))({ kv, lbuf ->
(Foldable.iterateRight(map.iterator(), Eval.always { just(sortedMapOf<A, C>().k()) })) { kv, lbuf ->
f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).toSortedMap().k() }
}).value()
}.value()
}

companion object
Expand All @@ -63,7 +63,7 @@ fun <A : Comparable<A>, B> Option<Tuple2<A, B>>.k(): SortedMapK<A, B> = this.fol
{ mapEntry -> sortedMapOf<A, B>(mapEntry.a to mapEntry.b).k() }
)

inline fun <K: Comparable<K>, V, G> SortedMapKOf<K, Kind<G, V>>.sequence(GA: Applicative<G>): Kind<G, SortedMapK<K, V>> =
inline fun <K : Comparable<K>, V, G> SortedMapKOf<K, Kind<G, V>>.sequence(GA: Applicative<G>): Kind<G, SortedMapK<K, V>> =
fix().traverse(GA, ::identity)

fun <A : Comparable<A>, B> List<Map.Entry<A, B>>.k(): SortedMapK<A, B> =
Expand Down
4 changes: 2 additions & 2 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt
Expand Up @@ -56,9 +56,9 @@ fun <S, A> StateFun<S, A>.toState(): State<S, A> = State(IdBimonad, this)
fun <S, A> StateFunOf<S, A>.toState(): State<S, A> = State(this)

fun <S, T, P1, R> State<S, T>.map(sx: State<S, P1>, f: (T, P1) -> R): State<S, R> =
flatMap(IdBimonad, { t -> sx.map { x -> f(t, x) } }).fix()
flatMap(IdBimonad) { t -> sx.map { x -> f(t, x) } }.fix()

fun <S, T, R> State<S, T>.map(f: (T) -> R): State<S, R> = flatMap(IdBimonad, { t -> StateApi.just<S, R>(f(t)) }).fix()
fun <S, T, R> State<S, T>.map(f: (T) -> R): State<S, R> = flatMap(IdBimonad) { t -> StateApi.just<S, R>(f(t)) }.fix()

/**
* Alias for [StateT.run] `StateT<ForId, S, A>`
Expand Down
6 changes: 3 additions & 3 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/StateT.kt
Expand Up @@ -157,7 +157,7 @@ class StateT<F, S, A>(
* @param f the function to apply.
* @param FF [Functor] for the context [F].
*/
fun <B> map(FF: Functor<F>, f: (A) -> B): StateT<F, S, B> = transform(FF, { (s, a) -> Tuple2(s, f(a)) })
fun <B> map(FF: Functor<F>, f: (A) -> B): StateT<F, S, B> = transform(FF) { (s, a) -> Tuple2(s, f(a)) }

/**
* Combine with another [StateT] of same context [F] and state [S].
Expand Down Expand Up @@ -201,15 +201,15 @@ class StateT<F, S, A>(
* @param MF [Monad] for the context [F].
*/
fun <B> ap(MF: Monad<F>, ff: StateTOf<F, S, (A) -> B>): StateT<F, S, B> =
ff.fix().map2(MF, this, { f, a -> f(a) })
ff.fix().map2(MF, this) { f, a -> f(a) }

/**
* Create a product of the value types of [StateT].
*
* @param sb other stateful computation.
* @param MF [Monad] for the context [F].
*/
fun <B> product(MF: Monad<F>, sb: StateT<F, S, B>): StateT<F, S, Tuple2<A, B>> = map2(MF, sb, { a, b -> Tuple2(a, b) })
fun <B> product(MF: Monad<F>, sb: StateT<F, S, B>): StateT<F, S, Tuple2<A, B>> = map2(MF, sb) { a, b -> Tuple2(a, b) }

/**
* Map the value [A] to another [StateT] object for the same state [S] and context [F] and flatten the structure.
Expand Down
Expand Up @@ -102,7 +102,7 @@ sealed class Validated<out E, out A> : ValidatedOf<E, A> {
/**
* Apply a function to a Valid value, returning a new Valid value
*/
fun <B> map(f: (A) -> B): Validated<E, B> = bimap(::identity, { f(it) })
fun <B> map(f: (A) -> B): Validated<E, B> = bimap(::identity) { f(it) }

/**
* Apply a function to an Invalid value, returning a new Invalid value.
Expand Down Expand Up @@ -177,7 +177,7 @@ fun <E, A> ValidatedOf<E, A>.orElse(default: () -> Validated<E, A>): Validated<E
fun <E, A, B> ValidatedOf<E, A>.ap(SE: Semigroup<E>, f: Validated<E, (A) -> B>): Validated<E, B> =
fix().fold(
{ e -> f.fold({ Invalid(SE.run { it.combine(e) }) }, { Invalid(e) }) },
{ a -> f.fold(::Invalid, { Valid(it(a)) }) }
{ a -> f.fold(::Invalid) { Valid(it(a)) } }
)

fun <E, A> ValidatedOf<E, A>.handleLeftWith(f: (E) -> ValidatedOf<E, A>): Validated<E, A> =
Expand Down

0 comments on commit f30946d

Please sign in to comment.