diff --git a/modules/core/arrow-annotations-processor/src/main/java/arrow/higherkinds/HigherKindsFileGenerator.kt b/modules/core/arrow-annotations-processor/src/main/java/arrow/higherkinds/HigherKindsFileGenerator.kt index 6f64a0eb7b3..2409179e4c6 100644 --- a/modules/core/arrow-annotations-processor/src/main/java/arrow/higherkinds/HigherKindsFileGenerator.kt +++ b/modules/core/arrow-annotations-processor/src/main/java/arrow/higherkinds/HigherKindsFileGenerator.kt @@ -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 = diff --git a/modules/core/arrow-core/src/main/kotlin/arrow/core/PartialFunction.kt b/modules/core/arrow-core/src/main/kotlin/arrow/core/PartialFunction.kt index e0ee204e1df..f20838dabc1 100644 --- a/modules/core/arrow-core/src/main/kotlin/arrow/core/PartialFunction.kt +++ b/modules/core/arrow-core/src/main/kotlin/arrow/core/PartialFunction.kt @@ -56,7 +56,7 @@ fun PartialFunction.andThen(f: (B) -> C): PartialFunction } private class Lifted(val pf: PartialFunction) : (A) -> Option { - override fun invoke(x: A): Option = pf.andThen { Some(it) }.invokeOrElse(x, { None }) + override fun invoke(x: A): Option = pf.andThen { Some(it) }.invokeOrElse(x) { None } } fun ((A) -> B).toPartialFunction(definedAt: (A) -> Boolean): PartialFunction = PartialFunction(definedAt, this) diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/Cokleisli.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/Cokleisli.kt index 7fbfa1455c1..847510f197a 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/Cokleisli.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/Cokleisli.kt @@ -11,28 +11,28 @@ typealias CoreaderT = Cokleisli @higherkind data class Cokleisli(val MM: Comonad, val run: CokleisliFun) : CokleisliOf, CokleisliKindedJ, Comonad by MM { - inline fun bimap(noinline g: (D) -> A, crossinline f: (B) -> C): Cokleisli = Cokleisli(MM, { f(run(it.map(g))) }) + inline fun bimap(noinline g: (D) -> A, crossinline f: (B) -> C): Cokleisli = Cokleisli(MM) { f(run(it.map(g))) } - fun lmap(g: (D) -> A): Cokleisli = Cokleisli(MM, { run(it.map(g)) }) + fun lmap(g: (D) -> A): Cokleisli = Cokleisli(MM) { run(it.map(g)) } - inline fun map(crossinline f: (B) -> C): Cokleisli = Cokleisli(MM, { f(run(it)) }) + inline fun map(crossinline f: (B) -> C): Cokleisli = Cokleisli(MM) { f(run(it)) } - inline fun contramapValue(crossinline f: (Kind) -> Kind): Cokleisli = Cokleisli(MM, { run(f(it)) }) + inline fun contramapValue(crossinline f: (Kind) -> Kind): Cokleisli = Cokleisli(MM) { run(f(it)) } - fun compose(a: Cokleisli): Cokleisli = Cokleisli(MM, { run(it.coflatMap(a.run)) }) + fun compose(a: Cokleisli): Cokleisli = Cokleisli(MM) { run(it.coflatMap(a.run)) } @JvmName("andThenK") - fun andThen(a: Kind): Cokleisli = Cokleisli(MM, { run { a.extract() } }) + fun andThen(a: Kind): Cokleisli = Cokleisli(MM) { run { a.extract() } } fun andThen(a: Cokleisli): Cokleisli = a.compose(this) - inline fun flatMap(crossinline f: (B) -> Cokleisli): Cokleisli = Cokleisli(MM, { f(run(it)).run(it) }) + inline fun flatMap(crossinline f: (B) -> Cokleisli): Cokleisli = Cokleisli(MM) { f(run(it)).run(it) } companion object { inline operator fun invoke(MF: Comonad, noinline run: (Kind) -> B): Cokleisli = Cokleisli(MF, run) - inline fun just(MF: Comonad, b: B): Cokleisli = Cokleisli(MF, { b }) + inline fun just(MF: Comonad, b: B): Cokleisli = Cokleisli(MF) { b } - inline fun ask(MF: Comonad): Cokleisli = Cokleisli(MF, { MF.run { it.extract() } }) + inline fun ask(MF: Comonad): Cokleisli = Cokleisli(MF) { MF.run { it.extract() } } } } diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/Coproduct.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/Coproduct.kt index 4d7d8115dd0..9217da13116 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/Coproduct.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/Coproduct.kt @@ -13,8 +13,8 @@ data class Coproduct(val run: Either, Kind>) : Coprodu fun coflatMap(CF: Comonad, CG: Comonad, f: (Coproduct) -> B): Coproduct = 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, CG: Comonad): A = @@ -31,9 +31,9 @@ data class Coproduct(val run: Either, Kind>) : Coprodu fun traverse(GA: Applicative, FT: Traverse, GT: Traverse, f: (A) -> Kind): Kind> = GA.run { run.fold({ - FT.run { it.traverse(GA, f) }.map({ Coproduct(Left(it)) }) + FT.run { it.traverse(GA, f) }.map { Coproduct(Left(it)) } }, { - GT.run { it.traverse(GA, f) }.map({ Coproduct(Right(it)) }) + GT.run { it.traverse(GA, f) }.map { Coproduct(Right(it)) } }) } diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt index cba340dec20..ce2c6588792 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt @@ -55,44 +55,44 @@ data class EitherT(val value: Kind>) : EitherTOf fold(FF: Functor, crossinline l: (A) -> C, crossinline r: (B) -> C): Kind = FF.run { - value.map({ either -> either.fold(l, r) }) + value.map { either -> either.fold(l, r) } } fun flatMap(MF: Monad, f: (B) -> EitherT): EitherT = - flatMapF(MF, { it -> f(it).value }) + flatMapF(MF) { it -> f(it).value } fun flatMapF(MF: Monad, f: (B) -> Kind>): EitherT = 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 cata(FF: Functor, l: (A) -> C, r: (B) -> C): Kind = fold(FF, l, r) fun liftF(FF: Functor, fa: Kind): EitherT = FF.run { - EitherT(fa.map({ Right(it) })) + EitherT(fa.map { Right(it) }) } - fun semiflatMap(MF: Monad, f: (B) -> Kind): EitherT = flatMap(MF, { liftF(MF, f(it)) }) + fun semiflatMap(MF: Monad, f: (B) -> Kind): EitherT = flatMap(MF) { liftF(MF, f(it)) } fun map(FF: Functor, f: (B) -> C): EitherT = FF.run { - EitherT(value.map({ it.map(f) })) + EitherT(value.map { it.map(f) }) } fun mapLeft(FF: Functor, f: (A) -> C): EitherT = FF.run { - EitherT(value.map({ it.mapLeft(f) })) + EitherT(value.map { it.mapLeft(f) }) } fun exists(FF: Functor, p: (B) -> Boolean): Kind = FF.run { - value.map({ it.exists(p) }) + value.map { it.exists(p) } } fun transform(FF: Functor, f: (Either) -> Either): EitherT = FF.run { - EitherT(value.map({ f(it) })) + EitherT(value.map { f(it) }) } - fun subflatMap(FF: Functor, f: (B) -> Either): EitherT = transform(FF, { it.flatMap(f = f) }) + fun subflatMap(FF: Functor, f: (B) -> Either): EitherT = transform(FF) { it.flatMap(f = f) } fun toOptionT(FF: Functor): OptionT = FF.run { - OptionT(value.map({ it.toOption() })) + OptionT(value.map { it.toOption() }) } fun combineK(MF: Monad, y: EitherTOf): EitherT = MF.run { @@ -104,5 +104,5 @@ data class EitherT(val value: Kind>) : EitherTOf ap(MF: Monad, ff: EitherTOf C>): EitherT = ff.fix().flatMap(MF, { f -> map(MF, f) }) + fun ap(MF: Monad, ff: EitherTOf C>): EitherT = ff.fix().flatMap(MF) { f -> map(MF, f) } } diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt index e9d7c654fa0..0ef313d7511 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt @@ -145,7 +145,7 @@ sealed class Ior : IorOf { fold({ lc }, { f(it, lc) }, { _, b -> f(b, lc) }) fun traverse(GA: Applicative, f: (B) -> Kind): Kind> = GA.run { - fold({ just(Left(it)) }, { f(it).map({ Right(it) }) }, { _, b -> f(b).map({ Right(it) }) }) + fold({ just(Left(it)) }, { f(it).map { Right(it) } }, { _, b -> f(b).map { Right(it) } }) } /** diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt index fa6de79c86a..d1ec1d22f29 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt @@ -64,9 +64,9 @@ class Kleisli(val run: KleisliFun) : KleisliOf, Kleis * @param MF [Monad] for the context [F]. */ fun zip(MF: Monad, o: Kleisli): Kleisli> = - 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. @@ -99,7 +99,7 @@ class Kleisli(val run: KleisliFun) : KleisliOf, Kleis * @param fb the new end of the arrow. * @param MF [Monad] for the context [F]. */ - fun andThen(MF: Monad, fb: Kind): Kleisli = andThen(MF, { fb }) + fun andThen(MF: Monad, fb: Kind): Kleisli = andThen(MF) { fb } /** * Handle error within context of [F] given a [MonadError] is defined for [F]. @@ -108,7 +108,7 @@ class Kleisli(val run: KleisliFun) : KleisliOf, Kleis * @param ME [MonadError] for the context [F]. */ fun handleErrorWith(ME: MonadError, f: (E) -> KleisliOf): Kleisli = 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 { @@ -128,7 +128,7 @@ class Kleisli(val run: KleisliFun) : KleisliOf, Kleis * @param MF [Monad] for the context [F]. */ fun tailRecM(MF: Monad, a: A, f: (A) -> KleisliOf>): Kleisli = - 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]. diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/ListK.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/ListK.kt index 3a28ae15fb9..3db9383c188 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/ListK.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/ListK.kt @@ -37,7 +37,7 @@ data class ListK(val list: List) : ListKOf, List by list { }.fix() fun mapFilter(f: (A) -> Option): ListK = - flatMap({ a -> f(a).fold({ empty() }, { just(it) }) }) + flatMap { a -> f(a).fold({ empty() }, { just(it) }) } companion object { diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/MapK.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/MapK.kt index 1c31a8ea6d8..eb9aed963e8 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/MapK.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/MapK.kt @@ -43,9 +43,9 @@ data class MapK(val map: Map) : MapKOf, Map by map { this.map.foldLeft(b) { m, (k, v) -> f(m.k(), Tuple2(k, v)) }.k() fun traverse(GA: Applicative, f: (A) -> Kind): Kind> = GA.run { - (Foldable.iterateRight(map.iterator(), Eval.always { just(emptyMap().k()) }))({ kv, lbuf -> + (Foldable.iterateRight(map.iterator(), Eval.always { just(emptyMap().k()) })) { kv, lbuf -> f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).k() } - }).value() + }.value() } companion object diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/NonEmptyList.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/NonEmptyList.kt index fb3b788c691..f116e5964ba 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/NonEmptyList.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/NonEmptyList.kt @@ -47,9 +47,9 @@ class NonEmptyList private constructor( fun traverse(AG: Applicative, f: (A) -> Kind): Kind> = with(AG) { f(fix().head).map2Eval(Eval.always { tail.k().traverse(AG, f) - }, { + }) { NonEmptyList(it.a, it.b.fix().list) - }).value() + }.value() } fun coflatMap(f: (NonEmptyListOf) -> B): NonEmptyList { diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt index 1ae88e86908..95ba91bf6a5 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt @@ -44,52 +44,52 @@ data class OptionT(val value: Kind>) : OptionTOf, Optio } inline fun fold(FF: Functor, crossinline default: () -> B, crossinline f: (A) -> B): Kind = FF.run { - value.map({ option -> option.fold(default, f) }) + value.map { option -> option.fold(default, f) } } fun cata(FF: Functor, default: () -> B, f: (A) -> B): Kind = fold(FF, default, f) - fun ap(MF: Monad, ff: OptionTOf B>): OptionT = ff.fix().flatMap(MF, { f -> map(MF, f) }) + fun ap(MF: Monad, ff: OptionTOf B>): OptionT = ff.fix().flatMap(MF) { f -> map(MF, f) } - fun flatMap(MF: Monad, f: (A) -> OptionT): OptionT = flatMapF(MF, { it -> f(it).value }) + fun flatMap(MF: Monad, f: (A) -> OptionT): OptionT = flatMapF(MF) { it -> f(it).value } fun flatMapF(MF: Monad, f: (A) -> Kind>): OptionT = MF.run { - OptionT(value.flatMap({ option -> option.fold({ just(None) }, f) })) + OptionT(value.flatMap { option -> option.fold({ just(None) }, f) }) } fun liftF(FF: Functor, fa: Kind): OptionT = FF.run { - OptionT(fa.map({ Some(it) })) + OptionT(fa.map { Some(it) }) } - fun semiflatMap(MF: Monad, f: (A) -> Kind): OptionT = flatMap(MF, { option -> liftF(MF, f(option)) }) + fun semiflatMap(MF: Monad, f: (A) -> Kind): OptionT = flatMap(MF) { option -> liftF(MF, f(option)) } fun map(FF: Functor, f: (A) -> B): OptionT = FF.run { - OptionT(value.map({ it.map(f) })) + OptionT(value.map { it.map(f) }) } - fun getOrElse(FF: Functor, default: () -> A): Kind = FF.run { value.map({ it.getOrElse(default) }) } + fun getOrElse(FF: Functor, default: () -> A): Kind = FF.run { value.map { it.getOrElse(default) } } fun getOrElseF(MF: Monad, default: () -> Kind): Kind = MF.run { - value.flatMap({ it.fold(default, { just(it) }) }) + value.flatMap { it.fold(default) { just(it) } } } fun filter(FF: Functor, p: (A) -> Boolean): OptionT = FF.run { - OptionT(value.map({ it.filter(p) })) + OptionT(value.map { it.filter(p) }) } fun forall(FF: Functor, p: (A) -> Boolean): Kind = FF.run { - value.map({ it.forall(p) }) + value.map { it.forall(p) } } fun isDefined(FF: Functor): Kind = FF.run { - value.map({ it.isDefined() }) + value.map { it.isDefined() } } fun isEmpty(FF: Functor): Kind = FF.run { - value.map({ it.isEmpty() }) + value.map { it.isEmpty() } } - fun orElse(MF: Monad, default: () -> OptionT): OptionT = orElseF(MF, { default().value }) + fun orElse(MF: Monad, default: () -> OptionT): OptionT = orElseF(MF) { default().value } fun orElseF(MF: Monad, default: () -> Kind>): OptionT = MF.run { OptionT(value.flatMap { @@ -101,10 +101,10 @@ data class OptionT(val value: Kind>) : OptionTOf, Optio } fun transform(FF: Functor, f: (Option) -> Option): OptionT = FF.run { - OptionT(value.map({ f(it) })) + OptionT(value.map { f(it) }) } - fun subflatMap(FF: Functor, f: (A) -> Option): OptionT = transform(FF, { it.flatMap(f) }) + fun subflatMap(FF: Functor, f: (A) -> Option): OptionT = transform(FF) { it.flatMap(f) } fun toLeft(FF: Functor, default: () -> R): EitherT = EitherT(cata(FF, { Right(default()) }, { Left(it) })) @@ -114,7 +114,7 @@ data class OptionT(val value: Kind>) : OptionTOf, Optio } fun OptionTOf.mapFilter(FF: Functor, f: (A) -> Option): OptionT = FF.run { - OptionT(fix().value.map({ it.flatMap(f) })) + OptionT(fix().value.map { it.flatMap(f) }) } fun OptionTOf.value(): Kind> = this.fix().value diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/SortedMapK.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/SortedMapK.kt index ec946c9ab71..5cb7057e02f 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/SortedMapK.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/SortedMapK.kt @@ -48,9 +48,9 @@ data class SortedMapK, B>(val map: SortedMap) : SortedMa this.map.foldLeft(c) { m: SortedMap, (a, b) -> f(m.k(), Tuple2(a, b)) }.k() fun traverse(GA: Applicative, f: (B) -> Kind): Kind> = GA.run { - (Foldable.iterateRight(map.iterator(), Eval.always { just(sortedMapOf().k()) }))({ kv, lbuf -> + (Foldable.iterateRight(map.iterator(), Eval.always { just(sortedMapOf().k()) })) { kv, lbuf -> f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).toSortedMap().k() } - }).value() + }.value() } companion object @@ -63,7 +63,7 @@ fun , B> Option>.k(): SortedMapK = this.fol { mapEntry -> sortedMapOf(mapEntry.a to mapEntry.b).k() } ) -inline fun , V, G> SortedMapKOf>.sequence(GA: Applicative): Kind> = +inline fun , V, G> SortedMapKOf>.sequence(GA: Applicative): Kind> = fix().traverse(GA, ::identity) fun , B> List>.k(): SortedMapK = diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt index 1a48f49f314..a79a73e7b6f 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt @@ -56,9 +56,9 @@ fun StateFun.toState(): State = State(IdBimonad, this) fun StateFunOf.toState(): State = State(this) fun State.map(sx: State, f: (T, P1) -> R): State = - flatMap(IdBimonad, { t -> sx.map { x -> f(t, x) } }).fix() + flatMap(IdBimonad) { t -> sx.map { x -> f(t, x) } }.fix() -fun State.map(f: (T) -> R): State = flatMap(IdBimonad, { t -> StateApi.just(f(t)) }).fix() +fun State.map(f: (T) -> R): State = flatMap(IdBimonad) { t -> StateApi.just(f(t)) }.fix() /** * Alias for [StateT.run] `StateT` diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/StateT.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/StateT.kt index 2db5d7a469d..1ad3102ad30 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/StateT.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/StateT.kt @@ -157,7 +157,7 @@ class StateT( * @param f the function to apply. * @param FF [Functor] for the context [F]. */ - fun map(FF: Functor, f: (A) -> B): StateT = transform(FF, { (s, a) -> Tuple2(s, f(a)) }) + fun map(FF: Functor, f: (A) -> B): StateT = transform(FF) { (s, a) -> Tuple2(s, f(a)) } /** * Combine with another [StateT] of same context [F] and state [S]. @@ -201,7 +201,7 @@ class StateT( * @param MF [Monad] for the context [F]. */ fun ap(MF: Monad, ff: StateTOf B>): StateT = - 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]. @@ -209,7 +209,7 @@ class StateT( * @param sb other stateful computation. * @param MF [Monad] for the context [F]. */ - fun product(MF: Monad, sb: StateT): StateT> = map2(MF, sb, { a, b -> Tuple2(a, b) }) + fun product(MF: Monad, sb: StateT): StateT> = 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. diff --git a/modules/core/arrow-data/src/main/kotlin/arrow/data/Validated.kt b/modules/core/arrow-data/src/main/kotlin/arrow/data/Validated.kt index 885016331d5..983743f4933 100644 --- a/modules/core/arrow-data/src/main/kotlin/arrow/data/Validated.kt +++ b/modules/core/arrow-data/src/main/kotlin/arrow/data/Validated.kt @@ -102,7 +102,7 @@ sealed class Validated : ValidatedOf { /** * Apply a function to a Valid value, returning a new Valid value */ - fun map(f: (A) -> B): Validated = bimap(::identity, { f(it) }) + fun map(f: (A) -> B): Validated = bimap(::identity) { f(it) } /** * Apply a function to an Invalid value, returning a new Invalid value. @@ -177,7 +177,7 @@ fun ValidatedOf.orElse(default: () -> Validated): Validated ValidatedOf.ap(SE: Semigroup, f: Validated B>): Validated = 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 ValidatedOf.handleLeftWith(f: (E) -> ValidatedOf): Validated = diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/ConstTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/ConstTest.kt index d3f181997ee..bf3893e96be 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/ConstTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/ConstTest.kt @@ -18,8 +18,8 @@ class ConstTest : UnitSpec() { testLaws( TraverseFilterLaws.laws(this, this, { Const(it) }, Eq.any()), ApplicativeLaws.laws(this, Eq.any()), - EqLaws.laws(Const.eq(Eq.any()), { Const(it) }), - ShowLaws.laws(Const.show(), Const.eq(Eq.any()), { Const(it) }) + EqLaws.laws(Const.eq(Eq.any())) { Const(it) }, + ShowLaws.laws(Const.show(), Const.eq(Eq.any())) { Const(it) } ) } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/CoreaderTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/CoreaderTest.kt index 149fad4b87b..57098018a80 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/CoreaderTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/CoreaderTest.kt @@ -24,28 +24,28 @@ class CoreaderTest : UnitSpec() { "flatMap should map over the inner value" { forAll { num: Int -> - invoke({ it -> it * 2 }).flatMap { a -> Coreader.just(this, a * 3) } + invoke { it -> it * 2 }.flatMap { a -> Coreader.just(this, a * 3) } .runId(num) == num * 6 } } "bimap should map over both sides of the run function" { forAll { num: Int -> - invoke({ it -> it * 2 }).bimap({ a: String -> Integer.parseInt(a) }, { it.toString() }) + invoke { it -> it * 2 }.bimap({ a: String -> Integer.parseInt(a) }, { it.toString() }) .runId("$num") == "${num * 2}" } } "lmap should map over the left side of the function" { forAll { num: Int -> - invoke({ it -> it * 2 }).lmap { a: String -> Integer.parseInt(a) } + invoke { it -> it * 2 }.lmap { a: String -> Integer.parseInt(a) } .runId("$num") == num * 2 } } "contramapValue" { forAll { num: Int -> - invoke({ it -> it * 2 }).contramapValue { a: IdOf -> Id(a.value() * 3) } + invoke { it -> it * 2 }.contramapValue { a: IdOf -> Id(a.value() * 3) } .runId(num) == num * 6 } } @@ -61,7 +61,7 @@ class CoreaderTest : UnitSpec() { cokleisli.andThen(Id(3)).run(Id(0)) shouldBe 3 - cokleisli.andThen(invoke({ it + 1 })).run(Id(0)) shouldBe 1 + cokleisli.andThen(invoke { it + 1 }).run(Id(0)) shouldBe 1 } } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTTest.kt index 509ff1a3c50..60b45495741 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTTest.kt @@ -31,9 +31,9 @@ class EitherTTest : UnitSpec() { forAll { i: Int, j: Int -> val left: Either = Left(i) val right: Either = Right(j) - EitherT(Option(left)).mapLeft(Option.functor(), { it + 1 }) == EitherT(Option(Left(i + 1))) && - EitherT(Option(right)).mapLeft(Option.functor(), { it + 1 }) == EitherT(Option(right)) && - EitherT(Option.empty>()).mapLeft(Option.functor(), { it + 1 }) == EitherT(Option.empty>()) + EitherT(Option(left)).mapLeft(Option.functor()) { it + 1 } == EitherT(Option(Left(i + 1))) && + EitherT(Option(right)).mapLeft(Option.functor()) { it + 1 } == EitherT(Option(right)) && + EitherT(Option.empty>()).mapLeft(Option.functor()) { it + 1 } == EitherT(Option.empty>()) } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTest.kt index 9ab8865dfef..bfc6bc76bcf 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/EitherTest.kt @@ -30,8 +30,8 @@ class EitherTest : UnitSpec() { BifunctorLaws.laws(Either.bifunctor(), { Right(it) }, EQ2), SemigroupLaws.laws(Either.semigroup(String.semigroup(), String.semigroup()), Either.right("1"), Either.right("2"), Either.right("3"), Either.eq(String.eq(), String.eq())), MonoidLaws.laws(Either.monoid(MOL=String.monoid(), MOR = Int.monoid()), Either.right(1), Either.eq(String.eq(), Int.eq())), - EqLaws.laws(Either.eq(String.eq(), Int.eq()), { Right(it) }), - ShowLaws.laws(Either.show(), Either.eq(String.eq(), Int.eq()), { Right(it) }), + EqLaws.laws(Either.eq(String.eq(), Int.eq())) { Right(it) }, + ShowLaws.laws(Either.show(), Either.eq(String.eq(), Int.eq())) { Right(it) }, MonadErrorLaws.laws(this, Eq.any(), Eq.any()), TraverseLaws.laws(this, this, { Right(it) }, Eq.any()), SemigroupKLaws.laws(Either.semigroupK(), Either.applicative(), EQ) diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/IorTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/IorTest.kt index 4e7cc4e624e..59ab25792b2 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/IorTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/IorTest.kt @@ -33,7 +33,7 @@ class IorTest : UnitSpec() { ForIor(Int.semigroup()) extensions { testLaws( BifunctorLaws.laws(Ior.bifunctor(), { Ior.Both(it, it) }, EQ2), - EqLaws.laws(EQ, { Right(it) }), + EqLaws.laws(EQ) { Right(it) }, ShowLaws.laws(Ior.show(), EQ) { Right(it) }, MonadLaws.laws(this, Eq.any()), TraverseLaws.laws(Ior.traverse(), this, ::Right, Eq.any()) @@ -130,7 +130,7 @@ class IorTest : UnitSpec() { "Ior.monad.flatMap should combine left values" { val ior1 = Ior.Both(3, "Hello, world!") - val iorResult = intIorMonad.run { ior1.flatMap({ Ior.Left(7) }) } + val iorResult = intIorMonad.run { ior1.flatMap { Ior.Left(7) } } iorResult shouldBe Ior.Left(10) } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/KleisliTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/KleisliTest.kt index c918b2c1994..a2687cd25ff 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/KleisliTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/KleisliTest.kt @@ -22,11 +22,11 @@ class KleisliTest : UnitSpec() { } "andThen should continue sequence" { - val kleisli: Kleisli = Kleisli({ a: Int -> Id(a) }) + val kleisli: Kleisli = Kleisli { a: Int -> Id(a) } kleisli.andThen(Id.monad(), Id(3)).run(0).fix().value shouldBe 3 - kleisli.andThen(Id.monad(), { b -> Id(b + 1) }).run(0).fix().value shouldBe 1 + kleisli.andThen(Id.monad()) { b -> Id(b + 1) }.run(0).fix().value shouldBe 1 } } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTTest.kt index b2fae6e1ca9..ae11bd399ba 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTTest.kt @@ -57,25 +57,25 @@ class OptionTTest : UnitSpec() { "toLeft for Some should build a correct EitherT" { forAll { a: Int, b: String -> - OptionT.fromOption(this.NELM, Some(a)).toLeft(this.NELM, { b }) == EitherT.left(this.NELM, a) + OptionT.fromOption(this.NELM, Some(a)).toLeft(this.NELM) { b } == EitherT.left(this.NELM, a) } } "toLeft for None should build a correct EitherT" { forAll { a: Int, b: String -> - OptionT.fromOption(this.NELM, None).toLeft(this.NELM, { b }) == EitherT.right(this.NELM, b) + OptionT.fromOption(this.NELM, None).toLeft(this.NELM) { b } == EitherT.right(this.NELM, b) } } "toRight for Some should build a correct EitherT" { forAll { a: Int, b: String -> - OptionT.fromOption(this.NELM, Some(b)).toRight(this.NELM, { a }) == EitherT.right(this.NELM, b) + OptionT.fromOption(this.NELM, Some(b)).toRight(this.NELM) { a } == EitherT.right(this.NELM, b) } } "toRight for None should build a correct EitherT" { forAll { a: Int, b: String -> - OptionT.fromOption(this.NELM, None).toRight(this.NELM, { a }) == EitherT.left(this.NELM, a) + OptionT.fromOption(this.NELM, None).toRight(this.NELM) { a } == EitherT.left(this.NELM, a) } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTest.kt index 6e240942f08..f641a1ac0c5 100755 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/OptionTest.kt @@ -43,8 +43,8 @@ class OptionTest : UnitSpec() { ForOption extensions { testLaws( - EqLaws.laws(Option.eq(Int.eq()), { genOption(Gen.int()).generate() }), - ShowLaws.laws(Option.show(), Option.eq(Int.eq()), { Some(it) }), + EqLaws.laws(Option.eq(Int.eq())) { genOption(Gen.int()).generate() }, + ShowLaws.laws(Option.show(), Option.eq(Int.eq())) { Some(it) }, MonoidLaws.laws(Option.monoid(Int.monoid()), Some(1), Option.eq(Int.eq())), //testLaws(MonadErrorLaws.laws(monadError(), Eq.any(), EQ_EITHER)) TODO reenable once the MonadErrorLaws are parametric to `E` TraverseFilterLaws.laws(this, this, ::Some, Eq.any()), diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/ReducibleTests.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/ReducibleTests.kt index f85a7b1b5c8..ed8843302aa 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/ReducibleTests.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/ReducibleTests.kt @@ -45,15 +45,15 @@ class ReducibleTests : UnitSpec() { val tail = (2 to 10).toList() val total = 1 + tail.sum() val nel = NonEmptyList(1, tail) - nel.reduceLeft({ a, b -> a + b }) shouldBe total - nel.reduceRight({ x, ly -> ly.map({ x + it }) }).value() shouldBe (total) + nel.reduceLeft { a, b -> a + b } shouldBe total + nel.reduceRight { x, ly -> ly.map { x + it } }.value() shouldBe (total) nel.reduce(this) shouldBe total // more basic checks val names = NonEmptyList.of("Aaron", "Betty", "Calvin", "Deirdra") - val totalLength = names.all.map({ it.length }).sum() + val totalLength = names.all.map { it.length }.sum() names.reduceLeftTo({ it.length }, { sum, s -> s.length + sum }) shouldBe totalLength - names.reduceMap(this, { it.length }) shouldBe totalLength + names.reduceMap(this) { it.length } shouldBe totalLength } } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/StateTests.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/StateTests.kt index 22c13f4c6a1..afac2dcf58e 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/StateTests.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/StateTests.kt @@ -22,7 +22,7 @@ class StateTests : UnitSpec() { } "addOne.map(n -> n).run(1) should return same Pair(2, 1)" { - addOne.map(Id.functor(), { n -> n }).run(1) shouldBe Tuple2(2, 1) + addOne.map(Id.functor()) { n -> n }.run(1) shouldBe Tuple2(2, 1) } "addOne.map(n -> n.toString).run(1) should return same Pair(2, \"1\")" { diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/TryTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/TryTest.kt index 73563862f0f..99a0af63046 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/TryTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/TryTest.kt @@ -146,8 +146,8 @@ class TryTest : UnitSpec() { map( Success("11th"), Success("Doctor"), - Success("Who"), - { (a, b, c) -> "$a $b $c" }) shouldBe Success("11th Doctor Who") + Success("Who") + ) { (a, b, c) -> "$a $b $c" } shouldBe Success("11th Doctor Who") } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/data/ValidatedTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/data/ValidatedTest.kt index 84f6f3b3e78..b71507d3f86 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/data/ValidatedTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/data/ValidatedTest.kt @@ -131,9 +131,9 @@ class ValidatedTest : UnitSpec() { val plusIntSemigroup: Semigroup = Int.semigroup() "findValid should return the first Valid value or combine or Invalid values in otherwise" { - Valid(10).findValid(plusIntSemigroup, { fail("None should not be called") }) shouldBe Valid(10) - Invalid(10).findValid(plusIntSemigroup, { Valid(5) }) shouldBe Valid(5) - Invalid(10).findValid(plusIntSemigroup, { Invalid(5) }) shouldBe Invalid(15) + Valid(10).findValid(plusIntSemigroup) { fail("None should not be called") } shouldBe Valid(10) + Invalid(10).findValid(plusIntSemigroup) { Valid(5) } shouldBe Valid(5) + Invalid(10).findValid(plusIntSemigroup) { Invalid(5) } shouldBe Invalid(15) } "ap should return Valid(f(a)) if both are Valid" { @@ -183,24 +183,24 @@ class ValidatedTest : UnitSpec() { map( Valid("11th"), Valid("Doctor"), - Valid("Who"), - { (a, b, c) -> "$a $b $c" }) shouldBe Valid("11th Doctor Who") + Valid("Who") + ) { (a, b, c) -> "$a $b $c" } shouldBe Valid("11th Doctor Who") } "Cartesian builder should build products over heterogeneous Validated" { map( Valid(13), Valid("Doctor"), - Valid(false), - { (a, b, c) -> "${a}th $b is $c" }) shouldBe Valid("13th Doctor is false") + Valid(false) + ) { (a, b, c) -> "${a}th $b is $c" } shouldBe Valid("13th Doctor is false") } "Cartesian builder should build products over Invalid Validated" { map( Invalid("fail1"), Invalid("fail2"), - Valid("Who"), - { "success!" }) shouldBe Invalid("fail1fail2") + Valid("Who") + ) { "success!" } shouldBe Invalid("fail1fail2") } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/free/CofreeTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/free/CofreeTest.kt index e3629477dd0..b96622af227 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/free/CofreeTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/free/CofreeTest.kt @@ -22,10 +22,10 @@ class CofreeTest : UnitSpec() { ForCofree() extensions { testLaws(ComonadLaws.laws(this, { val sideEffect = SideEffect() - unfold(Option.functor(), sideEffect.counter, { + unfold(Option.functor(), sideEffect.counter) { sideEffect.increment() if (it % 2 == 0) None else Some(it + 1) - }) + } }, Eq { a, b -> a.fix().run().fix() == b.fix().run().fix() })) @@ -33,7 +33,7 @@ class CofreeTest : UnitSpec() { "tailForced should evaluate and return" { val sideEffect = SideEffect() - val start: Cofree = unfold(Id.functor(), sideEffect.counter, { sideEffect.increment(); Id(it + 1) }) + val start: Cofree = unfold(Id.functor(), sideEffect.counter) { sideEffect.increment(); Id(it + 1) } sideEffect.counter shouldBe 0 start.tailForced() sideEffect.counter shouldBe 1 @@ -41,7 +41,7 @@ class CofreeTest : UnitSpec() { "runTail should run once and return" { val sideEffect = SideEffect() - val start: Cofree = unfold(Id.functor(), sideEffect.counter, { sideEffect.increment(); Id(it) }) + val start: Cofree = unfold(Id.functor(), sideEffect.counter) { sideEffect.increment(); Id(it) } sideEffect.counter shouldBe 0 start.runTail() sideEffect.counter shouldBe 1 @@ -49,10 +49,10 @@ class CofreeTest : UnitSpec() { "run should fold until completion" { val sideEffect = SideEffect() - val start: Cofree = unfold(Option.functor(), sideEffect.counter, { + val start: Cofree = unfold(Option.functor(), sideEffect.counter) { sideEffect.increment() if (it == 5) None else Some(it + 1) - }) + } sideEffect.counter shouldBe 0 start.run() sideEffect.counter shouldBe 6 @@ -63,10 +63,10 @@ class CofreeTest : UnitSpec() { try { val limit = 10000 val counter = SideEffect() - val startThousands: Cofree = unfold(Option.functor(), counter.counter, { + val startThousands: Cofree = unfold(Option.functor(), counter.counter) { counter.increment() if (it == limit) None else Some(it + 1) - }) + } startThousands.run() throw AssertionError("Run should overflow on a stack-unsafe monad") } catch (e: StackOverflowError) { @@ -76,15 +76,15 @@ class CofreeTest : UnitSpec() { "run with an stack-safe monad should not blow up the stack" { val counter = SideEffect() - val startThousands: Cofree = unfold(Eval.functor(), counter.counter, { + val startThousands: Cofree = unfold(Eval.functor(), counter.counter) { counter.increment() Eval.now(it + 1) - }) + } startThousands.run() counter.counter shouldBe 1 } - val startHundred: Cofree = unfold(Option.functor(), 0, { if (it == 100) None else Some(it + 1) }) + val startHundred: Cofree = unfold(Option.functor(), 0) { if (it == 100) None else Some(it + 1) } "mapBranchingRoot should modify the value of the functor" { val mapped = startHundred.mapBranchingRoot(object : FunctionK { @@ -114,7 +114,7 @@ class CofreeTest : UnitSpec() { cata shouldBe expected } - val startTwoThousand: Cofree = unfold(Option.functor(), 0, { if (it == 2000) None else Some(it + 1) }) + val startTwoThousand: Cofree = unfold(Option.functor(), 0) { if (it == 2000) None else Some(it + 1) } with(startTwoThousand) { "cata with an stack-unsafe monad should blow up the stack" { diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/free/CoyonedaTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/free/CoyonedaTest.kt index a177a379274..10c68af32b2 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/free/CoyonedaTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/free/CoyonedaTest.kt @@ -19,7 +19,7 @@ class CoyonedaTest : UnitSpec() { init { ForCoyoneda() extensions { - testLaws(FunctorLaws.laws(this, { Coyoneda(Id(0), { it }) }, EQ)) + testLaws(FunctorLaws.laws(this, { Coyoneda(Id(0)) { it } }, EQ)) } "map should be stack-safe" { diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeApplicativeTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeApplicativeTest.kt index ce95a82a551..e3facb069b2 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeApplicativeTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeApplicativeTest.kt @@ -44,7 +44,7 @@ class FreeApplicativeTest : UnitSpec() { ForFreeApplicative() extensions { testLaws( - EqLaws.laws(EQ, { OpsAp.value(it) }), + EqLaws.laws(EQ) { OpsAp.value(it) }, ApplicativeLaws.laws(OpsAp, EQ), ApplicativeLaws.laws(this, EQ) ) @@ -61,9 +61,9 @@ class FreeApplicativeTest : UnitSpec() { val loops = 10000 val start = 333 val r = FreeApplicative.liftF(NonEmptyList.of(start)) - val rr = (1..loops).toList().fold(r, { v, _ -> v.ap(FreeApplicative.liftF(NonEmptyList.of({ a: Int -> a + 1 }))) }) + val rr = (1..loops).toList().fold(r) { v, _ -> v.ap(FreeApplicative.liftF(NonEmptyList.of({ a: Int -> a + 1 }))) } rr.foldK(NonEmptyList.applicative()) shouldBe NonEmptyList.of(start + loops) - val rx = (1..loops).toList().foldRight(r, { _, v -> v.ap(FreeApplicative.liftF(NonEmptyList.of({ a: Int -> a + 1 }))) }) + val rx = (1..loops).toList().foldRight(r) { _, v -> v.ap(FreeApplicative.liftF(NonEmptyList.of({ a: Int -> a + 1 }))) } rx.foldK(NonEmptyList.applicative()) shouldBe NonEmptyList.of(start + loops) } } diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeTest.kt b/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeTest.kt index 268cc39a6d3..d3706a3b298 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeTest.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/free/FreeTest.kt @@ -54,7 +54,7 @@ class FreeTest : UnitSpec() { ForFree() extensions { testLaws( - EqLaws.laws(EQ, { Ops.value(it) }), + EqLaws.laws(EQ) { Ops.value(it) }, MonadLaws.laws(Ops, EQ), MonadLaws.laws(this, EQ) ) diff --git a/modules/core/arrow-data/src/test/kotlin/arrow/free/Interpreters.kt b/modules/core/arrow-data/src/test/kotlin/arrow/free/Interpreters.kt index 8987b32015c..80e381f6a3e 100644 --- a/modules/core/arrow-data/src/test/kotlin/arrow/free/Interpreters.kt +++ b/modules/core/arrow-data/src/test/kotlin/arrow/free/Interpreters.kt @@ -16,7 +16,7 @@ val cofreeListToNel: FunctionK, ForNonEmptyList> = obj override fun invoke(fa: Kind, A>): Kind = fa.fix().let { c: Cofree -> val all: List> = c.tailForced().fix() - val tail: List = all.foldRight(listOf(), { v, acc -> acc + invoke(v).fix().all }) + val tail: List = all.foldRight(listOf()) { v, acc -> acc + invoke(v).fix().all } val headL: List = listOf(c.head) NonEmptyList.fromListUnsafe(headL + tail) } diff --git a/modules/core/arrow-free/src/main/kotlin/arrow/free/Cofree.kt b/modules/core/arrow-free/src/main/kotlin/arrow/free/Cofree.kt index e9d930efb8b..5da08b99e1f 100644 --- a/modules/core/arrow-free/src/main/kotlin/arrow/free/Cofree.kt +++ b/modules/core/arrow-free/src/main/kotlin/arrow/free/Cofree.kt @@ -20,25 +20,25 @@ data class Cofree(val FS: Functor, val head: A, val tail: Eval transform(f: (A) -> B, noinline g: (Cofree) -> Cofree): Cofree = Cofree(FS, f(head), tail.map { it.map(g) }) - fun map(f: (A) -> B): Cofree = transform(f, { it.map(f) }) + fun map(f: (A) -> B): Cofree = transform(f) { it.map(f) } fun mapBranchingRoot(fk: FunctionK): Cofree = Cofree(FS, head, tail.map { fk(it) }) // Due to the recursive nature of this function, it cannot be reified for FT to use functor() - fun mapBranchingS(fk: FunctionK, FT: Functor): Cofree = Cofree(FT, head, tail.map { ce -> fk(ce.map({ it.mapBranchingS(fk, FT) })) }) + fun mapBranchingS(fk: FunctionK, FT: Functor): Cofree = Cofree(FT, head, tail.map { ce -> fk(ce.map { it.mapBranchingS(fk, FT) }) }) // Due to the recursive nature of this function, it cannot be reified for FT to use functor() fun mapBranchingT(fk: FunctionK, FT: Functor): Cofree = FT.run { - Cofree(this, head, tail.map { ce -> fk(ce).map({ it.mapBranchingT(fk, this) }) }) + Cofree(this, head, tail.map { ce -> fk(ce).map { it.mapBranchingT(fk, this) } }) } - fun coflatMap(f: (Cofree) -> B): Cofree = Cofree(FS, f(this), tail.map { it.map({ coflatMap(f) }) }) + fun coflatMap(f: (Cofree) -> B): Cofree = Cofree(FS, f(this), tail.map { it.map { coflatMap(f) } }) - fun duplicate(): Cofree> = Cofree(FS, this, tail.map { it.map({ duplicate() }) }) + fun duplicate(): Cofree> = Cofree(FS, this, tail.map { it.map { duplicate() } }) fun runTail(): Cofree = Cofree(FS, head, Eval.now(tail.value())) - fun run(): Cofree = Cofree(FS, head, Eval.now(tail.map { it.map({ it.run() }) }.value())) + fun run(): Cofree = Cofree(FS, head, Eval.now(tail.map { it.map { it.run() } }.value())) fun extract(): A = head @@ -49,7 +49,7 @@ data class Cofree(val FS: Functor, val head: A, val tail: Eval Cofree.cataM(MM: Monad, TF: Traverse, inclusion: FunctionK, folder: (A, Kind) -> Kind): Kind = MM.run { fun loop(ev: Cofree): Eval> { - val looped: Kind> = TF.run { ev.tailForced().traverse(MM, { inclusion(Eval.defer { loop(it) }).flatten() }) } + val looped: Kind> = TF.run { ev.tailForced().traverse(MM) { inclusion(Eval.defer { loop(it) }).flatten() } } val folded: Kind = looped.flatMap { fb -> folder(ev.head, fb) } return Eval.now(folded) } @@ -60,7 +60,7 @@ data class Cofree(val FS: Functor, val head: A, val tail: Eval unfold(FS: Functor, a: A, f: (A) -> Kind): Cofree = create(FS, a, f) fun create(FS: Functor, a: A, f: (A) -> Kind): Cofree = FS.run { - Cofree(this, a, Eval.later { f(a).map({ create(this, it, f) }) }) + Cofree(this, a, Eval.later { f(a).map { create(this, it, f) } }) } } diff --git a/modules/core/arrow-free/src/main/kotlin/arrow/free/Free.kt b/modules/core/arrow-free/src/main/kotlin/arrow/free/Free.kt index b44daaab0a6..b7ec3c8af54 100644 --- a/modules/core/arrow-free/src/main/kotlin/arrow/free/Free.kt +++ b/modules/core/arrow-free/src/main/kotlin/arrow/free/Free.kt @@ -51,7 +51,7 @@ sealed class Free : FreeOf { data class FlatMapped(val c: Free, val fm: (C) -> Free) : Free() { override fun transform(f: (A) -> B, fs: FunctionK): Free = - FlatMapped(c.transform(::identity, fs), { c.flatMap { fm(it) }.transform(f, fs) }) + FlatMapped(c.transform(::identity, fs)) { c.flatMap { fm(it) }.transform(f, fs) } } override fun toString(): String = "Free(...) : toString is not stack-safe" diff --git a/modules/core/arrow-generic/src/test/kotlin/arrow/generic/ProductTest.kt b/modules/core/arrow-generic/src/test/kotlin/arrow/generic/ProductTest.kt index 1525e8d7350..ea0067a3ff1 100644 --- a/modules/core/arrow-generic/src/test/kotlin/arrow/generic/ProductTest.kt +++ b/modules/core/arrow-generic/src/test/kotlin/arrow/generic/ProductTest.kt @@ -38,9 +38,9 @@ inline fun Gen.generateIn(applicative: Applicative): Gen Applicative.testPersonApplicative() { - forAll(Gen.string(), Gen.int(), personGen(), { a, b, c -> + forAll(Gen.string(), Gen.int(), personGen()) { a, b, c -> mapToPerson(just(a), just(b), just(c.some())) == just(Person(a, b, c.some())) - }) + } } @RunWith(KTestJUnitRunner::class) @@ -48,31 +48,31 @@ class ProductTest : UnitSpec() { init { ".tupled()" { - forAll(personGen(), { + forAll(personGen()) { it.tupled() == Tuple3(it.name, it.age, it.related) - }) + } } ".toPerson()" { - forAll(tuple3Gen(), { + forAll(tuple3Gen()) { it.toPerson() == Person(it.a, it.b, it.c) - }) + } } ".tupledLabeled()" { - forAll(personGen(), { + forAll(personGen()) { it.tupledLabeled() == Tuple3( "name" toT it.name, "age" toT it.age, "related" toT it.related ) - }) + } } "List<@product>.combineAll()" { - forAll(Gen.list(personGen()), { + forAll(Gen.list(personGen())) { it.combineAll() == it.reduce { a, b -> a + b } - }) + } } @@ -99,7 +99,7 @@ class ProductTest : UnitSpec() { } "Semigroup combine" { - forAll(personGen(), personGen(), { a, b -> + forAll(personGen(), personGen()) { a, b -> with(Person.semigroup()) { a.combine(b) == Person( a.name + b.name, @@ -107,34 +107,34 @@ class ProductTest : UnitSpec() { Option.monoid(this).combineAll(a.related, b.related) ) } - }) + } } "Semigroup + syntax" { - forAll(personGen(), personGen(), { a, b -> + forAll(personGen(), personGen()) { a, b -> a + b == Person( a.name + b.name, a.age + b.age, Option.monoid(Person.monoid()).combineAll(a.related, b.related) ) - }) + } } "Monoid empty" { - forAll(personGen(), personGen(), { a, b -> + forAll(personGen(), personGen()) { a, b -> Person.monoid().empty() == Person("", 0, None) - }) + } } "Monoid empty syntax" { - forAll(personGen(), personGen(), { a, b -> + forAll(personGen(), personGen()) { a, b -> emptyPerson() == Person("", 0, None) - }) + } } with(Gen) { testLaws( - EqLaws.laws(Person.eq(), { personGen().generate().copy(age = it) }), + EqLaws.laws(Person.eq()) { personGen().generate().copy(age = it) }, SemigroupLaws.laws( Person.semigroup(), personGen().generate(), diff --git a/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/option.kt b/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/option.kt index f19116eee41..9d3c0e74ad5 100644 --- a/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/option.kt +++ b/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/option.kt @@ -32,7 +32,7 @@ interface OptionApplicativeErrorInstance : OptionApplicativeInstance, Applicativ None override fun Kind.handleErrorWith(f: (Unit) -> Kind): Option = - fix().orElse({ f(Unit).fix() }) + fix().orElse { f(Unit).fix() } } @instance(Option::class) @@ -41,7 +41,7 @@ interface OptionMonadErrorInstance : OptionMonadInstance, MonadError Kind.handleErrorWith(f: (Unit) -> Kind): Option = - fix().orElse({ f(Unit).fix() }) + fix().orElse { f(Unit).fix() } } @instance(Option::class) diff --git a/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/try.kt b/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/try.kt index 41282535291..9bbce998799 100644 --- a/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/try.kt +++ b/modules/core/arrow-instances-core/src/main/kotlin/arrow/instances/try.kt @@ -128,7 +128,7 @@ interface TryFoldableInstance : Foldable { } fun TryOf.traverse(GA: Applicative, f: (A) -> Kind): Kind> = GA.run { - fix().fold({ just(Try.raise(it)) }, { f(it).map({ Try.just(it) }) }) + fix().fold({ just(Try.raise(it)) }, { f(it).map { Try.just(it) } }) } fun TryOf>.sequence(GA: Applicative): Kind> = diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/eithert.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/eithert.kt index 555122c14e2..4eac209419e 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/eithert.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/eithert.kt @@ -12,7 +12,7 @@ interface EitherTFunctorInstance : Functor> { fun FF(): Functor - override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(FF(), { f(it) }) + override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(FF()) { f(it) } } interface EitherTApplicativeInstance : EitherTFunctorInstance, Applicative> { @@ -21,7 +21,7 @@ interface EitherTApplicativeInstance : EitherTFunctorInstance, Appli override fun just(a: A): EitherT = EitherT.just(MF(), a) - override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(MF(), { f(it) }) + override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(MF()) { f(it) } override fun Kind, A>.ap(ff: Kind, (A) -> B>): EitherT = fix().ap(MF(), ff) @@ -29,12 +29,12 @@ interface EitherTApplicativeInstance : EitherTFunctorInstance, Appli interface EitherTMonadInstance : EitherTApplicativeInstance, Monad> { - override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(MF(), { f(it) }) + override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(MF()) { f(it) } override fun Kind, A>.ap(ff: Kind, (A) -> B>): EitherT = fix().ap(MF(), ff) - override fun Kind, A>.flatMap(f: (A) -> Kind, B>): EitherT = fix().flatMap(MF(), { f(it).fix() }) + override fun Kind, A>.flatMap(f: (A) -> Kind, B>): EitherT = fix().flatMap(MF()) { f(it).fix() } override fun tailRecM(a: A, f: (A) -> EitherTOf>): EitherT = EitherT.tailRecM(MF(), a, f) @@ -70,7 +70,7 @@ interface EitherTTraverseInstance : EitherTFunctorInstance, EitherTF fun TF(): Traverse - override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(TF(), { f(it) }) + override fun Kind, A>.map(f: (A) -> B): EitherT = fix().map(TF()) { f(it) } override fun Kind, B>.traverse(AP: Applicative, f: (B) -> Kind): Kind> = fix().traverse(TF(), AP, f) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/ior.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/ior.kt index db7cc9585dc..13f17066923 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/ior.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/ior.kt @@ -38,7 +38,7 @@ interface IorMonadInstance : IorApplicativeInstance, Monad override fun Kind, A>.map(f: (A) -> B): Ior = fix().map(f) override fun Kind, A>.flatMap(f: (A) -> Kind, B>): Ior = - fix().flatMap(SL(), { f(it).fix() }) + fix().flatMap(SL()) { f(it).fix() } override fun Kind, A>.ap(ff: Kind, (A) -> B>): Ior = fix().ap(SL(), ff) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/kleisli.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/kleisli.kt index e2682b9110f..26d6217cfc7 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/kleisli.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/kleisli.kt @@ -19,7 +19,7 @@ interface KleisliApplicativeInstance : KleisliFunctorInstance, Appli override fun FF(): Applicative - override fun just(a: A): Kleisli = Kleisli({ FF().just(a) }) + override fun just(a: A): Kleisli = Kleisli { FF().just(a) } override fun Kind, A>.map(f: (A) -> B): Kleisli = fix().map(FF(), f) @@ -28,7 +28,7 @@ interface KleisliApplicativeInstance : KleisliFunctorInstance, Appli fix().ap(FF(), ff) override fun Kind, A>.product(fb: Kind, B>): Kleisli> = - Kleisli({ FF().run { fix().run(it).product(fb.fix().run(it)) } }) + Kleisli { FF().run { fix().run(it).product(fb.fix().run(it)) } } } @instance(Kleisli::class) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/mapk.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/mapk.kt index 2294d00bef1..9db7a807642 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/mapk.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/mapk.kt @@ -33,8 +33,8 @@ interface MapKSemigroupInstance : Semigroup> { fun SG(): Semigroup override fun MapK.combine(b: MapK): MapK = with(SG()) { - if (fix().size < b.fix().size) fix().foldLeft(b.fix(), { my, (k, b) -> my.updated(k, b.maybeCombine(my[k])) }) - else b.fix().foldLeft(fix(), { my, (k, a) -> my.updated(k, a.maybeCombine(my[k])) }) + if (fix().size < b.fix().size) fix().foldLeft(b.fix()) { my, (k, b) -> my.updated(k, b.maybeCombine(my[k])) } + else b.fix().foldLeft(fix()) { my, (k, a) -> my.updated(k, a.maybeCombine(my[k])) } } } diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/optiont.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/optiont.kt index 79aacc673ac..f24bc679892 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/optiont.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/optiont.kt @@ -36,7 +36,7 @@ interface OptionTMonadInstance : OptionTApplicativeInstance, Monad Kind, A>.map(f: (A) -> B): OptionT = fix().map(FF(), f) - override fun Kind, A>.flatMap(f: (A) -> Kind, B>): OptionT = fix().flatMap(FF(), { f(it).fix() }) + override fun Kind, A>.flatMap(f: (A) -> Kind, B>): OptionT = fix().flatMap(FF()) { f(it).fix() } override fun Kind, A>.ap(ff: Kind, (A) -> B>): OptionT = fix().ap(FF(), ff) @@ -89,7 +89,7 @@ interface OptionTSemigroupKInstance : SemigroupK> { fun FF(): Monad - override fun Kind, A>.combineK(y: Kind, A>): OptionT = fix().orElse(FF(), { y.fix() }) + override fun Kind, A>.combineK(y: Kind, A>): OptionT = fix().orElse(FF()) { y.fix() } } @instance(OptionT::class) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/sortedmap.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/sortedmap.kt index a464c01cadf..819703e0ed1 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/sortedmap.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/sortedmap.kt @@ -32,10 +32,10 @@ interface SortedMapKSemigroupInstance, B> : Semigroup override fun SortedMapKOf.combine(b: SortedMapKOf): SortedMapKOf = - if (this.fix().size < b.fix().size) this.fix().foldLeft(b.fix(), { my, (k, b) -> + if (this.fix().size < b.fix().size) this.fix().foldLeft(b.fix()) { my, (k, b) -> my.updated(k, SG().run { b.maybeCombine(my[k]) }) - }) - else b.fix().foldLeft(this.fix(), { my: SortedMapK, (k, a) -> my.updated(k, SG().run { a.maybeCombine(my[k]) }) }) + } + else b.fix().foldLeft(this.fix()) { my: SortedMapK, (k, a) -> my.updated(k, SG().run { a.maybeCombine(my[k]) }) } } @instance(SortedMapK::class) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/statet.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/statet.kt index 7eb0573d491..9ed6dd09bae 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/statet.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/statet.kt @@ -48,7 +48,7 @@ interface StateTMonadInstance : StateTApplicativeInstance, Monad Kind, A>.ap(ff: Kind, (A) -> B>): StateT = - ff.fix().map2(FF(), fix(), { f, a -> f(a) }) + ff.fix().map2(FF(), fix()) { f, a -> f(a) } } @@ -71,7 +71,7 @@ interface StateTApplicativeErrorInstance : StateTApplicativeInstance raiseError(e: E): Kind, A> = StateT.lift(FF(), FF().raiseError(e)) override fun Kind, A>.handleErrorWith(f: (E) -> Kind, A>): StateT = - StateT(FF().just({ s -> FF().run { runM(FF(), s).handleErrorWith({ e -> f(e).runM(FF(), s) }) } })) + StateT(FF().just({ s -> FF().run { runM(FF(), s).handleErrorWith { e -> f(e).runM(FF(), s) } } })) } @instance(StateT::class) diff --git a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/writert.kt b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/writert.kt index 9c5c22972e2..7bc38fd7590 100644 --- a/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/writert.kt +++ b/modules/core/arrow-instances-data/src/main/kotlin/arrow/instances/writert.kt @@ -14,7 +14,7 @@ import arrow.typeclasses.* interface WriterTFunctorInstance : Functor> { fun FF(): Functor - override fun Kind, A>.map(f: (A) -> B): WriterT = fix().map(FF(), { f(it) }) + override fun Kind, A>.map(f: (A) -> B): WriterT = fix().map(FF()) { f(it) } } @instance(WriterT::class) @@ -31,17 +31,17 @@ interface WriterTApplicativeInstance : Applicative> fix().ap(FF(), MM(), ff) override fun Kind, A>.map(f: (A) -> B): WriterT = - fix().map(FF(), { f(it) }) + fix().map(FF()) { f(it) } } @instance(WriterT::class) interface WriterTMonadInstance : WriterTApplicativeInstance, Monad> { override fun Kind, A>.map(f: (A) -> B): WriterT = - fix().map(FF(), { f(it) }) + fix().map(FF()) { f(it) } override fun Kind, A>.flatMap(f: (A) -> Kind, B>): WriterT = - fix().flatMap(FF(), MM(), { f(it).fix() }) + fix().flatMap(FF(), MM()) { f(it).fix() } override fun tailRecM(a: A, f: (A) -> Kind, Either>): WriterT = WriterT.tailRecM(FF(), a, f) diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/composed.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/composed.kt index d782a44d521..65df210c91f 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/composed.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/composed.kt @@ -13,7 +13,7 @@ interface ComposedFunctorFilter : FunctorFilter>, ComposedFun override fun G(): FunctorFilter override fun Kind, A>.mapFilter(f: (A) -> Option): Kind, B> = - F().run { unnest().map({ G().run { it.mapFilter(f) } }).nest() } + F().run { unnest().map { G().run { it.mapFilter(f) } }.nest() } fun mapFilterC(fga: Kind>, f: (A) -> Option): Kind> = fga.nest().mapFilter(f).unnest() @@ -39,7 +39,7 @@ interface ComposedTraverseFilter : override fun GA(): Applicative override fun Kind, A>.traverseFilter(AP: Applicative, f: (A) -> Kind>): Kind, B>> = AP.run { - FT().run { unnest().traverse(AP, { ga -> GT().run { ga.traverseFilter(AP, f) } }) }.map({ it.nest() }) + FT().run { unnest().traverse(AP) { ga -> GT().run { ga.traverseFilter(AP, f) } } }.map { it.nest() } } fun traverseFilterC(fa: Kind>, f: (A) -> Kind>, HA: Applicative): Kind, B>> = diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/kleisli.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/kleisli.kt index 327cb69a370..8bd698585ec 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/kleisli.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/kleisli.kt @@ -13,7 +13,7 @@ import arrow.typeclasses.MonadError @instance(Kleisli::class) interface KleisliMonadReaderInstance : KleisliMonadInstance, MonadReader, D> { - override fun ask(): Kleisli = Kleisli({ FF().just(it) }) + override fun ask(): Kleisli = Kleisli { FF().just(it) } override fun Kind, A>.local(f: (D) -> D): Kleisli = fix().local(f) diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/statet.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/statet.kt index cb78bd9c987..0c3c7f42baf 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/statet.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/statet.kt @@ -35,7 +35,7 @@ interface StateTMonadCombineInstance : MonadCombine> override fun empty(): Kind, A> = liftT(MC().empty()) fun liftT(ma: Kind): StateT = FF().run { - StateT(just({ s: S -> ma.map({ a: A -> s toT a }) })) + StateT(just({ s: S -> ma.map { a: A -> s toT a } })) } } diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/writert.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/writert.kt index 7bc1c71b632..25b0f8f477f 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/writert.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/instances/writert.kt @@ -24,11 +24,11 @@ interface WriterTMonadFilterInstance : WriterTMonadInstance, MonadFi interface WriterTMonadWriterInstance : MonadWriter, W>, WriterTMonadInstance { override fun Kind, A>.listen(): Kind, Tuple2> = FF().run { - WriterT(fix().content(this).flatMap({ a -> fix().write(this).map({ l -> Tuple2(l, Tuple2(l, a)) }) })) + WriterT(fix().content(this).flatMap { a -> fix().write(this).map { l -> Tuple2(l, Tuple2(l, a)) } }) } override fun Kind, Tuple2<(W) -> W, A>>.pass(): WriterT = FF().run { - WriterT(fix().content(this).flatMap({ tuple2FA -> fix().write(this).map({ l -> Tuple2(tuple2FA.a(l), tuple2FA.b) }) })) + WriterT(fix().content(this).flatMap { tuple2FA -> fix().write(this).map { l -> Tuple2(tuple2FA.a(l), tuple2FA.b) } }) } override fun writer(aw: Tuple2): WriterT = WriterT.put2(FF(), aw.b, aw.a) diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/FunctorFilter.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/FunctorFilter.kt index cecc58bb0d5..a811041f4fe 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/FunctorFilter.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/FunctorFilter.kt @@ -27,5 +27,5 @@ interface FunctorFilter : Functor { * that don't. */ fun Kind.filter(f: (A) -> Boolean): Kind = - mapFilter({ a -> if (f(a)) Some(a) else None }) + mapFilter { a -> if (f(a)) Some(a) else None } } diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadCombine.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadCombine.kt index e041eb546b0..297436f4f1b 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadCombine.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadCombine.kt @@ -13,12 +13,12 @@ import arrow.typeclasses.Foldable interface MonadCombine : MonadFilter, Alternative { fun Kind>.unite(FG: Foldable): Kind = FG.run { - flatMap({ ga -> ga.foldLeft(empty(), { acc, a -> acc.combineK(just(a)) }) }) + flatMap { ga -> ga.foldLeft(empty()) { acc, a -> acc.combineK(just(a)) } } } fun Kind>.separate(BFG: Bifoldable): Tuple2, Kind> = BFG.run { - val asep = flatMap({ gab -> run { gab.bifoldMap(algebra(), { just(it) }, { _ -> empty() }) } }) - val bsep = flatMap({ gab -> run { gab.bifoldMap(algebra(), { _ -> empty() }, { just(it) }) } }) + val asep = flatMap { gab -> run { gab.bifoldMap(algebra(), { just(it) }, { _ -> empty() }) } } + val bsep = flatMap { gab -> run { gab.bifoldMap(algebra(), { _ -> empty() }, { just(it) }) } } return Tuple2(asep, bsep) } } diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadFilter.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadFilter.kt index f6961ccd3d0..ff76ba76764 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadFilter.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadFilter.kt @@ -10,7 +10,7 @@ interface MonadFilter : Monad, FunctorFilter { fun empty(): Kind override fun Kind.mapFilter(f: (A) -> Option): Kind = - this.flatMap({ a -> f(a).fold({ empty() }, { just(it) }) }) + this.flatMap { a -> f(a).fold({ empty() }, { just(it) }) } } /** diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadState.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadState.kt index 03fe7282036..22e0949ad7b 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadState.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/MonadState.kt @@ -6,13 +6,13 @@ import arrow.typeclasses.Monad interface MonadState : Monad { - fun state(f: (S) -> Tuple2): Kind = get().flatMap({ s -> f(s).let { (a, b) -> set(a).map({ b }) } }) + fun state(f: (S) -> Tuple2): Kind = get().flatMap { s -> f(s).let { (a, b) -> set(a).map { b } } } fun get(): Kind fun set(s: S): Kind - fun modify(f: (S) -> S): Kind = get().flatMap({ s -> set(f(s)) }) + fun modify(f: (S) -> S): Kind = get().flatMap { s -> set(f(s)) } fun inspect(f: (S) -> A): Kind = get().map(f) } \ No newline at end of file diff --git a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/TraverseFilter.kt b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/TraverseFilter.kt index 83cc273784a..9ef4924330a 100644 --- a/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/TraverseFilter.kt +++ b/modules/core/arrow-mtl/src/main/kotlin/arrow/mtl/typeclasses/TraverseFilter.kt @@ -10,10 +10,10 @@ interface TraverseFilter : Traverse, FunctorFilter { fun Kind.traverseFilter(AP: Applicative, f: (A) -> Kind>): Kind> override fun Kind.mapFilter(f: (A) -> Option): Kind = - traverseFilter(Id.applicative(), { Id(f(it)) }).value() + traverseFilter(Id.applicative()) { Id(f(it)) }.value() fun Kind.filterA(f: (A) -> Kind, GA: Applicative): Kind> = GA.run { - traverseFilter(this, { a -> f(a).map({ b -> if (b) Some(a) else None }) }) + traverseFilter(this) { a -> f(a).map { b -> if (b) Some(a) else None } } } override fun Kind.filter(f: (A) -> Boolean): Kind = diff --git a/modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/collections/iterable.kt b/modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/collections/iterable.kt index e726be3a92e..34dc4ec7829 100644 --- a/modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/collections/iterable.kt +++ b/modules/core/arrow-syntax/src/main/kotlin/arrow/syntax/collections/iterable.kt @@ -8,7 +8,7 @@ fun Iterable.firstOption(predicate: Predicate): Option = firstOrNul fun Iterable.collect(vararg cases: PartialFunction): List = flatMap { value: A -> - val f: PartialFunction = cases.reduce({ a, b -> a.orElse(b) }) + val f: PartialFunction = cases.reduce { a, b -> a.orElse(b) } if (f.isDefinedAt(value)) listOf(f(value)) else emptyList() } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AlternativeLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AlternativeLaws.kt index 2ec3a71557c..46af9230733 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AlternativeLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AlternativeLaws.kt @@ -16,26 +16,26 @@ object AlternativeLaws { noinline cff: (Int) -> Kind Int>, EQ: Eq>): List = ApplicativeLaws.laws(AF, EQ) + MonoidKLaws.laws(AF, AF, EQ) + listOf( - Law("Alternative Laws: Right Absorption", { AF.alternativeRightAbsorption(cff, EQ) }), - Law("Alternative Laws: Left Distributivity", { AF.alternativeLeftDistributivity(cf, EQ) }), - Law("Alternative Laws: Right Distributivity", { AF.alternativeRightDistributivity(cf, cff, EQ) })) + Law("Alternative Laws: Right Absorption") { AF.alternativeRightAbsorption(cff, EQ) }, + Law("Alternative Laws: Left Distributivity") { AF.alternativeLeftDistributivity(cf, EQ) }, + Law("Alternative Laws: Right Distributivity") { AF.alternativeRightDistributivity(cf, cff, EQ) }) fun Alternative.alternativeRightAbsorption(cff: (Int) -> Kind Int>, EQ: Eq>): Unit = - forAll(genConstructor2(Gen.int(), cff), { fa: Kind Int> -> + forAll(genConstructor2(Gen.int(), cff)) { fa: Kind Int> -> empty().ap(fa).equalUnderTheLaw(empty(), EQ) - }) + } fun Alternative.alternativeLeftDistributivity(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genConstructor(Gen.int(), cf), genFunctionAToB(Gen.int()), - { fa: Kind, fa2: Kind, f: (Int) -> Int -> - fa.combineK(fa2).map(f).equalUnderTheLaw(fa.map(f).combineK(fa2.map(f)), EQ) - }) + forAll(genConstructor(Gen.int(), cf), genConstructor(Gen.int(), cf), genFunctionAToB(Gen.int()) + ) { fa: Kind, fa2: Kind, f: (Int) -> Int -> + fa.combineK(fa2).map(f).equalUnderTheLaw(fa.map(f).combineK(fa2.map(f)), EQ) + } fun Alternative.alternativeRightDistributivity(cf: (Int) -> Kind, cff: (Int) -> Kind Int>, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genConstructor2(Gen.int(), cff), genConstructor2(Gen.int(), cff), - { fa: Kind, ff: Kind Int>, fg: Kind Int> -> - fa.ap(ff.combineK(fg)).equalUnderTheLaw(fa.ap(ff).combineK(fa.ap(fg)), EQ) - }) + forAll(genConstructor(Gen.int(), cf), genConstructor2(Gen.int(), cff), genConstructor2(Gen.int(), cff) + ) { fa: Kind, ff: Kind Int>, fg: Kind Int> -> + fa.ap(ff.combineK(fg)).equalUnderTheLaw(fa.ap(ff).combineK(fa.ap(fg)), EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeErrorLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeErrorLaws.kt index c4fb46e1807..d7e229ba519 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeErrorLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeErrorLaws.kt @@ -18,47 +18,47 @@ object ApplicativeErrorLaws { inline fun laws(AE: ApplicativeError, EQERR: Eq>, EQ_EITHER: Eq>>, EQ: Eq> = EQERR): List = ApplicativeLaws.laws(AE, EQ) + listOf( - Law("Applicative Error Laws: handle", { AE.applicativeErrorHandle(EQERR) }), - Law("Applicative Error Laws: handle with for error", { AE.applicativeErrorHandleWith(EQERR) }), - Law("Applicative Error Laws: handle with for success", { AE.applicativeErrorHandleWithPure(EQERR) }), - Law("Applicative Error Laws: attempt for error", { AE.applicativeErrorAttemptError(EQ_EITHER) }), - Law("Applicative Error Laws: attempt for success", { AE.applicativeErrorAttemptSuccess(EQ_EITHER) }), - Law("Applicative Error Laws: attempt fromEither consistent with pure", { AE.applicativeErrorAttemptFromEitherConsistentWithPure(EQ_EITHER) }), - Law("Applicative Error Laws: catch captures errors", { AE.applicativeErrorCatch(EQERR) }) + Law("Applicative Error Laws: handle") { AE.applicativeErrorHandle(EQERR) }, + Law("Applicative Error Laws: handle with for error") { AE.applicativeErrorHandleWith(EQERR) }, + Law("Applicative Error Laws: handle with for success") { AE.applicativeErrorHandleWithPure(EQERR) }, + Law("Applicative Error Laws: attempt for error") { AE.applicativeErrorAttemptError(EQ_EITHER) }, + Law("Applicative Error Laws: attempt for success") { AE.applicativeErrorAttemptSuccess(EQ_EITHER) }, + Law("Applicative Error Laws: attempt fromEither consistent with pure") { AE.applicativeErrorAttemptFromEitherConsistentWithPure(EQ_EITHER) }, + Law("Applicative Error Laws: catch captures errors") { AE.applicativeErrorCatch(EQERR) } ) fun ApplicativeError.applicativeErrorHandle(EQ: Eq>): Unit = - forAll(genFunctionAToB(Gen.int()), genThrowable(), { f: (Throwable) -> Int, e: Throwable -> + forAll(genFunctionAToB(Gen.int()), genThrowable()) { f: (Throwable) -> Int, e: Throwable -> raiseError(e).handleError(f).equalUnderTheLaw(just(f(e)), EQ) - }) + } fun ApplicativeError.applicativeErrorHandleWith(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), genThrowable(), { f: (Throwable) -> Kind, e: Throwable -> + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), genThrowable()) { f: (Throwable) -> Kind, e: Throwable -> raiseError(e).handleErrorWith(f).equalUnderTheLaw(f(e), EQ) - }) + } fun ApplicativeError.applicativeErrorHandleWithPure(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int(), { f: (Throwable) -> Kind, a: Int -> + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int()) { f: (Throwable) -> Kind, a: Int -> just(a).handleErrorWith(f).equalUnderTheLaw(just(a), EQ) - }) + } fun ApplicativeError.applicativeErrorAttemptError(EQ: Eq>>): Unit = - forAll(genThrowable(), { e: Throwable -> + forAll(genThrowable()) { e: Throwable -> raiseError(e).attempt().equalUnderTheLaw(just(Left(e)), EQ) - }) + } fun ApplicativeError.applicativeErrorAttemptSuccess(EQ: Eq>>): Unit = - forAll(Gen.int(), { a: Int -> + forAll(Gen.int()) { a: Int -> just(a).attempt().equalUnderTheLaw(just(Right(a)), EQ) - }) + } fun ApplicativeError.applicativeErrorAttemptFromEitherConsistentWithPure(EQ: Eq>>): Unit = - forAll(genEither(genThrowable(), Gen.int()), { either: Either -> + forAll(genEither(genThrowable(), Gen.int())) { either: Either -> fromEither(either).attempt().equalUnderTheLaw(just(either), EQ) - }) + } fun ApplicativeError.applicativeErrorCatch(EQ: Eq>): Unit = - forAll(genEither(genThrowable(), Gen.int()), { either: Either -> - catch({ either.fold({ throw it }, ::identity) }).equalUnderTheLaw(either.fold({ raiseError(it) }, { just(it) }), EQ) - }) + forAll(genEither(genThrowable(), Gen.int())) { either: Either -> + catch { either.fold({ throw it }, ::identity) }.equalUnderTheLaw(either.fold({ raiseError(it) }, { just(it) }), EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeLaws.kt index 2f785906642..0309beb328f 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ApplicativeLaws.kt @@ -13,41 +13,41 @@ object ApplicativeLaws { inline fun laws(A: Applicative, EQ: Eq>): List = FunctorLaws.laws(A, EQ) + listOf( - Law("Applicative Laws: ap identity", { A.apIdentity(EQ) }), - Law("Applicative Laws: homomorphism", { A.homomorphism(EQ) }), - Law("Applicative Laws: interchange", { A.interchange(EQ) }), - Law("Applicative Laws: map derived", { A.mapDerived(EQ) }), - Law("Applicative Laws: cartesian builder map", { A.cartesianBuilderMap(EQ) }), - Law("Applicative Laws: cartesian builder tupled", { A.cartesianBuilderTupled(EQ) }) + Law("Applicative Laws: ap identity") { A.apIdentity(EQ) }, + Law("Applicative Laws: homomorphism") { A.homomorphism(EQ) }, + Law("Applicative Laws: interchange") { A.interchange(EQ) }, + Law("Applicative Laws: map derived") { A.mapDerived(EQ) }, + Law("Applicative Laws: cartesian builder map") { A.cartesianBuilderMap(EQ) }, + Law("Applicative Laws: cartesian builder tupled") { A.cartesianBuilderTupled(EQ) } ) fun Applicative.apIdentity(EQ: Eq>): Unit = - forAll(genApplicative(Gen.int(), this), { fa: Kind -> + forAll(genApplicative(Gen.int(), this)) { fa: Kind -> fa.ap(just({ n: Int -> n })).equalUnderTheLaw(fa, EQ) - }) + } fun Applicative.homomorphism(EQ: Eq>): Unit = - forAll(genFunctionAToB(Gen.int()), Gen.int(), { ab: (Int) -> Int, a: Int -> + forAll(genFunctionAToB(Gen.int()), Gen.int()) { ab: (Int) -> Int, a: Int -> just(a).ap(just(ab)).equalUnderTheLaw(just(ab(a)), EQ) - }) + } fun Applicative.interchange(EQ: Eq>): Unit = - forAll(genApplicative(genFunctionAToB(Gen.int()), this), Gen.int(), { fa: Kind Int>, a: Int -> + forAll(genApplicative(genFunctionAToB(Gen.int()), this), Gen.int()) { fa: Kind Int>, a: Int -> just(a).ap(fa).equalUnderTheLaw(fa.ap(just({ x: (Int) -> Int -> x(a) })), EQ) - }) + } fun Applicative.mapDerived(EQ: Eq>): Unit = - forAll(genApplicative(Gen.int(), this), genFunctionAToB(Gen.int()), { fa: Kind, f: (Int) -> Int -> + forAll(genApplicative(Gen.int(), this), genFunctionAToB(Gen.int())) { fa: Kind, f: (Int) -> Int -> fa.map(f).equalUnderTheLaw(fa.ap(just(f)), EQ) - }) + } fun Applicative.cartesianBuilderMap(EQ: Eq>): Unit = - forAll(genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), { a: Int, b: Int, c: Int, d: Int, e: Int, f: Int -> - map(just(a), just(b), just(c), just(d), just(e), just(f), { (x, y, z, u, v, w) -> x + y + z - u - v - w }).equalUnderTheLaw(just(a + b + c - d - e - f), EQ) - }) + forAll(genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall()) { a: Int, b: Int, c: Int, d: Int, e: Int, f: Int -> + map(just(a), just(b), just(c), just(d), just(e), just(f)) { (x, y, z, u, v, w) -> x + y + z - u - v - w }.equalUnderTheLaw(just(a + b + c - d - e - f), EQ) + } fun Applicative.cartesianBuilderTupled(EQ: Eq>): Unit = - forAll(genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), { a: Int, b: Int, c: Int, d: Int, e: Int, f: Int -> - tupled(just(a), just(b), just(c), just(d), just(e), just(f)).map({ (x, y, z, u, v, w) -> x + y + z - u - v - w }).equalUnderTheLaw(just(a + b + c - d - e - f), EQ) - }) + forAll(genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall(), genIntSmall()) { a: Int, b: Int, c: Int, d: Int, e: Int, f: Int -> + tupled(just(a), just(b), just(c), just(d), just(e), just(f)).map { (x, y, z, u, v, w) -> x + y + z - u - v - w }.equalUnderTheLaw(just(a + b + c - d - e - f), EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AsyncLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AsyncLaws.kt index 7281d50a85a..21d795eec30 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AsyncLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/AsyncLaws.kt @@ -16,51 +16,51 @@ import kotlinx.coroutines.experimental.newSingleThreadContext object AsyncLaws { inline fun laws(AC: Async, EQ: Eq>, EQ_EITHER: Eq>>, EQERR: Eq> = EQ): List = MonadSuspendLaws.laws(AC, EQERR, EQ_EITHER, EQ) + listOf( - Law("Async Laws: success equivalence", { AC.asyncSuccess(EQ) }), - Law("Async Laws: error equivalence", { AC.asyncError(EQERR) }), - Law("Async Laws: continueOn jumps threads", { AC.continueOn(EQ) }), - Law("Async Laws: async constructor", { AC.asyncConstructor(EQ) }), - Law("Async Laws: continueOn on comprehensions", { AC.continueOnComprehension(EQ) }) + Law("Async Laws: success equivalence") { AC.asyncSuccess(EQ) }, + Law("Async Laws: error equivalence") { AC.asyncError(EQERR) }, + Law("Async Laws: continueOn jumps threads") { AC.continueOn(EQ) }, + Law("Async Laws: async constructor") { AC.asyncConstructor(EQ) }, + Law("Async Laws: continueOn on comprehensions") { AC.continueOnComprehension(EQ) } ) fun Async.asyncSuccess(EQ: Eq>): Unit = - forAll(Gen.int(), { num: Int -> + forAll(Gen.int()) { num: Int -> async { ff: (Either) -> Unit -> ff(Right(num)) }.equalUnderTheLaw(just(num), EQ) - }) + } fun Async.asyncError(EQ: Eq>): Unit = - forAll(genThrowable(), { e: Throwable -> + forAll(genThrowable()) { e: Throwable -> async { ff: (Either) -> Unit -> ff(Left(e)) }.equalUnderTheLaw(raiseError(e), EQ) - }) + } fun Async.continueOn(EQ: Eq>): Unit = - forFew(5, genIntSmall(), genIntSmall(), { threadId1: Int, threadId2: Int -> + forFew(5, genIntSmall(), genIntSmall()) { threadId1: Int, threadId2: Int -> Unit.just() .continueOn(newSingleThreadContext(threadId1.toString())) .map { getCurrentThread() } .continueOn(newSingleThreadContext(threadId2.toString())) .map { it + getCurrentThread() } .equalUnderTheLaw(just(threadId1 + threadId2), EQ) - }) + } fun Async.asyncConstructor(EQ: Eq>): Unit = - forFew(5, genIntSmall(), genIntSmall(), { threadId1: Int, threadId2: Int -> + forFew(5, genIntSmall(), genIntSmall()) { threadId1: Int, threadId2: Int -> invoke(newSingleThreadContext(threadId1.toString())) { getCurrentThread() } .flatMap { invoke(newSingleThreadContext(threadId2.toString())) { it + getCurrentThread() } } .equalUnderTheLaw(just(threadId1 + threadId2), EQ) - }) + } fun Async.continueOnComprehension(EQ: Eq>): Unit = - forFew(5, genIntSmall(), genIntSmall(), { threadId1: Int, threadId2: Int -> + forFew(5, genIntSmall(), genIntSmall()) { threadId1: Int, threadId2: Int -> binding { continueOn(newSingleThreadContext(threadId1.toString())) val t1: Int = getCurrentThread() continueOn(newSingleThreadContext(threadId2.toString())) t1 + getCurrentThread() }.equalUnderTheLaw(just(threadId1 + threadId2), EQ) - }) + } // Turns out that kotlinx.coroutines decides to rewrite thread names private fun getCurrentThread() = diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/BifoldableLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/BifoldableLaws.kt index 02153b28f10..e270794a567 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/BifoldableLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/BifoldableLaws.kt @@ -14,27 +14,27 @@ import io.kotlintest.properties.forAll object BifoldableLaws { inline fun laws(BF: Bifoldable, noinline cf: (Int) -> Kind2, EQ: Eq): List = listOf( - Law("Bifoldable Laws: Left bifold consistent with BifoldMap", { BF.bifoldLeftConsistentWithBifoldMap(cf, EQ) }), - Law("Bifoldable Laws: Right bifold consistent with BifoldMap", { BF.bifoldRightConsistentWithBifoldMap(cf, EQ) }) + Law("Bifoldable Laws: Left bifold consistent with BifoldMap") { BF.bifoldLeftConsistentWithBifoldMap(cf, EQ) }, + Law("Bifoldable Laws: Right bifold consistent with BifoldMap") { BF.bifoldRightConsistentWithBifoldMap(cf, EQ) } ) fun Bifoldable.bifoldLeftConsistentWithBifoldMap(cf: (Int) -> Kind2, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), - { f: (Int) -> Int, g: (Int) -> Int, fab: Kind2 -> - with(Int.monoid()) { - val expected = fab.bifoldLeft(Int.monoid().empty(), { c: Int, a: Int -> c.combine(f(a)) }, - { c: Int, b: Int -> c.combine(g(b)) }) - expected.equalUnderTheLaw(fab.bifoldMap(this, f, g), EQ) - } - }) + forAll(genFunctionAToB(genIntSmall()), genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf) + ) { f: (Int) -> Int, g: (Int) -> Int, fab: Kind2 -> + with(Int.monoid()) { + val expected = fab.bifoldLeft(Int.monoid().empty(), { c: Int, a: Int -> c.combine(f(a)) }, + { c: Int, b: Int -> c.combine(g(b)) }) + expected.equalUnderTheLaw(fab.bifoldMap(this, f, g), EQ) + } + } fun Bifoldable.bifoldRightConsistentWithBifoldMap(cf: (Int) -> Kind2, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), - { f: (Int) -> Int, g: (Int) -> Int, fab: Kind2 -> - with(Int.monoid()) { - val expected = fab.bifoldRight(Eval.Later({ Int.monoid().empty() }), { a: Int, ec: Eval -> ec.map({ c -> f(a).combine(c) }) }, - { b: Int, ec: Eval -> ec.map({ c -> g(b).combine(c) }) }) - expected.value().equalUnderTheLaw(fab.bifoldMap(this, f, g), EQ) - } - }) + forAll(genFunctionAToB(genIntSmall()), genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf) + ) { f: (Int) -> Int, g: (Int) -> Int, fab: Kind2 -> + with(Int.monoid()) { + val expected = fab.bifoldRight(Eval.Later { Int.monoid().empty() }, { a: Int, ec: Eval -> ec.map { c -> f(a).combine(c) } }, + { b: Int, ec: Eval -> ec.map { c -> g(b).combine(c) } }) + expected.value().equalUnderTheLaw(fab.bifoldMap(this, f, g), EQ) + } + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ComonadLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ComonadLaws.kt index eb391c86d78..654556c6382 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ComonadLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ComonadLaws.kt @@ -14,57 +14,57 @@ object ComonadLaws { inline fun laws(CM: Comonad, noinline cf: (Int) -> Kind, EQ: Eq>): List = FunctorLaws.laws(CM, cf, EQ) + listOf( - Law("Comonad Laws: duplicate then extract is identity", { CM.duplicateThenExtractIsId(cf, EQ) }), - Law("Comonad Laws: duplicate then map into extract is identity", { CM.duplicateThenMapExtractIsId(cf, EQ) }), - Law("Comonad Laws: map and coflatmap are coherent", { CM.mapAndCoflatmapCoherence(cf, EQ) }), - Law("Comonad Laws: left identity", { CM.comonadLeftIdentity(cf, EQ) }), - Law("Comonad Laws: right identity", { CM.comonadRightIdentity(cf, EQ) }), - Law("Comonad Laws: cokleisli left identity", { CM.cokleisliLeftIdentity(cf, EQ) }), - Law("Comonad Laws: cokleisli right identity", { CM.cokleisliRightIdentity(cf, EQ) }), - Law("Comonad Laws: cobinding", { CM.cobinding(cf, EQ) }) + Law("Comonad Laws: duplicate then extract is identity") { CM.duplicateThenExtractIsId(cf, EQ) }, + Law("Comonad Laws: duplicate then map into extract is identity") { CM.duplicateThenMapExtractIsId(cf, EQ) }, + Law("Comonad Laws: map and coflatmap are coherent") { CM.mapAndCoflatmapCoherence(cf, EQ) }, + Law("Comonad Laws: left identity") { CM.comonadLeftIdentity(cf, EQ) }, + Law("Comonad Laws: right identity") { CM.comonadRightIdentity(cf, EQ) }, + Law("Comonad Laws: cokleisli left identity") { CM.cokleisliLeftIdentity(cf, EQ) }, + Law("Comonad Laws: cokleisli right identity") { CM.cokleisliRightIdentity(cf, EQ) }, + Law("Comonad Laws: cobinding") { CM.cobinding(cf, EQ) } ) fun Comonad.duplicateThenExtractIsId(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> fa.duplicate().extract().equalUnderTheLaw(fa, EQ) - }) + } fun Comonad.duplicateThenMapExtractIsId(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> fa.duplicate().map() { it.extract() }.equalUnderTheLaw(fa, EQ) - }) + } fun Comonad.mapAndCoflatmapCoherence(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genFunctionAToB(Gen.int()), { fa: Kind, f: (Int) -> Int -> - fa.map(f).equalUnderTheLaw(fa.coflatMap({ f(it.extract()) }), EQ) - }) + forAll(genConstructor(Gen.int(), cf), genFunctionAToB(Gen.int())) { fa: Kind, f: (Int) -> Int -> + fa.map(f).equalUnderTheLaw(fa.coflatMap { f(it.extract()) }, EQ) + } fun Comonad.comonadLeftIdentity(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> - fa.coflatMap({ it.extract() }).equalUnderTheLaw(fa, EQ) - }) + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> + fa.coflatMap { it.extract() }.equalUnderTheLaw(fa, EQ) + } fun Comonad.comonadRightIdentity(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf)), { fa: Kind, f: (Kind) -> Kind -> + forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf))) { fa: Kind, f: (Kind) -> Kind -> fa.coflatMap(f).extract().equalUnderTheLaw(f(fa), EQ) - }) + } fun Comonad.cokleisliLeftIdentity(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf)), { fa: Kind, f: (Kind) -> Kind -> - Cokleisli(this, { hk: Kind -> hk.extract() }).andThen(Cokleisli(this, f)).run(fa).equalUnderTheLaw(f(fa), EQ) - }) + forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf))) { fa: Kind, f: (Kind) -> Kind -> + Cokleisli(this) { hk: Kind -> hk.extract() }.andThen(Cokleisli(this, f)).run(fa).equalUnderTheLaw(f(fa), EQ) + } fun Comonad.cokleisliRightIdentity(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf)), { fa: Kind, f: (Kind) -> Kind -> - Cokleisli(this, f).andThen(Cokleisli(this, { hk: Kind> -> hk.extract() })).run(fa).equalUnderTheLaw(f(fa), EQ) - }) + forAll(genConstructor(Gen.int(), cf), genFunctionAToB(genConstructor(Gen.int(), cf))) { fa: Kind, f: (Kind) -> Kind -> + Cokleisli(this, f).andThen(Cokleisli(this) { hk: Kind> -> hk.extract() }).run(fa).equalUnderTheLaw(f(fa), EQ) + } fun Comonad.cobinding(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> cobinding { val x = fa.extract() - val y = extract { fa.map({ it + x }) } - fa.map({ x + y }) - }.equalUnderTheLaw(fa.map({ it * 3 }), EQ) - }) + val y = extract { fa.map { it + x } } + fa.map { x + y } + }.equalUnderTheLaw(fa.map { it * 3 }, EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/EqLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/EqLaws.kt index 9a9d219f644..d567cd7b03c 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/EqLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/EqLaws.kt @@ -8,20 +8,20 @@ object EqLaws { inline fun laws(EQ: Eq, noinline cf: (Int) -> F): List = listOf( - Law("Eq Laws: identity", { EQ.identityEquality(cf) }), - Law("Eq Laws: commutativity", { EQ.commutativeEquality(cf) }) + Law("Eq Laws: identity") { EQ.identityEquality(cf) }, + Law("Eq Laws: commutativity") { EQ.commutativeEquality(cf) } ) fun Eq.identityEquality(cf: (Int) -> F): Unit = - forAll(Gen.int(), { int: Int -> + forAll(Gen.int()) { int: Int -> val a = cf(int) a.eqv(a) == a.eqv(a) - }) + } fun Eq.commutativeEquality(cf: (Int) -> F): Unit = - forAll(Gen.int(), { int: Int -> + forAll(Gen.int()) { int: Int -> val a = cf(int) val b = cf(int) a.eqv(b) == b.eqv(a) - }) + } } \ No newline at end of file diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FoldableLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FoldableLaws.kt index 866d3f046dd..9442195e4d7 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FoldableLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FoldableLaws.kt @@ -19,37 +19,37 @@ import io.kotlintest.properties.forAll object FoldableLaws { inline fun laws(FF: Foldable, noinline cf: (Int) -> Kind, EQ: Eq): List = listOf( - Law("Foldable Laws: Left fold consistent with foldMap", { FF.leftFoldConsistentWithFoldMap(cf, EQ) }), - Law("Foldable Laws: Right fold consistent with foldMap", { FF.rightFoldConsistentWithFoldMap(cf, EQ) }), - Law("Foldable Laws: Exists is consistent with find", { FF.existsConsistentWithFind(cf) }), - Law("Foldable Laws: Exists is lazy", { FF.existsIsLazy(cf, EQ) }), - Law("Foldable Laws: ForAll is lazy", { FF.forAllIsLazy(cf, EQ) }), - Law("Foldable Laws: ForAll consistent with exists", { FF.forallConsistentWithExists(cf) }), - Law("Foldable Laws: ForAll returns true if isEmpty", { FF.forallReturnsTrueIfEmpty(cf) }), - Law("Foldable Laws: FoldM for Id is equivalent to fold left", { FF.foldMIdIsFoldL(cf, EQ) }) + Law("Foldable Laws: Left fold consistent with foldMap") { FF.leftFoldConsistentWithFoldMap(cf, EQ) }, + Law("Foldable Laws: Right fold consistent with foldMap") { FF.rightFoldConsistentWithFoldMap(cf, EQ) }, + Law("Foldable Laws: Exists is consistent with find") { FF.existsConsistentWithFind(cf) }, + Law("Foldable Laws: Exists is lazy") { FF.existsIsLazy(cf, EQ) }, + Law("Foldable Laws: ForAll is lazy") { FF.forAllIsLazy(cf, EQ) }, + Law("Foldable Laws: ForAll consistent with exists") { FF.forallConsistentWithExists(cf) }, + Law("Foldable Laws: ForAll returns true if isEmpty") { FF.forallReturnsTrueIfEmpty(cf) }, + Law("Foldable Laws: FoldM for Id is equivalent to fold left") { FF.foldMIdIsFoldL(cf, EQ) } ) fun Foldable.leftFoldConsistentWithFoldMap(cf: (Int) -> Kind, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - fa.foldMap(this, f).equalUnderTheLaw(fa.foldLeft(empty(), { acc, a -> acc.combine(f(a)) }), EQ) + fa.foldMap(this, f).equalUnderTheLaw(fa.foldLeft(empty()) { acc, a -> acc.combine(f(a)) }, EQ) } - }) + } fun Foldable.rightFoldConsistentWithFoldMap(cf: (Int) -> Kind, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - fa.foldMap(this, f).equalUnderTheLaw(fa.foldRight(Eval.later { empty() }, { a, lb: Eval -> lb.map { f(a).combine(it) } }).value(), EQ) + fa.foldMap(this, f).equalUnderTheLaw(fa.foldRight(Eval.later { empty() }) { a, lb: Eval -> lb.map { f(a).combine(it) } }.value(), EQ) } - }) + } fun Foldable.existsConsistentWithFind(cf: (Int) -> Kind) = - forAll(genIntPredicate(), genConstructor(Gen.int(), cf), { f: (Int) -> Boolean, fa: Kind -> + forAll(genIntPredicate(), genConstructor(Gen.int(), cf)) { f: (Int) -> Boolean, fa: Kind -> fa.exists(f).equalUnderTheLaw(fa.find(f).fold({ false }, { true }), Eq.any()) - }) + } fun Foldable.existsIsLazy(cf: (Int) -> Kind, EQ: Eq) = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> val sideEffect = SideEffect() fa.exists { _ -> sideEffect.increment() @@ -57,10 +57,10 @@ object FoldableLaws { } val expected = if (fa.isEmpty()) 0 else 1 sideEffect.counter.equalUnderTheLaw(expected, EQ) - }) + } fun Foldable.forAllIsLazy(cf: (Int) -> Kind, EQ: Eq) = - forAll(genConstructor(Gen.int(), cf), { fa: Kind -> + forAll(genConstructor(Gen.int(), cf)) { fa: Kind -> val sideEffect = SideEffect() fa.forAll { _ -> sideEffect.increment() @@ -68,10 +68,10 @@ object FoldableLaws { } val expected = if (fa.isEmpty()) 0 else 1 sideEffect.counter.equalUnderTheLaw(expected, EQ) - }) + } fun Foldable.forallConsistentWithExists(cf: (Int) -> Kind) = - forAll(genIntPredicate(), genConstructor(Gen.int(), cf), { f: (Int) -> Boolean, fa: Kind -> + forAll(genIntPredicate(), genConstructor(Gen.int(), cf)) { f: (Int) -> Boolean, fa: Kind -> if (fa.forAll(f)) { val negationExists = fa.exists { a -> !(f(a)) } // if p is true for all elements, then there cannot be an element for which @@ -81,19 +81,19 @@ object FoldableLaws { // or there must exist an element for which it is true. (fa.isEmpty() || fa.exists(f)) } else true - }) + } fun Foldable.forallReturnsTrueIfEmpty(cf: (Int) -> Kind) = - forAll(genIntPredicate(), genConstructor(Gen.int(), cf), { f: (Int) -> Boolean, fa: Kind -> + forAll(genIntPredicate(), genConstructor(Gen.int(), cf)) { f: (Int) -> Boolean, fa: Kind -> !fa.isEmpty() || fa.forAll(f) - }) + } fun Foldable.foldMIdIsFoldL(cf: (Int) -> Kind, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - val foldL: Int = fa.foldLeft(empty(), { acc, a -> acc.combine(f(a)) }) - val foldM: Int = fa.foldM(Id.monad(), empty(), { acc, a -> Id(acc.combine(f(a))) }).value() + val foldL: Int = fa.foldLeft(empty()) { acc, a -> acc.combine(f(a)) } + val foldM: Int = fa.foldM(Id.monad(), empty()) { acc, a -> Id(acc.combine(f(a))) }.value() foldM.equalUnderTheLaw(foldL, EQ) } - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorFilterLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorFilterLaws.kt index b0f9bf3600f..0c3be10bf61 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorFilterLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorFilterLaws.kt @@ -16,24 +16,24 @@ object FunctorFilterLaws { inline fun laws(FFF: FunctorFilter, noinline cf: (Int) -> Kind, EQ: Eq>): List = FunctorLaws.laws(FFF, cf, EQ) + listOf( - Law("Functor Filter: mapFilter composition", { FFF.mapFilterComposition(cf, EQ) }), - Law("Functor Filter: mapFilter map consistency", { FFF.mapFilterMapConsistency(cf, EQ) }) + Law("Functor Filter: mapFilter composition") { FFF.mapFilterComposition(cf, EQ) }, + Law("Functor Filter: mapFilter map consistency") { FFF.mapFilterMapConsistency(cf, EQ) } ) fun FunctorFilter.mapFilterComposition(ff: (Int) -> Kind, EQ: Eq>): Unit = forAll( genConstructor(Gen.int(), ff), genFunctionAToB>(genOption(genIntSmall())), - genFunctionAToB>(genOption(genIntSmall())), - { fa: Kind, f, g -> - fa.mapFilter(f).mapFilter(g).equalUnderTheLaw(fa.mapFilter({ a -> f(a).flatMap(g) }), EQ) - }) + genFunctionAToB>(genOption(genIntSmall())) + ) { fa: Kind, f, g -> + fa.mapFilter(f).mapFilter(g).equalUnderTheLaw(fa.mapFilter { a -> f(a).flatMap(g) }, EQ) + } fun FunctorFilter.mapFilterMapConsistency(ff: (Int) -> Kind, EQ: Eq>): Unit = forAll( genConstructor(Gen.int(), ff), - genFunctionAToB(Gen.int()), - { fa: Kind, f -> - fa.mapFilter({ Some(f(it)) }).equalUnderTheLaw(fa.map(f), EQ) - }) + genFunctionAToB(Gen.int()) + ) { fa: Kind, f -> + fa.mapFilter { Some(f(it)) }.equalUnderTheLaw(fa.map(f), EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorLaws.kt index 7fe70cc4f28..ff1bb6f63e9 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/FunctorLaws.kt @@ -15,28 +15,27 @@ object FunctorLaws { inline fun laws(AP: Applicative, EQ: Eq>): List = listOf( - Law("Functor Laws: Covariant Identity", { AP.covariantIdentity(AP::just, EQ) }), - Law("Functor Laws: Covariant Composition", { AP.covariantComposition(AP::just, EQ) }) + Law("Functor Laws: Covariant Identity") { AP.covariantIdentity(AP::just, EQ) }, + Law("Functor Laws: Covariant Composition") { AP.covariantComposition(AP::just, EQ) } ) fun laws(FF: Functor, f: (Int) -> Kind, EQ: Eq>): List = listOf( - Law("Functor Laws: Covariant Identity", { FF.covariantIdentity(f, EQ) }), - Law("Functor Laws: Covariant Composition", { FF.covariantComposition(f, EQ) }) + Law("Functor Laws: Covariant Identity") { FF.covariantIdentity(f, EQ) }, + Law("Functor Laws: Covariant Composition") { FF.covariantComposition(f, EQ) } ) fun Functor.covariantIdentity(f: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), f), { fa: Kind -> + forAll(genConstructor(Gen.int(), f)) { fa: Kind -> fa.map(::identity).equalUnderTheLaw(fa, EQ) - }) + } fun Functor.covariantComposition(ff: (Int) -> Kind, EQ: Eq>): Unit = forAll( genConstructor(Gen.int(), ff), genFunctionAToB(Gen.int()), - genFunctionAToB(Gen.int()), - { fa: Kind, f, g -> - fa.map(f).map(g).equalUnderTheLaw(fa.map(f andThen g), EQ) - } - ) + genFunctionAToB(Gen.int()) + ) { fa: Kind, f, g -> + fa.map(f).map(g).equalUnderTheLaw(fa.map(f andThen g), EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/IsoLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/IsoLaws.kt index c2beb339ea3..69a51168bc6 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/IsoLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/IsoLaws.kt @@ -10,47 +10,47 @@ object IsoLaws { inline fun laws(iso: Iso, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq, EQB: Eq, bMonoid: Monoid): List = listOf( - Law("Iso Law: round trip one way", { iso.roundTripOneWay(aGen, EQA) }), - Law("Iso Law: round trip other way", { iso.roundTripOtherWay(bGen, EQB) }), - Law("Iso Law: modify identity is identity", { iso.modifyIdentity(aGen, EQA) }), - Law("Iso Law: compose modify", { iso.composeModify(aGen, funcGen, EQA) }), - Law("Iso Law: consitent set with modify", { iso.consistentSetModify(aGen, bGen, EQA) }), - Law("Iso Law: consistent modify with modify identity", { iso.consistentModifyModifyId(aGen, funcGen, EQA) }), - Law("Iso Law: consitent get with modify identity", { iso.consitentGetModifyId(aGen, EQB, bMonoid) }) + Law("Iso Law: round trip one way") { iso.roundTripOneWay(aGen, EQA) }, + Law("Iso Law: round trip other way") { iso.roundTripOtherWay(bGen, EQB) }, + Law("Iso Law: modify identity is identity") { iso.modifyIdentity(aGen, EQA) }, + Law("Iso Law: compose modify") { iso.composeModify(aGen, funcGen, EQA) }, + Law("Iso Law: consitent set with modify") { iso.consistentSetModify(aGen, bGen, EQA) }, + Law("Iso Law: consistent modify with modify identity") { iso.consistentModifyModifyId(aGen, funcGen, EQA) }, + Law("Iso Law: consitent get with modify identity") { iso.consitentGetModifyId(aGen, EQB, bMonoid) } ) fun Iso.roundTripOneWay(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> reverseGet(get(a)).equalUnderTheLaw(a, EQA) - }) + } fun Iso.roundTripOtherWay(bGen: Gen, EQB: Eq): Unit = - forAll(bGen, { b -> + forAll(bGen) { b -> get(reverseGet(b)).equalUnderTheLaw(b, EQB) - }) + } fun Iso.modifyIdentity(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> modify(a, ::identity).equalUnderTheLaw(a, EQA) - }) + } fun Iso.composeModify(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, funcGen, { a, f, g -> + forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } fun Iso.consistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(b).equalUnderTheLaw(modify(a) { b }, EQA) - }) + } fun Iso.consistentModifyModifyId(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, { a, f -> - modify(a, f).equalUnderTheLaw(modifyF(Id.functor(), a, { Id.just(f(it)) }).value(), EQA) - }) + forAll(aGen, funcGen) { a, f -> + modify(a, f).equalUnderTheLaw(modifyF(Id.functor(), a) { Id.just(f(it)) }.value(), EQA) + } fun Iso.consitentGetModifyId(aGen: Gen, EQB: Eq, bMonoid: Monoid): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> get(a).equalUnderTheLaw(modifyF(Const.applicative(bMonoid), a, ::Const).value(), EQB) - }) + } } \ No newline at end of file diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/LensLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/LensLaws.kt index cfdb269af8e..f1c6f3d3c62 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/LensLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/LensLaws.kt @@ -10,56 +10,56 @@ object LensLaws { inline fun laws(lens: Lens, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq, EQB: Eq, MB: Monoid) = listOf( - Law("Lens law: get set", { lens.lensGetSet(aGen, EQA) }), - Law("Lens law: set get", { lens.lensSetGet(aGen, bGen, EQB) }), - Law("Lens law: is set idempotent", { lens.lensSetIdempotent(aGen, bGen, EQA) }), - Law("Lens law: modify identity", { lens.lensModifyIdentity(aGen, EQA) }), - Law("Lens law: compose modify", { lens.lensComposeModify(aGen, funcGen, EQA) }), - Law("Lens law: consistent set modify", { lens.lensConsistentSetModify(aGen, bGen, EQA) }), - Law("Lens law: consistent modify modify id", { lens.lensConsistentModifyModifyId(aGen, funcGen, EQA) }), - Law("Lens law: consistent get modify id", { lens.lensConsistentGetModifyid(aGen, EQB, MB) }) + Law("Lens law: get set") { lens.lensGetSet(aGen, EQA) }, + Law("Lens law: set get") { lens.lensSetGet(aGen, bGen, EQB) }, + Law("Lens law: is set idempotent") { lens.lensSetIdempotent(aGen, bGen, EQA) }, + Law("Lens law: modify identity") { lens.lensModifyIdentity(aGen, EQA) }, + Law("Lens law: compose modify") { lens.lensComposeModify(aGen, funcGen, EQA) }, + Law("Lens law: consistent set modify") { lens.lensConsistentSetModify(aGen, bGen, EQA) }, + Law("Lens law: consistent modify modify id") { lens.lensConsistentModifyModifyId(aGen, funcGen, EQA) }, + Law("Lens law: consistent get modify id") { lens.lensConsistentGetModifyid(aGen, EQB, MB) } ) fun Lens.lensGetSet(aGen: Gen, EQA: Eq) = - forAll(aGen, { a -> + forAll(aGen) { a -> set(a, get(a)).equalUnderTheLaw(a, EQA) - }) + } fun Lens.lensSetGet(aGen: Gen, bGen: Gen, EQB: Eq) = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> get(set(a, b)).equalUnderTheLaw(b, EQB) - }) + } fun Lens.lensSetIdempotent(aGen: Gen, bGen: Gen, EQA: Eq) = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(set(a, b), b).equalUnderTheLaw(set(a, b), EQA) - }) + } fun Lens.lensModifyIdentity(aGen: Gen, EQA: Eq) = - forAll(aGen, { a -> + forAll(aGen) { a -> modify(a, ::identity).equalUnderTheLaw(a, EQA) - }) + } fun Lens.lensComposeModify(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq) = - forAll(aGen, funcGen, funcGen, { a, f, g -> + forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } fun Lens.lensConsistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq) = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(a, b).equalUnderTheLaw(modify(a) { b }, EQA) - }) + } fun Lens.lensConsistentModifyModifyId(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq) = - forAll(aGen, funcGen, { a, f -> + forAll(aGen, funcGen) { a, f -> modify(a, f) - .equalUnderTheLaw(modifyF(Id.functor(), a, { Id.just(f(it)) }).value(), EQA) - }) + .equalUnderTheLaw(modifyF(Id.functor(), a) { Id.just(f(it)) }.value(), EQA) + } fun Lens.lensConsistentGetModifyid(aGen: Gen, EQB: Eq, MA: Monoid) = - forAll(aGen, { a -> + forAll(aGen) { a -> get(a) .equalUnderTheLaw(modifyF(Const.applicative(MA), a, ::Const).value(), EQB) - }) + } } \ No newline at end of file diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadErrorLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadErrorLaws.kt index 418c6a191ea..8c6184301fb 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadErrorLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadErrorLaws.kt @@ -14,17 +14,17 @@ object MonadErrorLaws { inline fun laws(M: MonadError, EQERR: Eq>, EQ_EITHER: Eq>>, EQ: Eq> = EQERR): List = MonadLaws.laws(M, EQ) + ApplicativeErrorLaws.laws(M, EQERR, EQ_EITHER, EQ) + listOf( - Law("Monad Error Laws: left zero", { M.monadErrorLeftZero(EQERR) }), - Law("Monad Error Laws: ensure consistency", { M.monadErrorEnsureConsistency(EQERR) }) + Law("Monad Error Laws: left zero") { M.monadErrorLeftZero(EQERR) }, + Law("Monad Error Laws: ensure consistency") { M.monadErrorEnsureConsistency(EQERR) } ) fun MonadError.monadErrorLeftZero(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), genThrowable(), { f: (Int) -> Kind, e: Throwable -> + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), genThrowable()) { f: (Int) -> Kind, e: Throwable -> raiseError(e).flatMap(f).equalUnderTheLaw(raiseError(e), EQ) - }) + } fun MonadError.monadErrorEnsureConsistency(EQ: Eq>): Unit = - forAll(genApplicative(Gen.int(), this), genThrowable(), genFunctionAToB(Gen.bool()), { fa: Kind, e: Throwable, p: (Int) -> Boolean -> - fa.ensure({ e }, p).equalUnderTheLaw(fa.flatMap({ a -> if (p(a)) just(a) else raiseError(e) }), EQ) - }) + forAll(genApplicative(Gen.int(), this), genThrowable(), genFunctionAToB(Gen.bool())) { fa: Kind, e: Throwable, p: (Int) -> Boolean -> + fa.ensure({ e }, p).equalUnderTheLaw(fa.flatMap { a -> if (p(a)) just(a) else raiseError(e) }, EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadFilterLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadFilterLaws.kt index 5148fbec885..60252c33a47 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadFilterLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadFilterLaws.kt @@ -14,41 +14,41 @@ object MonadFilterLaws { inline fun laws(MF: MonadFilter, noinline cf: (Int) -> Kind, EQ: Eq>): List = MonadLaws.laws(MF, EQ) + FunctorFilterLaws.laws(MF, cf, EQ) + listOf( - Law("MonadFilter Laws: Left Empty", { MF.monadFilterLeftEmpty(EQ) }), - Law("MonadFilter Laws: Right Empty", { MF.monadFilterRightEmpty(EQ) }), - Law("MonadFilter Laws: Consistency", { MF.monadFilterConsistency(cf, EQ) }), - Law("MonadFilter Laws: Comprehension Guards", { MF.monadFilterEmptyComprehensions(EQ) }), - Law("MonadFilter Laws: Comprehension bindWithFilter Guards", { MF.monadFilterBindWithFilterComprehensions(EQ) })) + Law("MonadFilter Laws: Left Empty") { MF.monadFilterLeftEmpty(EQ) }, + Law("MonadFilter Laws: Right Empty") { MF.monadFilterRightEmpty(EQ) }, + Law("MonadFilter Laws: Consistency") { MF.monadFilterConsistency(cf, EQ) }, + Law("MonadFilter Laws: Comprehension Guards") { MF.monadFilterEmptyComprehensions(EQ) }, + Law("MonadFilter Laws: Comprehension bindWithFilter Guards") { MF.monadFilterBindWithFilterComprehensions(EQ) }) fun MonadFilter.monadFilterLeftEmpty(EQ: Eq>): Unit = - forAll(genFunctionAToB(genApplicative(Gen.int(), this)), { f: (Int) -> Kind -> + forAll(genFunctionAToB(genApplicative(Gen.int(), this))) { f: (Int) -> Kind -> empty().flatMap(f).equalUnderTheLaw(empty(), EQ) - }) + } fun MonadFilter.monadFilterRightEmpty(EQ: Eq>): Unit = - forAll(genApplicative(Gen.int(), this), { fa: Kind -> - fa.flatMap({ empty() }).equalUnderTheLaw(empty(), EQ) - }) + forAll(genApplicative(Gen.int(), this)) { fa: Kind -> + fa.flatMap { empty() }.equalUnderTheLaw(empty(), EQ) + } fun MonadFilter.monadFilterConsistency(cf: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genFunctionAToB(Gen.bool()), genConstructor(Gen.int(), cf), { f: (Int) -> Boolean, fa: Kind -> - fa.filter(f).equalUnderTheLaw(fa.flatMap({ a -> if (f(a)) just(a) else empty() }), EQ) - }) + forAll(genFunctionAToB(Gen.bool()), genConstructor(Gen.int(), cf)) { f: (Int) -> Boolean, fa: Kind -> + fa.filter(f).equalUnderTheLaw(fa.flatMap { a -> if (f(a)) just(a) else empty() }, EQ) + } fun MonadFilter.monadFilterEmptyComprehensions(EQ: Eq>): Unit = - forAll(Gen.bool(), Gen.int(), { guard: Boolean, n: Int -> + forAll(Gen.bool(), Gen.int()) { guard: Boolean, n: Int -> bindingFilter { continueIf(guard) n }.equalUnderTheLaw(if (!guard) empty() else just(n), EQ) - }) + } fun MonadFilter.monadFilterBindWithFilterComprehensions(EQ: Eq>): Unit = - forAll(Gen.bool(), Gen.int(), { guard: Boolean, n: Int -> + forAll(Gen.bool(), Gen.int()) { guard: Boolean, n: Int -> bindingFilter { val x = just(n).bindWithFilter { _ -> guard } x }.equalUnderTheLaw(if (!guard) empty() else just(n), EQ) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadLaws.kt index abd102665eb..6a890afec59 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadLaws.kt @@ -21,46 +21,46 @@ object MonadLaws { inline fun laws(M: Monad, EQ: Eq>): List = ApplicativeLaws.laws(M, EQ) + listOf( - Law("Monad Laws: left identity", { M.leftIdentity(EQ) }), - Law("Monad Laws: right identity", { M.rightIdentity(EQ) }), - Law("Monad Laws: kleisli left identity", { M.kleisliLeftIdentity(EQ) }), - Law("Monad Laws: kleisli right identity", { M.kleisliRightIdentity(EQ) }), - Law("Monad Laws: map / flatMap coherence", { M.mapFlatMapCoherence(EQ) }), - Law("Monad Laws: monad comprehensions", { M.monadComprehensions(EQ) }), - Law("Monad Laws: monad comprehensions binding in other threads", { M.monadComprehensionsBindInContext(EQ) }), - Law("Monad Laws: stack-safe//unsafe monad comprehensions equivalence", { M.equivalentComprehensions(EQ) }), - Law("Monad Laws: stack safe", { M.stackSafety(5000, EQ) }), - Law("Monad Laws: stack safe comprehensions", { M.stackSafetyComprehensions(5000, EQ) }) + Law("Monad Laws: left identity") { M.leftIdentity(EQ) }, + Law("Monad Laws: right identity") { M.rightIdentity(EQ) }, + Law("Monad Laws: kleisli left identity") { M.kleisliLeftIdentity(EQ) }, + Law("Monad Laws: kleisli right identity") { M.kleisliRightIdentity(EQ) }, + Law("Monad Laws: map / flatMap coherence") { M.mapFlatMapCoherence(EQ) }, + Law("Monad Laws: monad comprehensions") { M.monadComprehensions(EQ) }, + Law("Monad Laws: monad comprehensions binding in other threads") { M.monadComprehensionsBindInContext(EQ) }, + Law("Monad Laws: stack-safe//unsafe monad comprehensions equivalence") { M.equivalentComprehensions(EQ) }, + Law("Monad Laws: stack safe") { M.stackSafety(5000, EQ) }, + Law("Monad Laws: stack safe comprehensions") { M.stackSafetyComprehensions(5000, EQ) } ) fun Monad.leftIdentity(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int(), { f: (Int) -> Kind, a: Int -> + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int()) { f: (Int) -> Kind, a: Int -> just(a).flatMap(f).equalUnderTheLaw(f(a), EQ) - }) + } fun Monad.rightIdentity(EQ: Eq>): Unit = - forAll(genApplicative(Gen.int(), this), { fa: Kind -> - fa.flatMap({ just(it) }).equalUnderTheLaw(fa, EQ) - }) + forAll(genApplicative(Gen.int(), this)) { fa: Kind -> + fa.flatMap { just(it) }.equalUnderTheLaw(fa, EQ) + } fun Monad.kleisliLeftIdentity(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int(), { f: (Int) -> Kind, a: Int -> - (Kleisli({ n: Int -> just(n) }).andThen(this, Kleisli(f)).run(a).equalUnderTheLaw(f(a), EQ)) - }) + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int()) { f: (Int) -> Kind, a: Int -> + (Kleisli { n: Int -> just(n) }.andThen(this, Kleisli(f)).run(a).equalUnderTheLaw(f(a), EQ)) + } fun Monad.kleisliRightIdentity(EQ: Eq>): Unit = - forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int(), { f: (Int) -> Kind, a: Int -> - (Kleisli(f).andThen(this, Kleisli({ n: Int -> just(n) })).run(a).equalUnderTheLaw(f(a), EQ)) - }) + forAll(genFunctionAToB>(genApplicative(Gen.int(), this)), Gen.int()) { f: (Int) -> Kind, a: Int -> + (Kleisli(f).andThen(this, Kleisli { n: Int -> just(n) }).run(a).equalUnderTheLaw(f(a), EQ)) + } fun Monad.mapFlatMapCoherence(EQ: Eq>): Unit = - forAll(genFunctionAToB(Gen.int()), genApplicative(Gen.int(), this), { f: (Int) -> Int, fa: Kind -> - fa.flatMap({ just(f(it)) }).equalUnderTheLaw(fa.map(f), EQ) - }) + forAll(genFunctionAToB(Gen.int()), genApplicative(Gen.int(), this)) { f: (Int) -> Int, fa: Kind -> + fa.flatMap { just(f(it)) }.equalUnderTheLaw(fa.map(f), EQ) + } fun Monad.stackSafety(iterations: Int = 5000, EQ: Eq>): Unit = forFew(1, Gen.oneOf(listOf(iterations))) { iter -> - val res = tailRecM(0, { i -> just(if (i < iter) Left(i + 1) else Right(i)) }) + val res = tailRecM(0) { i -> just(if (i < iter) Left(i + 1) else Right(i)) } res.equalUnderTheLaw(just(iter), EQ) } @@ -71,7 +71,7 @@ object MonadLaws { } fun Monad.equivalentComprehensions(EQ: Eq>): Unit = - forAll(Gen.int(), { num: Int -> + forAll(Gen.int()) { num: Int -> val aa = binding { val a = just(num).bind() val b = just(a + 1).bind() @@ -86,26 +86,26 @@ object MonadLaws { }.run(this) aa.equalUnderTheLaw(bb, EQ) && aa.equalUnderTheLaw(just(num + 2), EQ) - }) + } fun Monad.monadComprehensions(EQ: Eq>): Unit = - forAll(Gen.int(), { num: Int -> + forAll(Gen.int()) { num: Int -> binding { val a = just(num).bind() val b = just(a + 1).bind() val c = just(b + 1).bind() c }.equalUnderTheLaw(just(num + 2), EQ) - }) + } fun Monad.monadComprehensionsBindInContext(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> binding { val a = bindIn(newSingleThreadContext("$num")) { num + 1 } val b = bindIn(newSingleThreadContext("$a")) { a + 1 } b }.equalUnderTheLaw(just(num + 2), EQ) - }) + } fun arrow.typeclasses.Monad.stackSafeTestProgram(n: kotlin.Int, stopAt: kotlin.Int): Free = bindingStackSafe { val v = this.just(n + 1).bind() diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadStateLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadStateLaws.kt index c6e6202c144..469ba7bd9a2 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadStateLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadStateLaws.kt @@ -10,29 +10,29 @@ object MonadStateLaws { inline fun laws(M: MonadState, EQ: Eq>, EQUnit: Eq>): List = MonadLaws.laws(M, EQ) + listOf( - Law("Monad State Laws: idempotence", { M.monadStateGetIdempotent(EQ) }), - Law("Monad State Laws: set twice eq to set once the last element", { M.monadStateSetTwice(EQUnit) }), - Law("Monad State Laws: set get", { M.monadStateSetGet(EQ) }), - Law("Monad State Laws: get set", { M.monadStateGetSet(EQUnit) }) + Law("Monad State Laws: idempotence") { M.monadStateGetIdempotent(EQ) }, + Law("Monad State Laws: set twice eq to set once the last element") { M.monadStateSetTwice(EQUnit) }, + Law("Monad State Laws: set get") { M.monadStateSetGet(EQ) }, + Law("Monad State Laws: get set") { M.monadStateGetSet(EQUnit) } ) fun MonadState.monadStateGetIdempotent(EQ: Eq>) { - get().flatMap({ get() }).equalUnderTheLaw(get(), EQ) + get().flatMap { get() }.equalUnderTheLaw(get(), EQ) } fun MonadState.monadStateSetTwice(EQ: Eq>) { - forAll(genIntSmall(), genIntSmall(), { s: Int, t: Int -> - set(s).flatMap({ set(t) }).equalUnderTheLaw(set(t), EQ) - }) + forAll(genIntSmall(), genIntSmall()) { s: Int, t: Int -> + set(s).flatMap { set(t) }.equalUnderTheLaw(set(t), EQ) + } } fun MonadState.monadStateSetGet(EQ: Eq>) { - forAll(genIntSmall(), { s: Int -> - set(s).flatMap({ get() }).equalUnderTheLaw(set(s).flatMap({ just(s) }), EQ) - }) + forAll(genIntSmall()) { s: Int -> + set(s).flatMap { get() }.equalUnderTheLaw(set(s).flatMap { just(s) }, EQ) + } } fun MonadState.monadStateGetSet(EQ: Eq>) { - get().flatMap({ set(it) }).equalUnderTheLaw(just(Unit), EQ) + get().flatMap { set(it) }.equalUnderTheLaw(just(Unit), EQ) } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadSuspendLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadSuspendLaws.kt index d2647f9e18d..2fa5fdfc0b6 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadSuspendLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadSuspendLaws.kt @@ -16,21 +16,21 @@ import kotlinx.coroutines.experimental.newSingleThreadContext object MonadSuspendLaws { inline fun laws(SC: MonadDefer, EQ: Eq>, EQ_EITHER: Eq>>, EQERR: Eq> = EQ): List = MonadErrorLaws.laws(SC, EQERR, EQ_EITHER, EQ) + listOf( - Law("Sync bind: binding blocks", { SC.asyncBind(EQ) }), - Law("Sync bind: binding failure", { SC.asyncBindError(EQERR) }), - Law("Sync bind: unsafe binding", { SC.asyncBindUnsafe(EQ) }), - Law("Sync bind: unsafe binding failure", { SC.asyncBindUnsafeError(EQERR) }), - Law("Sync bind: binding in parallel", { SC.asyncParallelBind(EQ) }), - Law("Sync bind: binding cancellation before flatMap", { SC.asyncCancellationBefore(EQ) }), - Law("Sync bind: binding cancellation after flatMap", { SC.asyncCancellationAfter(EQ) }), - Law("Sync bind: bindingInContext cancellation before flatMap", { SC.inContextCancellationBefore(EQ) }), - Law("Sync bind: bindingInContext cancellation after flatMap", { SC.inContextCancellationAfter(EQ) }), - Law("Sync bind: bindingInContext throw equivalent to raiseError", { SC.inContextErrorThrow(EQERR) }), - Law("Sync bind: monad comprehensions binding in other threads equivalence", { SC.monadComprehensionsBindInContextEquivalent(EQ) }) + Law("Sync bind: binding blocks") { SC.asyncBind(EQ) }, + Law("Sync bind: binding failure") { SC.asyncBindError(EQERR) }, + Law("Sync bind: unsafe binding") { SC.asyncBindUnsafe(EQ) }, + Law("Sync bind: unsafe binding failure") { SC.asyncBindUnsafeError(EQERR) }, + Law("Sync bind: binding in parallel") { SC.asyncParallelBind(EQ) }, + Law("Sync bind: binding cancellation before flatMap") { SC.asyncCancellationBefore(EQ) }, + Law("Sync bind: binding cancellation after flatMap") { SC.asyncCancellationAfter(EQ) }, + Law("Sync bind: bindingInContext cancellation before flatMap") { SC.inContextCancellationBefore(EQ) }, + Law("Sync bind: bindingInContext cancellation after flatMap") { SC.inContextCancellationAfter(EQ) }, + Law("Sync bind: bindingInContext throw equivalent to raiseError") { SC.inContextErrorThrow(EQERR) }, + Law("Sync bind: monad comprehensions binding in other threads equivalence") { SC.monadComprehensionsBindInContextEquivalent(EQ) } ) fun MonadDefer.asyncBind(EQ: Eq>): Unit = - forAll(genIntSmall(), genIntSmall(), genIntSmall(), { x: Int, y: Int, z: Int -> + forAll(genIntSmall(), genIntSmall(), genIntSmall()) { x: Int, y: Int, z: Int -> val (bound, dispose) = bindingCancellable { val a = bindDefer { x } val b = bindDefer { a + y } @@ -38,18 +38,18 @@ object MonadSuspendLaws { c } bound.equalUnderTheLaw(just(x + y + z), EQ) - }) + } fun MonadDefer.asyncBindError(EQ: Eq>): Unit = - forAll(genThrowable(), { e: Throwable -> + forAll(genThrowable()) { e: Throwable -> val (bound: Kind, cancel) = bindingCancellable { bindDefer { throw e } } bound.equalUnderTheLaw(raiseError(e), EQ) - }) + } fun MonadDefer.asyncBindUnsafe(EQ: Eq>): Unit = - forAll(genIntSmall(), genIntSmall(), genIntSmall(), { x: Int, y: Int, z: Int -> + forAll(genIntSmall(), genIntSmall(), genIntSmall()) { x: Int, y: Int, z: Int -> val (bound, dispose) = bindingCancellable { val a = bindDeferUnsafe { Right(x) } val b = bindDeferUnsafe { Right(a + y) } @@ -57,27 +57,27 @@ object MonadSuspendLaws { c } bound.equalUnderTheLaw(just(x + y + z), EQ) - }) + } fun MonadDefer.asyncBindUnsafeError(EQ: Eq>): Unit = - forAll(genThrowable(), { e: Throwable -> + forAll(genThrowable()) { e: Throwable -> val (bound: Kind, dispose) = bindingCancellable { bindDeferUnsafe { Left(e) } } bound.equalUnderTheLaw(raiseError(e), EQ) - }) + } fun MonadDefer.asyncParallelBind(EQ: Eq>): Unit = - forAll(genIntSmall(), genIntSmall(), genIntSmall(), { x: Int, y: Int, z: Int -> + forAll(genIntSmall(), genIntSmall(), genIntSmall()) { x: Int, y: Int, z: Int -> val (bound, dispose) = bindingCancellable { val value = bind { tupled(invoke { x }, invoke { y }, invoke { z }) } value.a + value.b + value.c } bound.equalUnderTheLaw(just(x + y + z), EQ) - }) + } fun MonadDefer.asyncCancellationBefore(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> val sideEffect = SideEffect() val (binding, dispose) = bindingCancellable { val a = bindDefer { Thread.sleep(500); num } @@ -88,10 +88,10 @@ object MonadSuspendLaws { } Try { Thread.sleep(250); dispose() }.recover { throw it } binding.equalUnderTheLaw(raiseError(BindingCancellationException()), EQ) && sideEffect.counter == 0 - }) + } fun MonadDefer.asyncCancellationAfter(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> val sideEffect = SideEffect() val (binding, dispose) = bindingCancellable { val a = bindDefer { num } @@ -102,10 +102,10 @@ object MonadSuspendLaws { Try { Thread.sleep(250); dispose() }.recover { throw it } binding.equalUnderTheLaw(raiseError(BindingCancellationException()), EQ) && sideEffect.counter == 0 - }) + } fun MonadDefer.inContextCancellationBefore(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> val sideEffect = SideEffect() val (binding, dispose) = bindingCancellable { val a = bindIn(CommonPool) { Thread.sleep(500); num } @@ -116,10 +116,10 @@ object MonadSuspendLaws { } Try { Thread.sleep(250); dispose() }.recover { throw it } binding.equalUnderTheLaw(raiseError(BindingCancellationException()), EQ) && sideEffect.counter == 0 - }) + } fun MonadDefer.inContextCancellationAfter(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> val sideEffect = SideEffect() val (binding, dispose) = bindingCancellable { val a = bindIn(CommonPool) { num } @@ -130,18 +130,18 @@ object MonadSuspendLaws { Try { Thread.sleep(250); dispose() }.recover { throw it } binding.equalUnderTheLaw(raiseError(BindingCancellationException()), EQ) && sideEffect.counter == 0 - }) + } fun MonadDefer.inContextErrorThrow(EQ: Eq>): Unit = - forFew(5, genThrowable(), { throwable: Throwable -> + forFew(5, genThrowable()) { throwable: Throwable -> bindingCancellable { val a: Int = bindIn(newSingleThreadContext("1")) { throw throwable } a }.a.equalUnderTheLaw(raiseError(throwable), EQ) - }) + } fun MonadDefer.monadComprehensionsBindInContextEquivalent(EQ: Eq>): Unit = - forFew(5, genIntSmall(), { num: Int -> + forFew(5, genIntSmall()) { num: Int -> val bindM = bindingCancellable { val a = bindDeferIn(newSingleThreadContext("$num")) { num + 1 } val b = bindDeferIn(newSingleThreadContext("$a")) { a + 1 } @@ -153,5 +153,5 @@ object MonadSuspendLaws { b } bindM.a.equalUnderTheLaw(bind.a, EQ) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadWriterLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadWriterLaws.kt index f164c753d33..dba88c2897e 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadWriterLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonadWriterLaws.kt @@ -19,36 +19,36 @@ object MonadWriterLaws { EqInt: Eq>, EqTupleWA: Eq>>): List = MonadLaws.laws(MF, EqInt) + listOf( - Law("Monad Writer Laws: writer just", { MW.monadWriterWriterJust(MOW, EqInt) }), - Law("Monad Writer Laws: tell fusion", { MW.monadWriterTellFusion(genW, MOW) }), - Law("Monad Writer Laws: listen just", { MW.monadWriterListenJust(MOW, EqTupleWA) }), - Law("Monad Writer Laws: listen writer", { MW.monadWriterListenWriter(genTupleWA, EqTupleWA) })) + Law("Monad Writer Laws: writer just") { MW.monadWriterWriterJust(MOW, EqInt) }, + Law("Monad Writer Laws: tell fusion") { MW.monadWriterTellFusion(genW, MOW) }, + Law("Monad Writer Laws: listen just") { MW.monadWriterListenJust(MOW, EqTupleWA) }, + Law("Monad Writer Laws: listen writer") { MW.monadWriterListenWriter(genTupleWA, EqTupleWA) }) fun MonadWriter.monadWriterWriterJust(MOW: Monoid, EQ: Eq>): Unit { - forAll(Gen.int(), { a: Int -> + forAll(Gen.int()) { a: Int -> writer(Tuple2(MOW.empty(), a)).equalUnderTheLaw(just(a), EQ) - }) + } } fun MonadWriter.monadWriterTellFusion(genW: Gen, MOW: Monoid): Unit { - forAll(genW, genW, { x: W, y: W -> - tell(x).flatMap({ tell(y) }).equalUnderTheLaw(tell(MOW.run { x.combine(y) }), Eq.any()) - }) + forAll(genW, genW) { x: W, y: W -> + tell(x).flatMap { tell(y) }.equalUnderTheLaw(tell(MOW.run { x.combine(y) }), Eq.any()) + } } fun MonadWriter.monadWriterListenJust(MOW: Monoid, EqTupleWA: Eq>>): Unit { - forAll(Gen.int(), { a: Int -> + forAll(Gen.int()) { a: Int -> just(a).listen().equalUnderTheLaw(just(Tuple2(MOW.empty(), a)), EqTupleWA) - }) + } } fun MonadWriter.monadWriterListenWriter(genTupleWA: Gen>, EqTupleWA: Eq>>): Unit { - forAll(genTupleWA, { tupleWA: Tuple2 -> - writer(tupleWA).listen().equalUnderTheLaw(tell(tupleWA.a).map({ tupleWA }), EqTupleWA) - }) + forAll(genTupleWA) { tupleWA: Tuple2 -> + writer(tupleWA).listen().equalUnderTheLaw(tell(tupleWA.a).map { tupleWA }, EqTupleWA) + } } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidKLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidKLaws.kt index c36b7cf8579..7def58e345f 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidKLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidKLaws.kt @@ -12,21 +12,21 @@ object MonoidKLaws { inline fun laws(SGK: MonoidK, AP: Applicative, EQ: Eq>): List = SemigroupKLaws.laws(SGK, AP, EQ) + listOf( - Law("MonoidK Laws: Left identity", { SGK.monoidKLeftIdentity(AP::just, EQ) }), - Law("MonoidK Laws: Right identity", { SGK.monoidKRightIdentity(AP::just, EQ) })) + Law("MonoidK Laws: Left identity") { SGK.monoidKLeftIdentity(AP::just, EQ) }, + Law("MonoidK Laws: Right identity") { SGK.monoidKRightIdentity(AP::just, EQ) }) inline fun laws(SGK: MonoidK, noinline f: (Int) -> Kind, EQ: Eq>): List = SemigroupKLaws.laws(SGK, f, EQ) + listOf( - Law("MonoidK Laws: Left identity", { SGK.monoidKLeftIdentity(f, EQ) }), - Law("MonoidK Laws: Right identity", { SGK.monoidKRightIdentity(f, EQ) })) + Law("MonoidK Laws: Left identity") { SGK.monoidKLeftIdentity(f, EQ) }, + Law("MonoidK Laws: Right identity") { SGK.monoidKRightIdentity(f, EQ) }) fun MonoidK.monoidKLeftIdentity(f: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), f), { fa: Kind -> + forAll(genConstructor(Gen.int(), f)) { fa: Kind -> empty().combineK(fa).equalUnderTheLaw(fa, EQ) - }) + } fun MonoidK.monoidKRightIdentity(f: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), f), { fa: Kind -> + forAll(genConstructor(Gen.int(), f)) { fa: Kind -> fa.combineK(empty()).equalUnderTheLaw(fa, EQ) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidLaws.kt index 731101c7f59..04d5f1e28bc 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/MonoidLaws.kt @@ -7,8 +7,8 @@ object MonoidLaws { inline fun laws(M: Monoid, A: F, EQ: Eq): List = listOf( - Law("Monoid Laws: Left identity", { M.monoidLeftIdentity(A, EQ) }), - Law("Monoid Laws: Right identity", { M.monoidRightIdentity(A, EQ) }) + Law("Monoid Laws: Left identity") { M.monoidLeftIdentity(A, EQ) }, + Law("Monoid Laws: Right identity") { M.monoidRightIdentity(A, EQ) } ) fun Monoid.monoidLeftIdentity(A: F, EQ: Eq): Boolean = diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OptionalLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OptionalLaws.kt index 51f1ca5aac7..1391bd6559b 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OptionalLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OptionalLaws.kt @@ -11,57 +11,57 @@ import io.kotlintest.properties.forAll object OptionalLaws { inline fun laws(optional: Optional, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq, EQOptionB: Eq>): List = listOf( - Law("Optional Law: set what you get", { optional.getOptionSet(aGen, EQA) }), - Law("Optional Law: set what you get", { optional.setGetOption(aGen, bGen, EQOptionB) }), - Law("Optional Law: set is idempotent", { optional.setIdempotent(aGen, bGen, EQA) }), - Law("Optional Law: modify identity = identity", { optional.modifyIdentity(aGen, EQA) }), - Law("Optional Law: compose modify", { optional.composeModify(aGen, funcGen, EQA) }), - Law("Optional Law: consistent set with modify", { optional.consistentSetModify(aGen, bGen, EQA) }), - Law("Optional Law: consistent modify with modify identity", { optional.consistentModifyModifyId(aGen, funcGen, EQA) }), - Law("Optional Law: consistent getOption with modify identity", { optional.consistentGetOptionModifyId(aGen, EQOptionB) }) + Law("Optional Law: set what you get") { optional.getOptionSet(aGen, EQA) }, + Law("Optional Law: set what you get") { optional.setGetOption(aGen, bGen, EQOptionB) }, + Law("Optional Law: set is idempotent") { optional.setIdempotent(aGen, bGen, EQA) }, + Law("Optional Law: modify identity = identity") { optional.modifyIdentity(aGen, EQA) }, + Law("Optional Law: compose modify") { optional.composeModify(aGen, funcGen, EQA) }, + Law("Optional Law: consistent set with modify") { optional.consistentSetModify(aGen, bGen, EQA) }, + Law("Optional Law: consistent modify with modify identity") { optional.consistentModifyModifyId(aGen, funcGen, EQA) }, + Law("Optional Law: consistent getOption with modify identity") { optional.consistentGetOptionModifyId(aGen, EQOptionB) } ) fun Optional.getOptionSet(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> - getOrModify(a).fold(::identity, { set(a, it) }) + forAll(aGen) { a -> + getOrModify(a).fold(::identity) { set(a, it) } .equalUnderTheLaw(a, EQA) - }) + } fun Optional.setGetOption(aGen: Gen, bGen: Gen, EQOptionB: Eq>): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> getOption(set(a, b)) .equalUnderTheLaw(getOption(a).map { _ -> b }, EQOptionB) - }) + } fun Optional.setIdempotent(aGen: Gen, bGen: Gen, EQA: Eq): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(set(a, b), b) .equalUnderTheLaw(set(a, b), EQA) - }) + } fun Optional.modifyIdentity(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> modify(a, ::identity) .equalUnderTheLaw(a, EQA) - }) + } fun Optional.composeModify(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, funcGen, { a, f, g -> + forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g) .equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } fun Optional.consistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(a, b) .equalUnderTheLaw(modify(a) { b }, EQA) - }) + } fun Optional.consistentModifyModifyId(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, { a, f -> + forAll(aGen, funcGen) { a, f -> modify(a, f) - .equalUnderTheLaw(modifyF(Id.applicative(), a, { Id.just(f(it)) }).value(), EQA) - }) + .equalUnderTheLaw(modifyF(Id.applicative(), a) { Id.just(f(it)) }.value(), EQA) + } fun Optional.consistentGetOptionModifyId(aGen: Gen, EQOptionB: Eq>): Unit { val firstMonoid = object : Monoid> { @@ -69,11 +69,11 @@ object OptionalLaws { override fun FirstOption.combine(b: FirstOption): FirstOption = if (option.fold({ false }, { true })) this else b } - forAll(aGen, { a -> - modifyF(Const.applicative(firstMonoid), a, { b -> + forAll(aGen) { a -> + modifyF(Const.applicative(firstMonoid), a) { b -> Const(FirstOption(Some(b))) - }).value().option.equalUnderTheLaw(getOption(a), EQOptionB) - }) + }.value().option.equalUnderTheLaw(getOption(a), EQOptionB) + } } @PublishedApi diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OrderLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OrderLaws.kt index 87c16f0fcb8..da392d574c6 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OrderLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/OrderLaws.kt @@ -8,99 +8,99 @@ import io.kotlintest.properties.forAll object OrderLaws { inline fun laws(O: Order, fGen: Gen, funcGen: Gen<(F) -> F>): List = - EqLaws.laws(O, { fGen.generate() }) + listOf( - Law("Order law: reflexivity equality", { O.reflexitivityEq(fGen) }), - Law("Order law: symmetry equality", { O.symmetryEq(fGen) }), - Law("Order law: antisymmetry equality", { O.antisymmetryEq(fGen, funcGen) }), - Law("Order law: transitivity equality", { O.transitivityEq(fGen) }), - Law("Order law: reflexivity partial order", { O.reflexivityPartialOrder(fGen) }), - Law("Order law: antisymmetry partial order", { O.antisymmetryPartialOrder(fGen) }), - Law("Order law: transitivity partial order", { O.transitivityPartialOrder(fGen) }), - Law("Order law: greater than or equal partial order", { O.greaterThanOrEqualPartialOrder(fGen) }), - Law("Order law: lesser than partial order", { O.lesserThanPartialOrder(fGen) }), - Law("Order law: greater than partial order", { O.greaterThanPartialOrder(fGen) }), - Law("Order law: totality order", { O.totalityOrder(fGen) }), - Law("Order law: compare order", { O.compareOrder(fGen) }), - Law("Order law: min order", { O.minOrder(fGen) }), - Law("Order law: max order", { O.maxOrder(fGen) }) + EqLaws.laws(O) { fGen.generate() } + listOf( + Law("Order law: reflexivity equality") { O.reflexitivityEq(fGen) }, + Law("Order law: symmetry equality") { O.symmetryEq(fGen) }, + Law("Order law: antisymmetry equality") { O.antisymmetryEq(fGen, funcGen) }, + Law("Order law: transitivity equality") { O.transitivityEq(fGen) }, + Law("Order law: reflexivity partial order") { O.reflexivityPartialOrder(fGen) }, + Law("Order law: antisymmetry partial order") { O.antisymmetryPartialOrder(fGen) }, + Law("Order law: transitivity partial order") { O.transitivityPartialOrder(fGen) }, + Law("Order law: greater than or equal partial order") { O.greaterThanOrEqualPartialOrder(fGen) }, + Law("Order law: lesser than partial order") { O.lesserThanPartialOrder(fGen) }, + Law("Order law: greater than partial order") { O.greaterThanPartialOrder(fGen) }, + Law("Order law: totality order") { O.totalityOrder(fGen) }, + Law("Order law: compare order") { O.compareOrder(fGen) }, + Law("Order law: min order") { O.minOrder(fGen) }, + Law("Order law: max order") { O.maxOrder(fGen) } ) fun Order.reflexitivityEq(fGen: Gen) = - forAll(fGen, { x -> + forAll(fGen) { x -> x.equalUnderTheLaw(x, this) - }) + } fun Order.symmetryEq(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> x.eqv(y).equalUnderTheLaw(y.eqv(x), Eq.any()) - }) + } fun Order.antisymmetryEq(fGen: Gen, funcGen: Gen<(F) -> F>) = - forAll(fGen, fGen, funcGen, { x, y, f -> + forAll(fGen, fGen, funcGen) { x, y, f -> !x.eqv(y) || f(x).eqv(f(y)) - }) + } fun Order.transitivityEq(fGen: Gen) = - forAll(fGen, fGen, fGen, { x, y, z -> + forAll(fGen, fGen, fGen) { x, y, z -> !(x.eqv(y) && y.eqv(z)) || x.eqv(z) - }) + } fun Order.reflexivityPartialOrder(fGen: Gen) = - forAll(fGen, { x -> + forAll(fGen) { x -> x.lte(x) - }) + } fun Order.antisymmetryPartialOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> !(x.lte(y) && y.lte(x)) || x.eqv(y) - }) + } fun Order.transitivityPartialOrder(fGen: Gen) = - forAll(fGen, fGen, fGen, { x, y, z -> + forAll(fGen, fGen, fGen) { x, y, z -> !(x.lte(y) && y.lte(x)) || x.lte(z) - }) + } fun Order.greaterThanOrEqualPartialOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> x.lte(y) == y.gte(x) - }) + } fun Order.lesserThanPartialOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> x.lt(y) == (x.lte(y) && x.neqv(y)) - }) + } fun Order.greaterThanPartialOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> x.lt(y) == y.gt(x) - }) + } fun Order.totalityOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> x.lte(y) || y.lte(x) - }) + } fun Order.compareOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> val c = x.compare(y) ((c < 0) == x.lt(y)) && ((c == 0) == x.eqv(y)) && ((c > 0) == x.gt(y)) - }) + } fun Order.minOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> val c = x.compare(y) val m = x.min(y) if (c < 0) m == x else if (c == 0) (m == x) && (m == y) else m == y - }) + } fun Order.maxOrder(fGen: Gen) = - forAll(fGen, fGen, { x, y -> + forAll(fGen, fGen) { x, y -> val c = x.compare(y) val m = x.max(y) if (c < 0) m == y else if (c == 0) (m == x) && (m == y) else m == x - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/PrismLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/PrismLaws.kt index 89d7d9d6960..25419418355 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/PrismLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/PrismLaws.kt @@ -10,54 +10,54 @@ import io.kotlintest.properties.forAll object PrismLaws { inline fun laws(prism: Prism, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq, EQOptionB: Eq>): List = listOf( - Law("Prism law: partial round trip one way", { prism.partialRoundTripOneWay(aGen, EQA) }), - Law("Prism law: round trip other way", { prism.roundTripOtherWay(bGen, EQOptionB) }), - Law("Prism law: modify identity", { prism.modifyIdentity(aGen, EQA) }), - Law("Prism law: compose modify", { prism.composeModify(aGen, funcGen, EQA) }), - Law("Prism law: consistent set modify", { prism.consistentSetModify(aGen, bGen, EQA) }), - Law("Prism law: consistent modify with modifyF Id", { prism.consistentModifyModifyFId(aGen, funcGen, EQA) }), - Law("Prism law: consistent get option modify id", { prism.consistentGetOptionModifyId(aGen, EQOptionB) }) + Law("Prism law: partial round trip one way") { prism.partialRoundTripOneWay(aGen, EQA) }, + Law("Prism law: round trip other way") { prism.roundTripOtherWay(bGen, EQOptionB) }, + Law("Prism law: modify identity") { prism.modifyIdentity(aGen, EQA) }, + Law("Prism law: compose modify") { prism.composeModify(aGen, funcGen, EQA) }, + Law("Prism law: consistent set modify") { prism.consistentSetModify(aGen, bGen, EQA) }, + Law("Prism law: consistent modify with modifyF Id") { prism.consistentModifyModifyFId(aGen, funcGen, EQA) }, + Law("Prism law: consistent get option modify id") { prism.consistentGetOptionModifyId(aGen, EQOptionB) } ) fun Prism.partialRoundTripOneWay(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> getOrModify(a).fold(::identity, this::reverseGet) .equalUnderTheLaw(a, EQA) - }) + } fun Prism.roundTripOtherWay(bGen: Gen, EQOptionB: Eq>): Unit = - forAll(bGen, { b -> + forAll(bGen) { b -> getOption(reverseGet(b)) .equalUnderTheLaw(Some(b), EQOptionB) - }) + } fun Prism.modifyIdentity(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> modify(a, ::identity).equalUnderTheLaw(a, EQA) - }) + } fun Prism.composeModify(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, funcGen, { a, f, g -> + forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } fun Prism.consistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(a, b).equalUnderTheLaw(modify(a) { b }, EQA) - }) + } fun Prism.consistentModifyModifyFId(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, { a, f -> - modifyF(Id.applicative(), a, { Id.just(f(it)) }).value().equalUnderTheLaw(modify(a, f), EQA) - }) + forAll(aGen, funcGen) { a, f -> + modifyF(Id.applicative(), a) { Id.just(f(it)) }.value().equalUnderTheLaw(modify(a, f), EQA) + } fun Prism.consistentGetOptionModifyId(aGen: Gen, EQOptionB: Eq>): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> modifyF(Const.applicative(object : Monoid> { override fun Option.combine(b: Option): Option = orElse { b } override fun empty(): Option = None - }), a, { Const(Some(it)) }).value().equalUnderTheLaw(getOption(a), EQOptionB) - }) + }), a) { Const(Some(it)) }.value().equalUnderTheLaw(getOption(a), EQOptionB) + } } \ No newline at end of file diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ReducibleLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ReducibleLaws.kt index caba9d7366f..00a0717847b 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ReducibleLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ReducibleLaws.kt @@ -15,53 +15,53 @@ import io.kotlintest.properties.forAll object ReducibleLaws { inline fun laws(RF: Reducible, noinline cf: (Int) -> Kind, EQ: Eq, EQOptionInt: Eq>, EQLong: Eq): List = FoldableLaws.laws(RF, cf, EQ) + listOf( - Law("Reducible Laws: reduceLeftTo consistent with reduceMap", { RF.reduceLeftToConsistentWithReduceMap(cf, EQ) }), - Law("Reducible Laws: reduceRightTo consistent with reduceMap", { RF.reduceRightToConsistentWithReduceMap(cf, EQ) }), - Law("Reducible Laws: reduceRightTo consistent with reduceRightToOption", { RF.reduceRightToConsistentWithReduceRightToOption(cf, EQOptionInt) }), - Law("Reducible Laws: reduceRight consistent with reduceRightOption", { RF.reduceRightConsistentWithReduceRightOption(cf, EQOptionInt) }), - Law("Reducible Laws: reduce reduce left consistent", { RF.reduceReduceLeftConsistent(cf, EQ) }), - Law("Reducible Laws: size consistent", { RF.sizeConsistent(cf, EQLong) }) + Law("Reducible Laws: reduceLeftTo consistent with reduceMap") { RF.reduceLeftToConsistentWithReduceMap(cf, EQ) }, + Law("Reducible Laws: reduceRightTo consistent with reduceMap") { RF.reduceRightToConsistentWithReduceMap(cf, EQ) }, + Law("Reducible Laws: reduceRightTo consistent with reduceRightToOption") { RF.reduceRightToConsistentWithReduceRightToOption(cf, EQOptionInt) }, + Law("Reducible Laws: reduceRight consistent with reduceRightOption") { RF.reduceRightConsistentWithReduceRightOption(cf, EQOptionInt) }, + Law("Reducible Laws: reduce reduce left consistent") { RF.reduceReduceLeftConsistent(cf, EQ) }, + Law("Reducible Laws: size consistent") { RF.sizeConsistent(cf, EQLong) } ) fun Reducible.reduceLeftToConsistentWithReduceMap(cf: (Int) -> Kind, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - fa.reduceMap(this, f).equalUnderTheLaw(fa.reduceLeftTo(f, { b, a -> b.combine(f(a)) }), EQ) + fa.reduceMap(this, f).equalUnderTheLaw(fa.reduceLeftTo(f) { b, a -> b.combine(f(a)) }, EQ) } - }) + } fun Reducible.reduceRightToConsistentWithReduceMap(cf: (Int) -> Kind, EQ: Eq) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - fa.reduceMap(this, f).equalUnderTheLaw(fa.reduceRightTo(f, { a, eb -> eb.map({ f(a).combine(it) }) }).value(), EQ) + fa.reduceMap(this, f).equalUnderTheLaw(fa.reduceRightTo(f) { a, eb -> eb.map { f(a).combine(it) } }.value(), EQ) } - }) + } fun Reducible.reduceRightToConsistentWithReduceRightToOption(cf: (Int) -> Kind, EQ: Eq>) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> with(Int.monoid()) { - fa.reduceRightToOption(f, { a, eb -> eb.map({ f(a).combine(it) }) }).value() - .equalUnderTheLaw(fa.reduceRightTo(f, { a, eb -> eb.map({ f(a).combine(it) }) }).map({ Option(it) }).value(), EQ) + fa.reduceRightToOption(f) { a, eb -> eb.map { f(a).combine(it) } }.value() + .equalUnderTheLaw(fa.reduceRightTo(f) { a, eb -> eb.map { f(a).combine(it) } }.map { Option(it) }.value(), EQ) } - }) + } fun Reducible.reduceRightConsistentWithReduceRightOption(cf: (Int) -> Kind, EQ: Eq>) = - forAll(genFunctionAAToA(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int, Int) -> Int, fa: Kind -> - fa.reduceRight({ a1, e2 -> Eval.Now(f(a1, e2.value())) }).map({ Option(it) }).value() - .equalUnderTheLaw(fa.reduceRightOption({ a1, e2 -> Eval.Now(f(a1, e2.value())) }).value(), EQ) - }) + forAll(genFunctionAAToA(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int, Int) -> Int, fa: Kind -> + fa.reduceRight { a1, e2 -> Eval.Now(f(a1, e2.value())) }.map { Option(it) }.value() + .equalUnderTheLaw(fa.reduceRightOption { a1, e2 -> Eval.Now(f(a1, e2.value())) }.value(), EQ) + } fun Reducible.reduceReduceLeftConsistent(cf: (Int) -> Kind, EQ: Eq) = - forAll(genConstructor(genIntSmall(), cf), { fa: Kind -> + forAll(genConstructor(genIntSmall(), cf)) { fa: Kind -> with(Int.monoid()) { - fa.reduce(this).equalUnderTheLaw(fa.reduceLeft({ a1, a2 -> a1.combine(a2) }), EQ) + fa.reduce(this).equalUnderTheLaw(fa.reduceLeft { a1, a2 -> a1.combine(a2) }, EQ) } - }) + } fun Reducible.sizeConsistent(cf: (Int) -> Kind, EQ: Eq) = - forAll(genConstructor(genIntSmall(), cf), { fa: Kind -> + forAll(genConstructor(genIntSmall(), cf)) { fa: Kind -> with(Long.monoid()) { fa.size(this).equalUnderTheLaw(fa.reduceMap(this) { 1L }, EQ) } - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupKlaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupKlaws.kt index e4b59edb3a3..cc4e0a2e305 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupKlaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupKlaws.kt @@ -11,13 +11,13 @@ import io.kotlintest.properties.forAll object SemigroupKLaws { inline fun laws(SGK: SemigroupK, AP: Applicative, EQ: Eq>): List = - listOf(Law("SemigroupK: associativity", { SGK.semigroupKAssociative(AP::just, EQ) })) + listOf(Law("SemigroupK: associativity") { SGK.semigroupKAssociative(AP::just, EQ) }) inline fun laws(SGK: SemigroupK, noinline f: (Int) -> Kind, EQ: Eq>): List = - listOf(Law("SemigroupK: associativity", { SGK.semigroupKAssociative(f, EQ) })) + listOf(Law("SemigroupK: associativity") { SGK.semigroupKAssociative(f, EQ) }) fun SemigroupK.semigroupKAssociative(f: (Int) -> Kind, EQ: Eq>): Unit = - forAll(genConstructor(Gen.int(), f), genConstructor(Gen.int(), f), genConstructor(Gen.int(), f), { a, b, c -> + forAll(genConstructor(Gen.int(), f), genConstructor(Gen.int(), f), genConstructor(Gen.int(), f)) { a, b, c -> a.combineK(b).combineK(c).equalUnderTheLaw(a.combineK(b.combineK(c)), EQ) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupLaws.kt index 48c3ee09992..e607c649151 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SemigroupLaws.kt @@ -6,7 +6,7 @@ import arrow.typeclasses.Semigroup object SemigroupLaws { inline fun laws(SG: Semigroup, A: F, B: F, C: F, EQ: Eq): List = - listOf(Law("Semigroup: associativity", { SG.semigroupAssociative(A, B, C, EQ) })) + listOf(Law("Semigroup: associativity") { SG.semigroupAssociative(A, B, C, EQ) }) fun Semigroup.semigroupAssociative(A: F, B: F, C: F, EQ: Eq): Boolean = A.combine(B).combine(C).equalUnderTheLaw(A.combine(B.combine(C)), EQ) diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SetterLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SetterLaws.kt index 81966ac207b..9782e25f844 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SetterLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/SetterLaws.kt @@ -10,25 +10,25 @@ import io.kotlintest.properties.forAll object SetterLaws { inline fun laws(setter: Setter, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq) = listOf( - Law("Setter law: set is idempotent", { setter.setIdempotent(aGen, bGen, EQA) }), - Law("Setter law: modify identity", { setter.modifyIdentity(aGen, EQA) }), - Law("Setter law: compose modify", { setter.composeModify(aGen, EQA, funcGen) }), - Law("Setter law: consistent set modify", { setter.consistentSetModify(aGen, bGen, EQA) }) + Law("Setter law: set is idempotent") { setter.setIdempotent(aGen, bGen, EQA) }, + Law("Setter law: modify identity") { setter.modifyIdentity(aGen, EQA) }, + Law("Setter law: compose modify") { setter.composeModify(aGen, EQA, funcGen) }, + Law("Setter law: consistent set modify") { setter.consistentSetModify(aGen, bGen, EQA) } ) - fun Setter.setIdempotent(aGen: Gen, bGen: Gen, EQA: Eq): Unit = forAll(aGen, bGen, { a, b -> + fun Setter.setIdempotent(aGen: Gen, bGen: Gen, EQA: Eq): Unit = forAll(aGen, bGen) { a, b -> set(set(a, b), b).equalUnderTheLaw(set(a, b), EQA) - }) + } - fun Setter.modifyIdentity(aGen: Gen, EQA: Eq): Unit = forAll(aGen, { a -> + fun Setter.modifyIdentity(aGen: Gen, EQA: Eq): Unit = forAll(aGen) { a -> modify(a, ::identity).equalUnderTheLaw(a, EQA) - }) + } - fun Setter.composeModify(aGen: Gen, EQA: Eq, funcGen: Gen<(B) -> B>): Unit = forAll(aGen, funcGen, funcGen, { a, f, g -> + fun Setter.composeModify(aGen: Gen, EQA: Eq, funcGen: Gen<(B) -> B>): Unit = forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g).equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } - fun Setter.consistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq): Unit = forAll(aGen, bGen, { a, b -> + fun Setter.consistentSetModify(aGen: Gen, bGen: Gen, EQA: Eq): Unit = forAll(aGen, bGen) { a, b -> modify(a) { b }.equalUnderTheLaw(set(a, b), EQA) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ShowLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ShowLaws.kt index 0c270f9c903..4a0b166e566 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ShowLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/ShowLaws.kt @@ -9,14 +9,14 @@ object ShowLaws { inline fun laws(S: Show, EQ: Eq, noinline cf: (Int) -> F): List = EqLaws.laws(EQ, cf) + listOf( - Law("Show Laws: equality", { S.equalShow(EQ, cf) }) + Law("Show Laws: equality") { S.equalShow(EQ, cf) } ) fun Show.equalShow(EQ: Eq, cf: (Int) -> F): Unit = - forAll(Gen.int(), { int: Int -> + forAll(Gen.int()) { int: Int -> val a = cf(int) val b = cf(int) EQ.run { a.eqv(b) } && a.show() == b.show() - }) + } } \ No newline at end of file diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraversalLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraversalLaws.kt index 9457ef59492..1e61e0f36a4 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraversalLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraversalLaws.kt @@ -13,39 +13,39 @@ import io.kotlintest.properties.forAll object TraversalLaws { inline fun laws(traversal: Traversal, aGen: Gen, bGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq, EQOptionB: Eq>, EQListB: Eq>) = listOf( - Law("Traversal law: head option", { traversal.headOption(aGen, EQOptionB) }), - Law("Traversal law: modify get all", { traversal.modifyGetAll(aGen, funcGen, EQListB) }), - Law("Traversal law: set is idempotent", { traversal.setIdempotent(aGen, bGen, EQA) }), - Law("Traversal law: modify identity", { traversal.modifyIdentity(aGen, EQA) }), - Law("Traversal law: compose modify", { traversal.composeModify(aGen, funcGen, EQA) }) + Law("Traversal law: head option") { traversal.headOption(aGen, EQOptionB) }, + Law("Traversal law: modify get all") { traversal.modifyGetAll(aGen, funcGen, EQListB) }, + Law("Traversal law: set is idempotent") { traversal.setIdempotent(aGen, bGen, EQA) }, + Law("Traversal law: modify identity") { traversal.modifyIdentity(aGen, EQA) }, + Law("Traversal law: compose modify") { traversal.composeModify(aGen, funcGen, EQA) } ) fun Traversal.headOption(aGen: Gen, EQOptionB: Eq>): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> headOption(a) .equalUnderTheLaw(getAll(a).firstOrNull().toOption(), EQOptionB) - }) + } fun Traversal.modifyGetAll(aGen: Gen, funcGen: Gen<(B) -> B>, EQListB: Eq>): Unit = - forAll(aGen, funcGen, { a, f -> + forAll(aGen, funcGen) { a, f -> getAll(modify(a, f)) .equalUnderTheLaw(getAll(a).map(f), EQListB) - }) + } fun Traversal.setIdempotent(aGen: Gen, bGen: Gen, EQA: Eq): Unit = - forAll(aGen, bGen, { a, b -> + forAll(aGen, bGen) { a, b -> set(set(a, b), b) .equalUnderTheLaw(set(a, b), EQA) - }) + } fun Traversal.modifyIdentity(aGen: Gen, EQA: Eq): Unit = - forAll(aGen, { a -> + forAll(aGen) { a -> modify(a, ::identity).equalUnderTheLaw(a, EQA) - }) + } fun Traversal.composeModify(aGen: Gen, funcGen: Gen<(B) -> B>, EQA: Eq): Unit = - forAll(aGen, funcGen, funcGen, { a, f, g -> + forAll(aGen, funcGen, funcGen) { a, f, g -> modify(modify(a, f), g) .equalUnderTheLaw(modify(a, g compose f), EQA) - }) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseFilterLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseFilterLaws.kt index 3c10ccc76d1..c4530339907 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseFilterLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseFilterLaws.kt @@ -17,17 +17,17 @@ object TraverseFilterLaws { //FIXME(paco): TraverseLaws cannot receive AP::just due to a crash caused by the inliner. Check in TraverseLaws why. inline fun laws(TF: TraverseFilter, GA: Applicative, noinline cf: (Int) -> Kind, EQ: Eq>, EQ_NESTED: Eq>> = Eq.any()): List = TraverseLaws.laws(TF, GA, cf, EQ) + listOf( - Law("TraverseFilter Laws: Identity", { TF.identityTraverseFilter(GA, EQ_NESTED) }), - Law("TraverseFilter Laws: filterA consistent with TraverseFilter", { TF.filterAconsistentWithTraverseFilter(GA, EQ_NESTED) }) + Law("TraverseFilter Laws: Identity") { TF.identityTraverseFilter(GA, EQ_NESTED) }, + Law("TraverseFilter Laws: filterA consistent with TraverseFilter") { TF.filterAconsistentWithTraverseFilter(GA, EQ_NESTED) } ) fun TraverseFilter.identityTraverseFilter(GA: Applicative, EQ: Eq>> = Eq.any()) = - forAll(genApplicative(genIntSmall(), GA), { fa: Kind -> - fa.traverseFilter(GA, { GA.just(Some(it)) }).equalUnderTheLaw(GA.just(fa), EQ) - }) + forAll(genApplicative(genIntSmall(), GA)) { fa: Kind -> + fa.traverseFilter(GA) { GA.just(Some(it)) }.equalUnderTheLaw(GA.just(fa), EQ) + } fun TraverseFilter.filterAconsistentWithTraverseFilter(GA: Applicative, EQ: Eq>> = Eq.any()) = - forAll(genApplicative(genIntSmall(), GA), genFunctionAToB(genApplicative(Gen.bool(), GA)), { fa: Kind, f: (Int) -> Kind -> - fa.filterA(f, GA).equalUnderTheLaw(fa.traverseFilter(GA, { a -> f(a).map { b: Boolean -> if (b) Some(a) else None } }), EQ) - }) + forAll(genApplicative(genIntSmall(), GA), genFunctionAToB(genApplicative(Gen.bool(), GA))) { fa: Kind, f: (Int) -> Kind -> + fa.filterA(f, GA).equalUnderTheLaw(fa.traverseFilter(GA) { a -> f(a).map { b: Boolean -> if (b) Some(a) else None } }, EQ) + } } diff --git a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseLaws.kt b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseLaws.kt index acaa2e95edc..7368d61bcb8 100644 --- a/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseLaws.kt +++ b/modules/core/arrow-test/src/main/kotlin/arrow/test/laws/TraverseLaws.kt @@ -37,30 +37,30 @@ object TraverseLaws { inline fun laws(TF: Traverse, FF: Functor, noinline cf: (Int) -> Kind, EQ: Eq>): List = FoldableLaws.laws(TF, cf, Eq.any()) + FunctorLaws.laws(FF, cf, EQ) + listOf( - Law("Traverse Laws: Identity", { TF.identityTraverse(FF, cf, EQ) }), - Law("Traverse Laws: Sequential composition", { TF.sequentialComposition(cf, EQ) }), - Law("Traverse Laws: Parallel composition", { TF.parallelComposition(cf, EQ) }), - Law("Traverse Laws: FoldMap derived", { TF.foldMapDerived(cf) }) + Law("Traverse Laws: Identity") { TF.identityTraverse(FF, cf, EQ) }, + Law("Traverse Laws: Sequential composition") { TF.sequentialComposition(cf, EQ) }, + Law("Traverse Laws: Parallel composition") { TF.parallelComposition(cf, EQ) }, + Law("Traverse Laws: FoldMap derived") { TF.foldMapDerived(cf) } ) fun Traverse.identityTraverse(FF: Functor, cf: (Int) -> Kind, EQ: Eq>) = Id.applicative().run { - forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf), { f: (Int) -> Kind, fa: Kind -> + forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf)) { f: (Int) -> Kind, fa: Kind -> fa.traverse(this, f).value().equalUnderTheLaw(FF.run { fa.map(f).map { it.value() } }, EQ) - }) + } } fun Traverse.sequentialComposition(cf: (Int) -> Kind, EQ: Eq>) = Id.applicative().run { - forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf), { f: (Int) -> Kind, g: (Int) -> Kind, fha: Kind -> + forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf)) { f: (Int) -> Kind, g: (Int) -> Kind, fha: Kind -> val fa = fha.traverse(this, f).fix() - val composed = fa.map({ it.traverse(this, g) }).value.value() - val expected = fha.traverse(ComposedApplicative(this, this), { a: Int -> f(a).map(g).nest() }).unnest().value().value() + val composed = fa.map { it.traverse(this, g) }.value.value() + val expected = fha.traverse(ComposedApplicative(this, this)) { a: Int -> f(a).map(g).nest() }.unnest().value().value() composed.equalUnderTheLaw(expected, EQ) - }) + } } fun Traverse.parallelComposition(cf: (Int) -> Kind, EQ: Eq>) = - forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf), { f: (Int) -> Kind, g: (Int) -> Kind, fha: Kind -> + forAll(genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genFunctionAToB>(genConstructor(genIntSmall(), ::Id)), genConstructor(genIntSmall(), cf)) { f: (Int) -> Kind, g: (Int) -> Kind, fha: Kind -> val TIA = object : Applicative { override fun just(a: A): Kind = TIC(Id(a) toT Id(a)) @@ -79,16 +79,16 @@ object TraverseLaws { } } - val seen: TI> = fha.traverse(TIA, { TIC(f(it) toT g(it)) }).fix().ti + val seen: TI> = fha.traverse(TIA) { TIC(f(it) toT g(it)) }.fix().ti val expected: TI> = TIC(fha.traverse(Id.applicative(), f) toT fha.traverse(Id.applicative(), g)).ti seen.equalUnderTheLaw(expected, TIEQ) - }) + } fun Traverse.foldMapDerived(cf: (Int) -> Kind) = - forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf), { f: (Int) -> Int, fa: Kind -> - val traversed = fa.traverse(Const.applicative(Int.monoid()), { a -> f(a).const() }).value() + forAll(genFunctionAToB(genIntSmall()), genConstructor(genIntSmall(), cf)) { f: (Int) -> Int, fa: Kind -> + val traversed = fa.traverse(Const.applicative(Int.monoid())) { a -> f(a).const() }.value() val mapped = fa.foldMap(Int.monoid(), f) mapped.equalUnderTheLaw(traversed, Eq.any()) - }) + } } diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Applicative.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Applicative.kt index 0d578eafbd6..7e6c4843de8 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Applicative.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Applicative.kt @@ -25,20 +25,20 @@ interface Applicative : Functor { fun Kind>.product( other: Kind, dummyImplicit: Any? = null): Kind> = - other.product(this).map({ Tuple3(it.b.a, it.b.b, it.a) }) + other.product(this).map { Tuple3(it.b.a, it.b.b, it.a) } fun Kind>.product( other: Kind, dummyImplicit: Any? = null, dummyImplicit2: Any? = null): Kind> = - other.product(this).map({ Tuple4(it.b.a, it.b.b, it.b.c, it.a) }) + other.product(this).map { Tuple4(it.b.a, it.b.b, it.b.c, it.a) } fun Kind>.product( other: Kind, dummyImplicit: Any? = null, dummyImplicit2: Any? = null, dummyImplicit3: Any? = null): Kind> = - other.product(this).map({ Tuple5(it.b.a, it.b.b, it.b.c, it.b.d, it.a) }) + other.product(this).map { Tuple5(it.b.a, it.b.b, it.b.c, it.b.d, it.a) } fun Kind>.product( other: Kind, @@ -46,7 +46,7 @@ interface Applicative : Functor { dummyImplicit2: Any? = null, dummyImplicit3: Any? = null, dummyImplicit4: Any? = null): Kind> = - other.product(this).map({ Tuple6(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.a) }) + other.product(this).map { Tuple6(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.a) } fun Kind>.product( other: Kind, @@ -55,7 +55,7 @@ interface Applicative : Functor { dummyImplicit3: Any? = null, dummyImplicit4: Any? = null, dummyImplicit5: Any? = null): Kind> = - other.product(this).map({ Tuple7(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.a) }) + other.product(this).map { Tuple7(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.a) } fun Kind>.product( other: Kind, @@ -65,7 +65,7 @@ interface Applicative : Functor { dummyImplicit4: Any? = null, dummyImplicit5: Any? = null, dummyImplicit6: Any? = null): Kind> = - other.product(this).map({ Tuple8(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.a) }) + other.product(this).map { Tuple8(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.a) } fun Kind>.product( other: Kind, @@ -76,7 +76,7 @@ interface Applicative : Functor { dummyImplicit5: Any? = null, dummyImplicit6: Any? = null, dummyImplicit7: Any? = null): Kind> = - other.product(this).map({ Tuple9(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.b.h, it.a) }) + other.product(this).map { Tuple9(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.b.h, it.a) } fun Kind>.product( other: Kind, @@ -88,7 +88,7 @@ interface Applicative : Functor { dummyImplicit6: Any? = null, dummyImplicit7: Any? = null, dummyImplicit9: Any? = null): Kind> = - other.product(this).map({ Tuple10(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.b.h, it.b.i, it.a) }) + other.product(this).map { Tuple10(it.b.a, it.b.b, it.b.c, it.b.d, it.b.e, it.b.f, it.b.g, it.b.h, it.b.i, it.a) } fun tupled( a: Kind, diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Comonad.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Comonad.kt index a23c239d704..2f28879bb1f 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Comonad.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Comonad.kt @@ -36,11 +36,11 @@ open class ComonadContinuation(CM: Comonad, override val context: suspend fun extract(m: () -> Kind): B = suspendCoroutineOrReturn { c -> val labelHere = c.stateStack // save the whole coroutine stack labels - returnedMonad = m().coflatMap({ x: Kind -> + returnedMonad = m().coflatMap { x: Kind -> c.stateStack = labelHere c.resume(x.extract()) returnedMonad - }).extract() + }.extract() COROUTINE_SUSPENDED } } diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Monad.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Monad.kt index 399f355fed3..406e61538a2 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Monad.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Monad.kt @@ -14,10 +14,10 @@ interface Monad : Applicative { fun tailRecM(a: A, f: (A) -> Kind>): Kind override fun Kind.map(f: (A) -> B): Kind = - flatMap({ a -> just(f(a)) }) + flatMap { a -> just(f(a)) } override fun Kind.ap(ff: Kind B>): Kind = - ff.flatMap({ f -> this.map(f) }) + ff.flatMap { f -> this.map(f) } fun Kind>.flatten(): Kind = flatMap(::identity) @@ -29,13 +29,13 @@ interface Monad : Applicative { flatMap { fb.value() } fun Kind.forEffect(fb: Kind): Kind = - flatMap { a -> fb.map({ a }) } + flatMap { a -> fb.map { a } } fun Kind.forEffectEval(fb: Eval>): Kind = - flatMap { a -> fb.value().map({ a }) } + flatMap { a -> fb.value().map { a } } fun Kind.mproduct(f: (A) -> Kind): Kind> = - flatMap { a -> f(a).map({ Tuple2(a, it) }) } + flatMap { a -> f(a).map { Tuple2(a, it) } } fun Kind.ifM(ifTrue: () -> Kind, ifFalse: () -> Kind): Kind = flatMap { if (it) ifTrue() else ifFalse() } diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadContinuations.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadContinuations.kt index a421a3a5476..d822b56efd7 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadContinuations.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadContinuations.kt @@ -54,32 +54,32 @@ open class MonadContinuation(M: Monad, override val context: CoroutineC open suspend fun bind(m: () -> Kind): B = suspendCoroutineOrReturn { c -> val labelHere = c.stateStack // save the whole coroutine stack labels - returnedMonad = m().flatMap({ x: B -> + returnedMonad = m().flatMap { x: B -> c.stateStack = labelHere c.resume(x) returnedMonad - }) + } COROUTINE_SUSPENDED } open suspend fun bindIn(context: CoroutineContext, m: () -> B): B = suspendCoroutineOrReturn { c -> val labelHere = c.stateStack // save the whole coroutine stack labels val monadCreation: suspend () -> Kind = { - just(m()).flatMap({ xx: B -> + just(m()).flatMap { xx: B -> c.stateStack = labelHere c.resume(xx) returnedMonad - }) + } } val completion = bindingInContextContinuation(context) - returnedMonad = just(Unit).flatMap({ + returnedMonad = just(Unit).flatMap { monadCreation.startCoroutine(completion) val error = completion.await() if (error != null) { throw error } returnedMonad - }) + } COROUTINE_SUSPENDED } diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt index 06e1b1f18bb..35f7d764e54 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/MonadError.kt @@ -6,10 +6,10 @@ import kotlin.coroutines.experimental.startCoroutine interface MonadError : ApplicativeError, Monad { fun Kind.ensure(error: () -> E, predicate: (A) -> Boolean): Kind = - this.flatMap({ + this.flatMap { if (predicate(it)) just(it) else raiseError(error()) - }) + } } diff --git a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Reducible.kt b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Reducible.kt index aece88f745e..a82822a670c 100644 --- a/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Reducible.kt +++ b/modules/core/arrow-typeclasses/src/main/kotlin/arrow/typeclasses/Reducible.kt @@ -43,7 +43,7 @@ interface Reducible : Foldable { fun Kind.reduceRightTo(f: (A) -> B, g: (A, Eval) -> Eval): Eval override fun Kind.reduceRightToOption(f: (A) -> B, g: (A, Eval) -> Eval): Eval> = - reduceRightTo(f, g).map({ Some(it) }) + reduceRightTo(f, g).map { Some(it) } override fun Kind.isEmpty(): Boolean = false @@ -53,7 +53,7 @@ interface Reducible : Foldable { * Reduce a F value using the given Semigroup. */ fun Kind.reduce(SG: Semigroup): A = SG.run { - reduceLeft({ a, b -> a.combine(b) }) + reduceLeft { a, b -> a.combine(b) } } /** @@ -69,7 +69,7 @@ interface Reducible : Foldable { * Apply f to each element of fa and combine them using the given Semigroup. */ fun Kind.reduceMap(SG: Semigroup, f: (A) -> B): B = SG.run { - reduceLeftTo(f, { b, a -> b.combine(f(a)) }) + reduceLeftTo(f) { b, a -> b.combine(f(a)) } } } @@ -90,20 +90,20 @@ interface NonEmptyReducible : Reducible { } override fun Kind.foldRight(lb: Eval, f: (A, Eval) -> Eval): Eval = FG().run { - Eval.Always({ split() }).flatMap { (a, ga) -> f(a, ga.foldRight(lb, f)) } + Eval.Always { split() }.flatMap { (a, ga) -> f(a, ga.foldRight(lb, f)) } } override fun Kind.reduceLeftTo(f: (A) -> B, g: (B, A) -> B): B = FG().run { val (a, ga) = split() - ga.foldLeft(f(a), { bb, aa -> g(bb, aa) }) + ga.foldLeft(f(a)) { bb, aa -> g(bb, aa) } } override fun Kind.reduceRightTo(f: (A) -> B, g: (A, Eval) -> Eval): Eval = FG().run { - Eval.Always({ split() }).flatMap { (a, ga) -> + Eval.Always { split() }.flatMap { (a, ga) -> ga.reduceRightToOption(f, g).flatMap { option -> when (option) { is Some -> g(a, Eval.Now(option.t)) - is None -> Eval.Later({ f(a) }) + is None -> Eval.Later { f(a) } } } } @@ -142,6 +142,6 @@ interface NonEmptyReducible : Reducible { fun Kind.foldM_(M: Monad, z: B, f: (B, A) -> Kind): Kind = M.run { val (a, ga) = split() - return f(z, a).flatMap({ FG().run { ga.foldM(M, it, f) } }) + return f(z, a).flatMap { FG().run { ga.foldM(M, it, f) } } } } diff --git a/modules/docs/arrow-examples/src/test/kotlin/arrow/DataTypeExamples.kt b/modules/docs/arrow-examples/src/test/kotlin/arrow/DataTypeExamples.kt index 27949c74593..84458e63167 100644 --- a/modules/docs/arrow-examples/src/test/kotlin/arrow/DataTypeExamples.kt +++ b/modules/docs/arrow-examples/src/test/kotlin/arrow/DataTypeExamples.kt @@ -130,7 +130,7 @@ class DataTypeExamples : FreeSpec() { init { "Functor" { // Transforming the value, if the computation is a success: - val actual = Try.functor().run { Try { "3".toInt() }.map({ it + 1 }) } + val actual = Try.functor().run { Try { "3".toInt() }.map { it + 1 } } actual shouldBe Try.Success(4) } diff --git a/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeK.kt b/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeK.kt index 379cb9a77bc..df4b518cc1f 100644 --- a/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeK.kt +++ b/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeK.kt @@ -65,7 +65,7 @@ data class MaybeK(val maybe: Maybe) : MaybeKOf, MaybeKKindedJ { Maybe.defer { fa().value() }.k() fun async(fa: Proc): MaybeK = - Maybe.create({ emitter: MaybeEmitter -> + Maybe.create { emitter: MaybeEmitter -> fa { either: Either -> either.fold({ emitter.onError(it) @@ -74,7 +74,7 @@ data class MaybeK(val maybe: Maybe) : MaybeKOf, MaybeKKindedJ { }) } - }).k() + }.k() tailrec fun tailRecM(a: A, f: (A) -> MaybeKOf>): MaybeK { val either = f(a).fix().value().blockingGet() diff --git a/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/SingleK.kt b/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/SingleK.kt index a36a3ed44f7..1fe2e08743e 100644 --- a/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/SingleK.kt +++ b/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/SingleK.kt @@ -49,7 +49,7 @@ data class SingleK(val single: Single) : SingleKOf, SingleKKindedJ { Single.defer { fa().value() }.k() fun async(fa: Proc): SingleK = - Single.create({ emitter: SingleEmitter -> + Single.create { emitter: SingleEmitter -> fa { either: Either -> either.fold({ emitter.onError(it) @@ -58,7 +58,7 @@ data class SingleK(val single: Single) : SingleKOf, SingleKKindedJ { }) } - }).k() + }.k() tailrec fun tailRecM(a: A, f: (A) -> SingleKOf>): SingleK { val either = f(a).fix().value().blockingGet() diff --git a/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/IOParallel.kt b/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/IOParallel.kt index daf8c2119af..93e084f3e45 100644 --- a/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/IOParallel.kt +++ b/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/IOParallel.kt @@ -7,46 +7,46 @@ import arrow.effects.internal.parMap3 import kotlin.coroutines.experimental.CoroutineContext fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, f: (A, B) -> C): IO = - IO.async(IO.effect().parMap2(ctx, ioA, ioB, f, /* see parMap2 notes on this parameter */ { it.fix().unsafeRunSync() })) + IO.async(IO.effect().parMap2(ctx, ioA, ioB, f /* see parMap2 notes on this parameter */) { it.fix().unsafeRunSync() }) fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, f: (A, B, C) -> D): IO = - IO.async(IO.effect().parMap3(ctx, ioA, ioB, ioC, f, /* see parMap2 notes on this parameter */ { it.fix().unsafeRunSync() })) + IO.async(IO.effect().parMap3(ctx, ioA, ioB, ioC, f /* see parMap2 notes on this parameter */) { it.fix().unsafeRunSync() }) fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, f: (A, B, C, D) -> E): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ::Tuple2), - parallelMapN(ctx, ioC, ioD, ::Tuple2), - { ab, cd -> f(ab.a, ab.b, cd.a, cd.b) }) + parallelMapN(ctx, ioC, ioD, ::Tuple2) + ) { ab, cd -> f(ab.a, ab.b, cd.a, cd.b) } fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, ioE: IO, f: (A, B, C, D, E) -> F): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ioC, ::Tuple3), - parallelMapN(ctx, ioD, ioE, ::Tuple2), - { abc, de -> f(abc.a, abc.b, abc.c, de.a, de.b) }) + parallelMapN(ctx, ioD, ioE, ::Tuple2) + ) { abc, de -> f(abc.a, abc.b, abc.c, de.a, de.b) } fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, ioE: IO, ioF: IO, f: (A, B, C, D, E, F) -> G): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ioC, ::Tuple3), - parallelMapN(ctx, ioD, ioE, ioF, ::Tuple3), - { abc, def -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c) }) + parallelMapN(ctx, ioD, ioE, ioF, ::Tuple3) + ) { abc, def -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c) } fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, ioE: IO, ioF: IO, ioG: IO, f: (A, B, C, D, E, F, G) -> H): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ioC, ::Tuple3), parallelMapN(ctx, ioD, ioE, ::Tuple2), - parallelMapN(ctx, ioF, ioG, ::Tuple2), - { abc, de, fg -> f(abc.a, abc.b, abc.c, de.a, de.b, fg.a, fg.b) }) + parallelMapN(ctx, ioF, ioG, ::Tuple2) + ) { abc, de, fg -> f(abc.a, abc.b, abc.c, de.a, de.b, fg.a, fg.b) } fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, ioE: IO, ioF: IO, ioG: IO, ioH: IO, f: (A, B, C, D, E, F, G, H) -> I): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ioC, ::Tuple3), parallelMapN(ctx, ioD, ioE, ioF, ::Tuple3), - parallelMapN(ctx, ioG, ioH, ::Tuple2), - { abc, def, gh -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c, gh.a, gh.b) }) + parallelMapN(ctx, ioG, ioH, ::Tuple2) + ) { abc, def, gh -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c, gh.a, gh.b) } fun IO.Companion.parallelMapN(ctx: CoroutineContext, ioA: IO, ioB: IO, ioC: IO, ioD: IO, ioE: IO, ioF: IO, ioG: IO, ioH: IO, ioI: IO, f: (A, B, C, D, E, F, G, H, I) -> J): IO = parallelMapN(ctx, parallelMapN(ctx, ioA, ioB, ioC, ::Tuple3), parallelMapN(ctx, ioD, ioE, ioF, ::Tuple3), - parallelMapN(ctx, ioG, ioH, ioI, ::Tuple3), - { abc, def, ghi -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c, ghi.a, ghi.b, ghi.c) }) + parallelMapN(ctx, ioG, ioH, ioI, ::Tuple3) + ) { abc, def, ghi -> f(abc.a, abc.b, abc.c, def.a, def.b, def.c, ghi.a, ghi.b, ghi.c) } diff --git a/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/typeclasses/MonadSuspendCancellableContinuations.kt b/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/typeclasses/MonadSuspendCancellableContinuations.kt index bc02ee280be..ed9dbafa817 100644 --- a/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/typeclasses/MonadSuspendCancellableContinuations.kt +++ b/modules/effects/arrow-effects/src/main/kotlin/arrow/effects/typeclasses/MonadSuspendCancellableContinuations.kt @@ -37,14 +37,14 @@ open class MonadDeferCancellableContinuation(SC: MonadDefer, override v override suspend fun bind(m: () -> Kind): B = suspendCoroutineOrReturn { c -> val labelHere = c.stateStack // save the whole coroutine stack labels - returnedMonad = m().flatMap({ x: B -> + returnedMonad = m().flatMap { x: B -> c.stateStack = labelHere if (cancelled.get()) { throw BindingCancellationException() } c.resume(x) returnedMonad - }) + } COROUTINE_SUSPENDED } @@ -56,24 +56,24 @@ open class MonadDeferCancellableContinuation(SC: MonadDefer, override v } catch (t: Throwable) { ME.raiseError(t) } - datatype.flatMap({ xx: B -> + datatype.flatMap { xx: B -> c.stateStack = labelHere if (cancelled.get()) { throw BindingCancellationException() } c.resume(xx) returnedMonad - }) + } } val completion = bindingInContextContinuation(context) - returnedMonad = just(Unit).flatMap({ + returnedMonad = just(Unit).flatMap { monadCreation.startCoroutine(completion) val error = completion.await() if (error != null) { throw error } returnedMonad - }) + } COROUTINE_SUSPENDED } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Fold.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Fold.kt index d0e29027681..c00cc889be5 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Fold.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Fold.kt @@ -71,7 +71,7 @@ interface Fold : FoldOf { /** * Check if there is no target */ - fun isEmpty(s: S): Boolean = foldMap(AndMonoid, s, { _ -> false }) + fun isEmpty(s: S): Boolean = foldMap(AndMonoid, s) { _ -> false } /** * Check if there is at least one target @@ -81,12 +81,12 @@ interface Fold : FoldOf { /** * Get the first target */ - fun headOption(s: S): Option = foldMap(firstOptionMonoid(), s, { b -> Const(Some(b)) }).value + fun headOption(s: S): Option = foldMap(firstOptionMonoid(), s) { b -> Const(Some(b)) }.value /** * Get the last target */ - fun lastOption(s: S): Option = foldMap(lastOptionMonoid(), s, { b -> Const(Some(b)) }).value + fun lastOption(s: S): Option = foldMap(lastOptionMonoid(), s) { b -> Const(Some(b)) }.value /** * Fold using the given [Monoid] instance. @@ -101,7 +101,7 @@ interface Fold : FoldOf { /** * Get all targets of the [Fold] */ - fun getAll(s: S): ListK = foldMap(ListK.monoid(), s, { ListK.just(it) }) + fun getAll(s: S): ListK = foldMap(ListK.monoid(), s) { ListK.just(it) } /** * Join two [Fold] with the same target @@ -116,7 +116,7 @@ interface Fold : FoldOf { */ fun left(): Fold, Either> = object : Fold, Either> { override fun foldMap(M: Monoid, s: Either, f: (Either) -> R): R = - s.fold({ a1: S -> this@Fold.foldMap(M, a1, { b -> f(Either.Left(b)) }) }, { c -> f(Either.Right(c)) }) + s.fold({ a1: S -> this@Fold.foldMap(M, a1) { b -> f(Either.Left(b)) } }, { c -> f(Either.Right(c)) }) } /** @@ -124,7 +124,7 @@ interface Fold : FoldOf { */ fun right(): Fold, Either> = object : Fold, Either> { override fun foldMap(M: Monoid, s: Either, f: (Either) -> R): R = - s.fold({ c -> f(Either.Left(c)) }, { a1 -> this@Fold.foldMap(M, a1, { b -> f(Either.Right(b)) }) }) + s.fold({ c -> f(Either.Left(c)) }, { a1 -> this@Fold.foldMap(M, a1) { b -> f(Either.Right(b)) } }) } /** @@ -132,7 +132,7 @@ interface Fold : FoldOf { */ infix fun compose(other: Fold): Fold = object : Fold { override fun foldMap(M: Monoid, s: S, f: (C) -> R): R = - this@Fold.foldMap(M, s, { c -> other.foldMap(M, c, f) }) + this@Fold.foldMap(M, s) { c -> other.foldMap(M, c, f) } } /** @@ -186,7 +186,7 @@ interface Fold : FoldOf { * Find the first element matching the predicate, if one exists. */ fun find(s: S, p: (A) -> Boolean): Option = - foldMap(firstOptionMonoid(), s, { b -> (if (p(b)) Const(Some(b)) else Const(None)) }).value + foldMap(firstOptionMonoid(), s) { b -> (if (p(b)) Const(Some(b)) else Const(None)) }.value /** * Check whether at least one element satisfies the predicate. diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Iso.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Iso.kt index 470c3c5f0f7..8d909f4136c 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Iso.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Iso.kt @@ -220,7 +220,7 @@ interface PIso : PIsoOf { /** * View a [PIso] as a [PLens] */ - fun asLens(): PLens = PLens(this::get, { b -> { _ -> set(b) } }) + fun asLens(): PLens = PLens(this::get) { b -> { _ -> set(b) } } /** * View a [PIso] as a [Getter] diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Lens.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Lens.kt index c6c2bf6072b..3f5f827b8f5 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Lens.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Lens.kt @@ -67,7 +67,7 @@ interface PLens : PLensOf { * Modify the focus of a [PLens] using Functor function */ fun modifyF(FF: Functor, s: S, f: (A) -> Kind): Kind = FF.run { - f(get(s)).map({ b -> set(s, b) }) + f(get(s)).map { b -> set(s, b) } } /** @@ -200,7 +200,7 @@ interface PLens : PLensOf { */ fun asTraversal(): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.run { - f(get(s)).map({ b -> this@PLens.set(s, b) }) + f(get(s)).map { b -> this@PLens.set(s, b) } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Optional.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Optional.kt index fdc38f9e668..9d48fc0753a 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Optional.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Optional.kt @@ -91,9 +91,8 @@ interface POptional : POptionalOf { */ fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.run { getOrModify(s).fold( - ::just, - { f(it).map({ set(s, it) }) } - ) + ::just + ) { f(it).map { set(s, it) } } } /** @@ -236,7 +235,7 @@ interface POptional : POptionalOf { /** * Modify the focus of a [POptional] with a function [f] */ - fun modify(s: S, f: (A) -> B): T = getOrModify(s).fold(::identity, { a -> set(s, f(a)) }) + fun modify(s: S, f: (A) -> B): T = getOrModify(s).fold(::identity) { a -> set(s, f(a)) } /** * Lift a function [f]: `(A) -> B to the context of `S`: `(S) -> T` @@ -247,7 +246,7 @@ interface POptional : POptionalOf { * Modify the focus of a [POptional] with a function [f] * @return [Option.None] if the [POptional] is not matching */ - fun modifiyOption(s: S, f: (A) -> B): Option = getOption(s).map({ set(s, f(it)) }) + fun modifiyOption(s: S, f: (A) -> B): Option = getOption(s).map { set(s, f(it)) } /** * Find the focus that satisfies the predicate [p] diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Prism.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Prism.kt index d9c8476a123..1c55769c799 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Prism.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Prism.kt @@ -87,9 +87,8 @@ interface PPrism : PPrismOf { */ fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.run { getOrModify(s).fold( - ::just, - { f(it).map(::reverseGet) } - ) + ::just + ) { f(it).map(::reverseGet) } } /** @@ -98,9 +97,8 @@ interface PPrism : PPrismOf { fun liftF(FA: Applicative, f: (A) -> Kind): (S) -> Kind = FA.run { { s -> getOrModify(s).fold( - ::just, - { f(it).map(::reverseGet) } - ) + ::just + ) { f(it).map(::reverseGet) } } } @@ -204,9 +202,8 @@ interface PPrism : PPrismOf { * View a [PPrism] as an [POptional] */ fun asOptional(): POptional = POptional( - this::getOrModify, - { b -> { s -> set(s, b) } } - ) + this::getOrModify + ) { b -> { s -> set(s, b) } } /** * View a [PPrism] as a [PSetter] @@ -226,9 +223,8 @@ interface PPrism : PPrismOf { fun asTraversal(): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.run { getOrModify(s).fold( - ::just, - { f(it).map(this@PPrism::reverseGet) } - ) + ::just + ) { f(it).map(this@PPrism::reverseGet) } } } } @@ -236,12 +232,12 @@ interface PPrism : PPrismOf { /** * Modify the focus of a [PPrism] with a function */ -inline fun PPrism.modify(s: S, crossinline f: (A) -> B): T = getOrModify(s).fold(::identity, { a -> reverseGet(f(a)) }) +inline fun PPrism.modify(s: S, crossinline f: (A) -> B): T = getOrModify(s).fold(::identity) { a -> reverseGet(f(a)) } /** * Lift a function [f]: `(A) -> B to the context of `S`: `(S) -> T` */ -inline fun PPrism.lift(crossinline f: (A) -> B): (S) -> T = { s -> getOrModify(s).fold(::identity, { a -> reverseGet(f(a)) }) } +inline fun PPrism.lift(crossinline f: (A) -> B): (S) -> T = { s -> getOrModify(s).fold(::identity) { a -> reverseGet(f(a)) } } /** * Modify the focus of a [PPrism] with a function diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt index 185560d3401..9ecbde1cd56 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/Traversal.kt @@ -39,7 +39,7 @@ interface PTraversal : PTraversalOf { fun codiagonal(): Traversal, S> = object : Traversal, S> { override fun modifyF(FA: Applicative, s: Either, f: (S) -> Kind): Kind> = FA.run { - s.bimap(f, f).fold({ fa -> fa.map({ a -> Either.Left(a) }) }, { fa -> fa.map({ a -> Either.Right(a) }) }) + s.bimap(f, f).fold({ fa -> fa.map { a -> Either.Left(a) } }, { fa -> fa.map { a -> Either.Right(a) } }) } } @@ -62,9 +62,8 @@ interface PTraversal : PTraversalOf { operator fun invoke(get1: (S) -> A, get2: (S) -> A, set: (B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), - { (b1, b2) -> set(b1, b2, s) } - ) + f(get1(s)), f(get2(s)) + ) { (b1, b2) -> set(b1, b2, s) } } operator fun invoke( @@ -74,9 +73,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), - { (b1, b2, b3) -> set(b1, b2, b3, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)) + ) { (b1, b2, b3) -> set(b1, b2, b3, s) } } operator fun invoke( @@ -87,9 +85,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), - { (b1, b2, b3, b4) -> set(b1, b2, b3, b4, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)) + ) { (b1, b2, b3, b4) -> set(b1, b2, b3, b4, s) } } operator fun invoke( @@ -101,9 +98,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), - { (b1, b2, b3, b4, b5) -> set(b1, b2, b3, b4, b5, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)) + ) { (b1, b2, b3, b4, b5) -> set(b1, b2, b3, b4, b5, s) } } operator fun invoke( @@ -116,9 +112,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), - { (b1, b2, b3, b4, b5, b6) -> set(b1, b2, b3, b4, b5, b6, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)) + ) { (b1, b2, b3, b4, b5, b6) -> set(b1, b2, b3, b4, b5, b6, s) } } operator fun invoke( @@ -132,9 +127,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), - { (b1, b2, b3, b4, b5, b6, b7) -> set(b1, b2, b3, b4, b5, b6, b7, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)) + ) { (b1, b2, b3, b4, b5, b6, b7) -> set(b1, b2, b3, b4, b5, b6, b7, s) } } operator fun invoke( @@ -149,9 +143,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)), - { (b1, b2, b3, b4, b5, b6, b7, b8) -> set(b1, b2, b3, b4, b5, b6, b7, b8, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)) + ) { (b1, b2, b3, b4, b5, b6, b7, b8) -> set(b1, b2, b3, b4, b5, b6, b7, b8, s) } } operator fun invoke( @@ -167,9 +160,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)), f(get9(s)), - { (b1, b2, b3, b4, b5, b6, b7, b8, b9) -> set(b1, b2, b3, b4, b5, b6, b7, b8, b9, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)), f(get9(s)) + ) { (b1, b2, b3, b4, b5, b6, b7, b8, b9) -> set(b1, b2, b3, b4, b5, b6, b7, b8, b9, s) } } operator fun invoke( @@ -186,9 +178,8 @@ interface PTraversal : PTraversalOf { set: (B, B, B, B, B, B, B, B, B, B, S) -> T): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (A) -> Kind): Kind = FA.map( - f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)), f(get9(s)), f(get10(s)), - { (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10) -> set(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, s) } - ) + f(get1(s)), f(get2(s)), f(get3(s)), f(get4(s)), f(get5(s)), f(get6(s)), f(get7(s)), f(get8(s)), f(get9(s)), f(get10(s)) + ) { (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10) -> set(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, s) } } } @@ -197,7 +188,7 @@ interface PTraversal : PTraversalOf { * Map each target to a Monoid and combine the results */ fun foldMap(M: Monoid, s: S, f: (A) -> R): R = - modifyF(Const.applicative(M, Unit), s, { b -> Const(f(b)) }).value() + modifyF(Const.applicative(M, Unit), s) { b -> Const(f(b)) }.value() /** * Fold using the given [Monoid] instance. @@ -212,7 +203,7 @@ interface PTraversal : PTraversalOf { /** * Get all foci of the [PTraversal] */ - fun getAll(s: S): ListK = foldMap(ListK.monoid(), s, { ListK(listOf(it)) }) + fun getAll(s: S): ListK = foldMap(ListK.monoid(), s) { ListK(listOf(it)) } /** * Set polymorphically the target of a [PTraversal] with a value @@ -222,12 +213,12 @@ interface PTraversal : PTraversalOf { /** * Calculate the number of targets in the [PTraversal] */ - fun size(s: S): Int = foldMap(Int.monoid(), s, { 1 }) + fun size(s: S): Int = foldMap(Int.monoid(), s) { 1 } /** * Check if there is no target */ - fun isEmpty(s: S): Boolean = foldMap(AndMonoid, s, { _ -> false }) + fun isEmpty(s: S): Boolean = foldMap(AndMonoid, s) { _ -> false } /** * Check if there is at least one target @@ -237,12 +228,12 @@ interface PTraversal : PTraversalOf { /** * Find the first target or [Option.None] if no targets */ - fun headOption(s: S): Option = foldMap(firstOptionMonoid(), s, { b -> Const(Some(b)) }).value + fun headOption(s: S): Option = foldMap(firstOptionMonoid(), s) { b -> Const(Some(b)) }.value /** * Find the first target or [Option.None] if no targets */ - fun lastOption(s: S): Option = foldMap(lastOptionMonoid(), s, { b -> Const(Some(b)) }).value + fun lastOption(s: S): Option = foldMap(lastOptionMonoid(), s) { b -> Const(Some(b)) }.value fun choice(other: PTraversal): PTraversal, Either, A, B> = object : PTraversal, Either, A, B> { override fun modifyF(FA: Applicative, s: Either, f: (A) -> Kind): Kind> = FA.run { @@ -258,7 +249,7 @@ interface PTraversal : PTraversalOf { */ infix fun compose(other: PTraversal): PTraversal = object : PTraversal { override fun modifyF(FA: Applicative, s: S, f: (C) -> Kind): Kind = - this@PTraversal.modifyF(FA, s, { b -> other.modifyF(FA, b, f) }) + this@PTraversal.modifyF(FA, s) { b -> other.modifyF(FA, b, f) } } /** @@ -318,21 +309,21 @@ interface PTraversal : PTraversalOf { /** * Find the first target matching the predicate */ - fun find(s: S, p: (A) -> Boolean): Option = foldMap(firstOptionMonoid(), s, { a -> + fun find(s: S, p: (A) -> Boolean): Option = foldMap(firstOptionMonoid(), s) { a -> if (p(a)) Const(Some(a)) else Const(None) - }).value + }.value /** * Map each target to a Monoid and combine the results */ fun foldMap(s: S, f: (A) -> R, M: Monoid): R = - modifyF(Const.applicative(M), s, { b -> Const(f(b)) }).value() + modifyF(Const.applicative(M), s) { b -> Const(f(b)) }.value() /** * Modify polymorphically the target of a [PTraversal] with a function [f] */ - fun modify(s: S, f: (A) -> B): T = modifyF(Id.applicative(), s, { b -> Id(f(b)) }).value() + fun modify(s: S, f: (A) -> B): T = modifyF(Id.applicative(), s) { b -> Id(f(b)) }.value() /** * Check whether at least one element satisfies the predicate. diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/list.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/list.kt index 4d43e28605a..9a35e22509d 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/list.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/list.kt @@ -60,9 +60,9 @@ fun ListInstances.filterIndex(): FilterIndex, Int, A> = ListFilterIn interface ListFilterIndexInstance : FilterIndex, Int, A> { override fun filter(p: (Int) -> Boolean): Traversal, A> = object : Traversal, A> { override fun modifyF(FA: Applicative, s: List, f: (A) -> Kind): Kind> = FA.run { - s.mapIndexed { index, a -> a toT index }.k().traverse(FA, { (a, j) -> + s.mapIndexed { index, a -> a toT index }.k().traverse(FA) { (a, j) -> if (p(j)) f(a) else just(a) - }).map { it.list } + }.map { it.list } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/listk.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/listk.kt index 3941caf79f0..e7db433537b 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/listk.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/listk.kt @@ -39,9 +39,9 @@ interface ListKEachInstance : Each, A> { interface ListKFilterIndexInstance : FilterIndex, Int, A> { override fun filter(p: (Int) -> Boolean): Traversal, A> = object : Traversal, A> { override fun modifyF(FA: Applicative, s: ListK, f: (A) -> Kind): Kind> = FA.run { - s.mapIndexed { index, a -> a toT index }.k().traverse(FA, { (a, j) -> + s.mapIndexed { index, a -> a toT index }.k().traverse(FA) { (a, j) -> if (p(j)) f(a) else just(a) - }) + } } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/map.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/map.kt index 13ad0e7947c..ccbab550ae9 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/map.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/map.kt @@ -85,11 +85,11 @@ fun MapInstances.filterIndex(): FilterIndex, K, V> = MapFilterI interface MapFilterIndexInstance : FilterIndex, K, V> { override fun filter(p: Predicate) = object : Traversal, V> { override fun modifyF(FA: Applicative, s: Map, f: (V) -> Kind): Kind> = FA.run { - s.toList().k().traverse(FA, { (k, v) -> + s.toList().k().traverse(FA) { (k, v) -> (if (p(k)) f(v) else just(v)).map { k to it } - }).map { it.toMap() } + }.map { it.toMap() } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/mapk.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/mapk.kt index d6569f600bb..9234951eb1a 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/mapk.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/mapk.kt @@ -57,11 +57,11 @@ interface MapKEachInstance : Each, V> { interface MapKFilterIndexInstance : FilterIndex, K, V> { override fun filter(p: (K) -> Boolean): Traversal, V> = object : Traversal, V> { override fun modifyF(FA: Applicative, s: MapK, f: (V) -> Kind): Kind> = FA.run { - s.toList().k().traverse(FA, { (k, v) -> + s.toList().k().traverse(FA) { (k, v) -> (if (p(k)) f(v) else just(v)).map { k to it } - }).map { it.toMap().k() } + }.map { it.toMap().k() } } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/sequencek.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/sequencek.kt index d3b8d26d247..daf35055547 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/sequencek.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/instances/sequencek.kt @@ -39,9 +39,9 @@ interface SequenceKEachInstance : Each, A> { interface SequenceKFilterIndexInstance : FilterIndex, Int, A> { override fun filter(p: (Int) -> Boolean): Traversal, A> = object : Traversal, A> { override fun modifyF(FA: Applicative, s: SequenceK, f: (A) -> Kind): Kind> = FA.run { - s.mapIndexed { index, a -> a toT index }.k().traverse(FA, { (a, j) -> + s.mapIndexed { index, a -> a toT index }.k().traverse(FA) { (a, j) -> if (p(j)) f(a) else just(a) - }) + } } } } diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/std/try.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/std/try.kt index 9269714f06f..cd65a88099b 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/std/try.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/std/try.kt @@ -21,7 +21,7 @@ fun Try.Companion.success(): Prism, A> = pSuccess() * [Prism] to focus into an [arrow.Try.Failure] */ fun Try.Companion.failure(): Prism, Throwable> = Prism( - getOrModify = { aTry -> aTry.fold(::Right, { Either.Left(aTry) }) }, + getOrModify = { aTry -> aTry.fold(::Right) { Either.Left(aTry) } }, reverseGet = ::Failure ) diff --git a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/typeclasses/FilterIndex.kt b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/typeclasses/FilterIndex.kt index 4fdeaaba193..ccdc8ea5dee 100644 --- a/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/typeclasses/FilterIndex.kt +++ b/modules/optics/arrow-optics/src/main/kotlin/arrow/optics/typeclasses/FilterIndex.kt @@ -40,9 +40,9 @@ interface FilterIndex { override fun modifyF(FA: Applicative, s: Kind, f: (A) -> Kind): Kind> = traverse.run { FA.run { - zipWithIndex(s).traverse(this, { (a, j) -> + zipWithIndex(s).traverse(this) { (a, j) -> if (p(j)) f(a) else just(a) - }) + } } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/FoldTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/FoldTest.kt index c1e3e613aed..cde909307ff 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/FoldTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/FoldTest.kt @@ -23,72 +23,72 @@ class FoldTest : UnitSpec() { "Fold select a list that contains one" { val select = Fold.select> { it.contains(1) } - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> select.run { getAll(ints) }.list.firstOrNull() == ints.let { if (it.contains(1)) it else null } - }) + } } with(intFold) { "Folding a list of ints" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> fold(Int.monoid(), ints.k()) == ints.sum() - }) + } } "Folding a list should yield same result as combineAll" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> combineAll(Int.monoid(), ints.k()) == ints.sum() - }) + } } "Folding and mapping a list of strings" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> stringFold.run { foldMap(Int.monoid(), ints.map(Int::toString).k(), String::toInt) } == ints.sum() - }) + } } "Get all targets" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> getAll(ints.k()) == ints.k() - }) + } } "Get the size of the fold" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> size(ints.k()) == ints.size - }) + } } "Find the first element matching the predicate" { - forAll(Gen.list(Gen.choose(-100, 100)), { ints -> + forAll(Gen.list(Gen.choose(-100, 100))) { ints -> find(ints.k()) { it > 10 } == Option.fromNullable(ints.firstOrNull { it > 10 }) - }) + } } "Checking existence of a target" { - forAll(Gen.list(Gen.int()), Gen.bool(), { ints, predicate -> + forAll(Gen.list(Gen.int()), Gen.bool()) { ints, predicate -> exists(ints.k()) { predicate } == predicate - }) + } } "Check if all targets match the predicate" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> forall(ints.k()) { it % 2 == 0 } == ints.all { it % 2 == 0 } - }) + } } "Check if there is no target" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> isEmpty(ints.k()) == ints.isEmpty() - }) + } } "Check if there is a target" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> nonEmpty(ints.k()) == ints.isNotEmpty() - }) + } } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/GetterTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/GetterTest.kt index 6d524ab8ba9..f96f57c011b 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/GetterTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/GetterTest.kt @@ -73,60 +73,60 @@ class GetterTest : UnitSpec() { with(tokenGetter) { "Getting the target should always yield the exact result" { - forAll({ value: String -> + forAll { value: String -> get(Token(value)) == value - }) + } } "Finding a target using a predicate within a Getter should be wrapped in the correct option result" { - forAll({ value: String, predicate: Boolean -> + forAll { value: String, predicate: Boolean -> find(Token(value)) { predicate }.fold({ false }, { true }) == predicate - }) + } } "Checking existence of a target should always result in the same result as predicate" { - forAll({ value: String, predicate: Boolean -> + forAll { value: String, predicate: Boolean -> exist(Token(value)) { predicate } == predicate - }) + } } } "Zipping two lenses should yield a tuple of the targets" { - forAll({ value: String -> + forAll { value: String -> length.zip(upper).get(value) == value.length toT value.toUpperCase() - }) + } } "Joining two getters together with same target should yield same result" { val userTokenStringGetter = userGetter compose tokenGetter val joinedGetter = tokenGetter.choice(userTokenStringGetter) - forAll({ tokenValue: String -> + forAll { tokenValue: String -> val token = Token(tokenValue) val user = User(token) joinedGetter.get(Left(token)) == joinedGetter.get(Right(user)) - }) + } } "Pairing two disjoint getters should yield a pair of their results" { val splitGetter: Getter, Tuple2> = tokenGetter.split(userGetter) - forAll(TokenGen, UserGen, { token: Token, user: User -> + forAll(TokenGen, UserGen) { token: Token, user: User -> splitGetter.get(token toT user) == token.value toT user.token - }) + } } "Creating a first pair with a type should result in the target to value" { val first = tokenGetter.first() - forAll(TokenGen, Gen.int(), { token: Token, int: Int -> + forAll(TokenGen, Gen.int()) { token: Token, int: Int -> first.get(token toT int) == token.value toT int - }) + } } "Creating a second pair with a type should result in the value target" { val first = tokenGetter.second() - forAll(Gen.int(), TokenGen, { int: Int, token: Token -> + forAll(Gen.int(), TokenGen) { int: Int, token: Token -> first.get(int toT token) == int toT token.value - }) + } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/IsoTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/IsoTest.kt index 347c42e6348..72243c9a04d 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/IsoTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/IsoTest.kt @@ -153,77 +153,77 @@ class IsoTest : UnitSpec() { } "Lifting a function should yield the same result as not yielding" { - forAll(TokenGen, Gen.string(), { token, value -> + forAll(TokenGen, Gen.string()) { token, value -> tokenIso.modify(token) { value } == tokenIso.lift { value }(token) - }) + } } "Lifting a function as a functor should yield the same result as not yielding" { - forAll(TokenGen, Gen.string(), { token, value -> + forAll(TokenGen, Gen.string()) { token, value -> tokenIso.modifyF(Option.functor(), token) { Some(value) } == tokenIso.liftF(Option.functor()) { Some(value) }(token) - }) + } } "Creating a first pair with a type should result in the target to value" { val first = tokenIso.first() - forAll(TokenGen, Gen.int(), { token: Token, int: Int -> + forAll(TokenGen, Gen.int()) { token: Token, int: Int -> first.get(token toT int) == token.value toT int - }) + } } "Creating a second pair with a type should result in the value to target" { val second = tokenIso.second() - forAll(Gen.int(), TokenGen, { int: Int, token: Token -> + forAll(Gen.int(), TokenGen) { int: Int, token: Token -> second.get(int toT token) == int toT token.value - }) + } } "Creating a left with a type should result in a sum target to value" { val left = tokenIso.left() - forAll(TokenGen, Gen.int(), { token: Token, int: Int -> + forAll(TokenGen, Gen.int()) { token: Token, int: Int -> left.get(Either.Left(token)) == Either.Left(token.value) && left.get(Either.Right(int)) == Either.Right(int) - }) + } } "Creating a right with a type should result in a sum value to target" { val left = tokenIso.right() - forAll(TokenGen, Gen.int(), { token: Token, int: Int -> + forAll(TokenGen, Gen.int()) { token: Token, int: Int -> left.get(Either.Left(int)) == Either.Left(int) && left.get(Either.Right(token)) == Either.Right(token.value) - }) + } } "Finding a target using a predicate within a Iso should be wrapped in the correct option result" { - forAll({ predicate: Boolean -> + forAll { predicate: Boolean -> tokenIso.find(Token("any value")) { predicate }.fold({ false }, { true }) == predicate - }) + } } "Checking existence predicate over the target should result in same result as predicate" { - forAll({ predicate: Boolean -> + forAll { predicate: Boolean -> tokenIso.exist(Token("any value")) { predicate } == predicate - }) + } } "Pairing two disjoint isos together" { val joinedIso = tokenIso split userIso - forAll({ tokenValue: String -> + forAll { tokenValue: String -> val token = Token(tokenValue) val user = User(token) joinedIso.get(token toT user) == tokenValue toT token - }) + } } "Composing isos should result in an iso of the first iso's value with the second iso's target" { val composedIso = userIso compose tokenIso - forAll({ tokenValue: String -> + forAll { tokenValue: String -> val token = Token(tokenValue) val user = User(token) composedIso.get(user) == tokenValue - }) + } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/LensTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/LensTest.kt index 06a405c8b96..79ac608ec75 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/LensTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/LensTest.kt @@ -137,59 +137,59 @@ class LensTest : UnitSpec() { } "Lifting a function should yield the same result as not yielding" { - forAll(TokenGen, Gen.string(), { token, value -> + forAll(TokenGen, Gen.string()) { token, value -> tokenLens.set(token, value) == tokenLens.lift { value }(token) - }) + } } "Lifting a function as a functor should yield the same result as not yielding" { - forAll(TokenGen, Gen.string(), { token, value -> + forAll(TokenGen, Gen.string()) { token, value -> tokenLens.modifyF(Option.functor(), token) { Some(value) } == tokenLens.liftF(Option.functor()) { Some(value) }(token) - }) + } } "Finding a target using a predicate within a Lens should be wrapped in the correct option result" { - forAll({ predicate: Boolean -> + forAll { predicate: Boolean -> tokenLens.find(Token("any value")) { predicate }.fold({ false }, { true }) == predicate - }) + } } "Checking existence predicate over the target should result in same result as predicate" { - forAll({ predicate: Boolean -> + forAll { predicate: Boolean -> tokenLens.exist(Token("any value")) { predicate } == predicate - }) + } } "Joining two lenses together with same target should yield same result" { val userTokenStringLens = userLens compose tokenLens val joinedLens = tokenLens choice userTokenStringLens - forAll({ tokenValue: String -> + forAll { tokenValue: String -> val token = Token(tokenValue) val user = User(token) joinedLens.get(Left(token)) == joinedLens.get(Right(user)) - }) + } } "Pairing two disjoint lenses should yield a pair of their results" { val spiltLens: Lens, Tuple2> = tokenLens split userLens - forAll(TokenGen, UserGen, { token: Token, user: User -> + forAll(TokenGen, UserGen) { token: Token, user: User -> spiltLens.get(token toT user) == token.value toT user.token - }) + } } "Creating a first pair with a type should result in the target to value" { val first = tokenLens.first() - forAll(TokenGen, Gen.int(), { token: Token, int: Int -> + forAll(TokenGen, Gen.int()) { token: Token, int: Int -> first.get(token toT int) == token.value toT int - }) + } } "Creating a second pair with a type should result in the value target" { val first = tokenLens.second() - forAll(Gen.int(), TokenGen, { int: Int, token: Token -> + forAll(Gen.int(), TokenGen) { int: Int, token: Token -> first.get(int toT token) == int toT token.value - }) + } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/OptionalTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/OptionalTest.kt index af50929fad7..10a7c5a88d1 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/OptionalTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/OptionalTest.kt @@ -140,67 +140,67 @@ class OptionalTest : UnitSpec() { } "void should always " { - forAll({ string: String -> + forAll { string: String -> Optional.void().getOption(string) == None - }) + } } "void should always return source when setting target" { - forAll({ int: Int, string: String -> + forAll { int: Int, string: String -> Optional.void().set(string, int) == string - }) + } } "Checking if there is no target" { - forAll(Gen.list(Gen.int()), { list -> + forAll(Gen.list(Gen.int())) { list -> optionalHead.nonEmpty(list) == list.isNotEmpty() - }) + } } "Lift should be consistent with modify" { - forAll(Gen.list(Gen.int()), { list -> + forAll(Gen.list(Gen.int())) { list -> val f = { i: Int -> i + 5 } optionalHead.lift(f)(list) == optionalHead.modify(list, f) - }) + } } "LiftF should be consistent with modifyF" { - forAll(Gen.list(Gen.int()), genTry(Gen.int()), { list, tryInt -> + forAll(Gen.list(Gen.int()), genTry(Gen.int())) { list, tryInt -> val f = { _: Int -> tryInt } optionalHead.liftF(Try.applicative(), f)(list) == optionalHead.modifyF(Try.applicative(), list, f) - }) + } } "Checking if a target exists" { - forAll(Gen.list(Gen.int()), { list -> + forAll(Gen.list(Gen.int())) { list -> optionalHead.isEmpty(list) == list.isEmpty() - }) + } } "Finding a target using a predicate should be wrapped in the correct option result" { - forAll(Gen.list(Gen.int()), Gen.bool(), { list, predicate -> + forAll(Gen.list(Gen.int()), Gen.bool()) { list, predicate -> optionalHead.find(list) { predicate }.fold({ false }, { true }) == predicate - }) + } } "Checking existence predicate over the target should result in same result as predicate" { - forAll(Gen.list(Gen.int()), Gen.bool(), { list, predicate -> + forAll(Gen.list(Gen.int()), Gen.bool()) { list, predicate -> optionalHead.exists(list) { predicate } == predicate - }) + } } "Checking satisfaction of predicate over the target should result in opposite result as predicate" { - forAll(Gen.list(Gen.int()), Gen.bool(), { list, predicate -> + forAll(Gen.list(Gen.int()), Gen.bool()) { list, predicate -> optionalHead.all(list) { predicate } == predicate - }) + } } "Joining two optionals together with same target should yield same result" { val joinedOptional = optionalHead.choice(defaultHead) - forAll(Gen.int(), { int -> + forAll(Gen.int()) { int -> joinedOptional.getOption(Left(listOf(int))) == joinedOptional.getOption(Right(int)) - }) + } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/PrismTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/PrismTest.kt index 965b2b64fe7..59bb9f76e55 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/PrismTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/PrismTest.kt @@ -161,54 +161,54 @@ class PrismTest : UnitSpec() { } "Joining two prisms together with same target should yield same result" { - forAll(SumGen, { a -> + forAll(SumGen) { a -> (sumPrism compose stringPrism).getOption(a) == sumPrism.getOption(a).flatMap(stringPrism::getOption) && (sumPrism + stringPrism).getOption(a) == (sumPrism compose stringPrism).getOption(a) - }) + } } "Checking if a prism exists with a target" { - forAll(SumGen, SumGen, Gen.bool(), { a, other, bool -> + forAll(SumGen, SumGen, Gen.bool()) { a, other, bool -> Prism.only(a, object : Eq { override fun SumType.eqv(b: SumType): Boolean = bool }).isEmpty(other) == bool - }) + } } "Checking if there is no target" { - forAll(SumGen, { sum -> + forAll(SumGen) { sum -> sumPrism.isEmpty(sum) == sum !is SumType.A - }) + } } "Checking if a target exists" { - forAll(SumGen, { sum -> + forAll(SumGen) { sum -> sumPrism.nonEmpty(sum) == sum is SumType.A - }) + } } "Setting a target on a prism should set the correct target"{ - forAll(AGen, Gen.string(), { a, string -> + forAll(AGen, Gen.string()) { a, string -> sumPrism.setOption(a, string) == Some(a.copy(string = string)) - }) + } } "Finding a target using a predicate within a Lens should be wrapped in the correct option result" { - forAll(SumGen, Gen.bool(), { sum, predicate -> + forAll(SumGen, Gen.bool()) { sum, predicate -> sumPrism.find(sum) { predicate }.fold({ false }, { true }) == (predicate && sum is SumType.A) - }) + } } "Checking existence predicate over the target should result in same result as predicate" { - forAll(SumGen, Gen.bool(), { sum, predicate -> + forAll(SumGen, Gen.bool()) { sum, predicate -> sumPrism.exist(sum) { predicate } == (predicate && sum is SumType.A) - }) + } } "Checking satisfaction of predicate over the target should result in opposite result as predicate" { - forAll(SumGen, Gen.bool(), { sum, predicate -> + forAll(SumGen, Gen.bool()) { sum, predicate -> sumPrism.all(sum) { predicate } == (predicate || sum is SumType.B) - }) + } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/SetterTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/SetterTest.kt index 69414fbcf07..9675cf86859 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/SetterTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/SetterTest.kt @@ -47,16 +47,16 @@ class SetterTest : UnitSpec() { val token = Token(oldValue) val user = User(token) - forAll({ value: String -> + forAll { value: String -> joinedSetter.set(token.left(), value).swap().getOrElse { Token("Wrong value") }.value == joinedSetter.set(user.right(), value).getOrElse { User(Token("Wrong value")) }.token.value - }) + } } "Lifting a function should yield the same result as direct modify" { - forAll(TokenGen, Gen.string(), { token, value -> + forAll(TokenGen, Gen.string()) { token, value -> tokenSetter.modify(token) { value } == tokenSetter.lift { value }(token) - }) + } } } } diff --git a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/TraversalTest.kt b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/TraversalTest.kt index 0f38854372a..bef6e9098a1 100644 --- a/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/TraversalTest.kt +++ b/modules/optics/arrow-optics/src/test/kotlin/arrow/optics/TraversalTest.kt @@ -110,33 +110,33 @@ class TraversalTest : UnitSpec() { with(listKTraverse) { "Getting all targets of a traversal" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> getAll(ints.k()) == ints.k() - }) + } } "Folding all the values of a traversal" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> fold(Int.monoid(), ints.k()) == ints.sum() - }) + } } "Combining all the values of a traversal" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> combineAll(Int.monoid(), ints.k()) == ints.sum() - }) + } } "Finding an number larger than 10" { - forAll(Gen.list(Gen.choose(-100, 100)), { ints -> + forAll(Gen.list(Gen.choose(-100, 100))) { ints -> find(ints.k()) { it > 10 } == Option.fromNullable(ints.firstOrNull { it > 10 }) - }) + } } "Get the length from a traversal" { - forAll(Gen.list(Gen.int()), { ints -> + forAll(Gen.list(Gen.int())) { ints -> size(ints.k()) == ints.size - }) + } } }