Skip to content

Commit

Permalink
Rename pure to just
Browse files Browse the repository at this point in the history
  • Loading branch information
pakoito committed Mar 30, 2018
1 parent 9652526 commit 0930083
Show file tree
Hide file tree
Showing 130 changed files with 374 additions and 374 deletions.
4 changes: 2 additions & 2 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Eval.kt
Expand Up @@ -37,11 +37,11 @@ sealed class Eval<out A> : EvalOf<A> {
f(a).fix().flatMap { eval: Either<A, B> ->
when (eval) {
is Either.Left -> tailRecM(eval.a, f)
is Either.Right -> pure(eval.b)
is Either.Right -> just(eval.b)
}
}

fun <A> pure(a: A): Eval<A> = now(a)
fun <A> just(a: A): Eval<A> = now(a)

fun <A> now(a: A) = Now(a)

Expand Down
2 changes: 1 addition & 1 deletion modules/core/arrow-core/src/main/kotlin/arrow/core/Id.kt
Expand Up @@ -31,6 +31,6 @@ data class Id<out A>(val value: A) : IdOf<A> {
}
}

fun <A> pure(a: A): Id<A> = Id(a)
fun <A> just(a: A): Id<A> = Id(a)
}
}
Expand Up @@ -11,7 +11,7 @@ sealed class Option<out A> : OptionOf<A> {

companion object {

fun <A> pure(a: A): Option<A> = Some(a)
fun <A> just(a: A): Option<A> = Some(a)

tailrec fun <A, B> tailRecM(a: A, f: (A) -> OptionOf<Either<A, B>>): Option<B> {
val option = f(a).fix()
Expand Down
4 changes: 2 additions & 2 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Try.kt
Expand Up @@ -17,7 +17,7 @@ sealed class Try<out A> : TryOf<A> {

companion object {

fun <A> pure(a: A): Try<A> = Success(a)
fun <A> just(a: A): Try<A> = Success(a)

tailrec fun <A, B> tailRecM(a: A, f: (A) -> TryOf<Either<A, B>>): Try<B> {
val ev: Try<Either<A, B>> = f(a).fix()
Expand Down Expand Up @@ -114,7 +114,7 @@ sealed class Try<out A> : TryOf<A> {
return this
}

@Deprecated(DeprecatedUnsafeAccess, ReplaceWith("fold ({ Try { body(it); it }}, { Try.pure(it) })"))
@Deprecated(DeprecatedUnsafeAccess, ReplaceWith("fold ({ Try { body(it); it }}, { Try.just(it) })"))
fun onFailure(body: (Throwable) -> Unit): Try<A> = when (this) {
is Success -> this
is Failure -> {
Expand Down
Expand Up @@ -31,7 +31,7 @@ data class Cokleisli<F, A, B>(val MM: Comonad<F>, val run: CokleisliFun<F, A, B>
companion object {
inline operator fun <F, A, B> invoke(MF: Comonad<F>, noinline run: (Kind<F, A>) -> B): Cokleisli<F, A, B> = Cokleisli(MF, run)

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

inline fun <F, B> ask(MF: Comonad<F>): Cokleisli<F, B, B> = Cokleisli(MF, { MF.run { it.extract() } })
}
Expand Down
6 changes: 3 additions & 3 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/Const.kt
Expand Up @@ -13,12 +13,12 @@ data class Const<A, out T>(val value: A) : ConstOf<A, T> {
@Suppress("UNCHECKED_CAST")
fun <U> retag(): Const<A, U> = this as Const<A, U>

fun <F, U> traverse(f: (T) -> Kind<F, U>, FA: Applicative<F>): Kind<F, Const<A, U>> = FA.pure(retag())
fun <F, U> traverse(f: (T) -> Kind<F, U>, FA: Applicative<F>): Kind<F, Const<A, U>> = FA.just(retag())

fun <F, U> traverseFilter(f: (T) -> Kind<F, Option<U>>, FA: Applicative<F>): Kind<F, Const<A, U>> = FA.pure(retag())
fun <F, U> traverseFilter(f: (T) -> Kind<F, Option<U>>, FA: Applicative<F>): Kind<F, Const<A, U>> = FA.just(retag())

companion object {
fun <A, T> pure(a: A): Const<A, T> = Const(a)
fun <A, T> just(a: A): Const<A, T> = Const(a)
}
}

Expand Down
Expand Up @@ -14,7 +14,7 @@ fun <A, B> CoreaderT<ForId, A, B>.runId(d: A): B = this.run(Id(d))
object Coreader {
operator fun <A, B> invoke(run: (A) -> B): CoreaderT<ForId, A, B> = Cokleisli(IdBimonad) { a: IdOf<A> -> run(a.fix().value) }

fun <A, B> pure(MF: Comonad<ForId>, x: B): CoreaderT<ForId, A, B> = Cokleisli.pure(MF, x)
fun <A, B> just(MF: Comonad<ForId>, x: B): CoreaderT<ForId, A, B> = Cokleisli.just(MF, x)

fun <B> ask(MF: Comonad<ForId>): CoreaderT<ForId, B, B> = Cokleisli.ask(MF)
}
12 changes: 6 additions & 6 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/EitherT.kt
Expand Up @@ -23,7 +23,7 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,

inline operator fun <F, A, B> invoke(value: Kind<F, Either<A, B>>): EitherT<F, A, B> = EitherT(value)

fun <F, A, B> pure(MF: Applicative<F>, b: B): EitherT<F, A, B> = right(MF, b)
fun <F, A, B> just(MF: Applicative<F>, b: B): EitherT<F, A, B> = right(MF, b)

fun <F, L, A, B> tailRecM(MF: Monad<F>, a: A, f: (A) -> EitherTOf<F, L, Either<A, B>>): EitherT<F, L, B> = MF.run {
EitherT(tailRecM(a, {
Expand All @@ -42,12 +42,12 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,
}))
}

fun <F, A, B> right(MF: Applicative<F>, b: B): EitherT<F, A, B> = EitherT(MF.pure(Right(b)))
fun <F, A, B> right(MF: Applicative<F>, b: B): EitherT<F, A, B> = EitherT(MF.just(Right(b)))

fun <F, A, B> left(MF: Applicative<F>, a: A): EitherT<F, A, B> = EitherT(MF.pure(Left(a)))
fun <F, A, B> left(MF: Applicative<F>, a: A): EitherT<F, A, B> = EitherT(MF.just(Left(a)))

inline fun <F, A, B> fromEither(AP: Applicative<F>, value: Either<A, B>): EitherT<F, A, B> =
EitherT(AP.pure(value))
EitherT(AP.just(value))
}

inline fun <C> fold(FF: Functor<F>, crossinline l: (A) -> C, crossinline r: (B) -> C): Kind<F, C> = FF.run {
Expand All @@ -58,7 +58,7 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,
flatMapF(MF, { it -> f(it).value })

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

inline fun <C> cata(FF: Functor<F>, crossinline l: (A) -> C, crossinline r: (B) -> C): Kind<F, C> = fold(FF, l, r)
Expand Down Expand Up @@ -95,7 +95,7 @@ data class EitherT<F, A, B>(val value: Kind<F, Either<A, B>>) : EitherTOf<F, A,
EitherT(fix().value.flatMap {
when (it) {
is Either.Left -> y.fix().value
is Either.Right -> pure(it)
is Either.Right -> just(it)
}
})
}
Expand Down
Expand Up @@ -10,7 +10,7 @@ operator fun <A> Function0Of<A>.invoke(): A = this.fix().f()
@higherkind
data class Function0<out A>(internal val f: () -> A) : Function0Of<A> {

fun <B> map(f: (A) -> B): Function0<B> = pure(f(this()))
fun <B> map(f: (A) -> B): Function0<B> = just(f(this()))

fun <B> flatMap(ff: (A) -> Function0Of<B>): Function0<B> = ff(f()).fix()

Expand All @@ -22,7 +22,7 @@ data class Function0<out A>(internal val f: () -> A) : Function0Of<A> {

companion object {

fun <A> pure(a: A): Function0<A> = { a }.k()
fun <A> just(a: A): Function0<A> = { a }.k()

tailrec fun <A, B> loop(a: A, f: (A) -> Kind<ForFunction0, Either<A, B>>): B {
val fa = f(a).fix()()
Expand Down
Expand Up @@ -24,7 +24,7 @@ class Function1<I, out O>(val f: (I) -> O) : Function1Of<I, O> {

fun <I> ask(): Function1<I, I> = { a: I -> a }.k()

fun <I, A> pure(a: A): Function1<I, A> = { _: I -> a }.k()
fun <I, A> just(a: A): Function1<I, A> = { _: I -> a }.k()

tailrec private fun <I, A, B> step(a: A, t: I, fn: (A) -> Function1Of<I, Either<A, B>>): B {
val af = fn(a)(t)
Expand Down
2 changes: 1 addition & 1 deletion modules/core/arrow-data/src/main/kotlin/arrow/data/Ior.kt
Expand Up @@ -145,7 +145,7 @@ sealed class Ior<out A, out B> : IorOf<A, B> {
fold({ lc }, { f(it, lc) }, { _, b -> f(b, lc) })

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

/**
Expand Down
4 changes: 2 additions & 2 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/Kleisli.kt
Expand Up @@ -135,14 +135,14 @@ class Kleisli<F, D, A> (val run: KleisliFun<F, D, A>) : KleisliOf<F, D, A>, Klei
* @param x value of [A].
* @param AF [Applicative] for context [F].
*/
inline fun <F, D, A> pure(AF: Applicative<F>, x: A): Kleisli<F, D, A> = Kleisli { _ -> AF.pure(x) }
inline fun <F, D, A> just(AF: Applicative<F>, x: A): Kleisli<F, D, A> = Kleisli { _ -> AF.just(x) }

/**
* Ask an arrow from [D] to [D].
*
* @param AF [Applicative] for context [F].
*/
inline fun <F, D> ask(AF: Applicative<F>): Kleisli<F, D, D> = Kleisli { AF.pure(it) }
inline fun <F, D> ask(AF: Applicative<F>): Kleisli<F, D, D> = Kleisli { AF.just(it) }

/**
* Raise an error [E].
Expand Down
6 changes: 3 additions & 3 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/ListK.kt
Expand Up @@ -28,7 +28,7 @@ data class ListK<out A> (val list: List<A>) : ListKOf<A>, List<A> by list {
fun <B> ap(ff: ListKOf<(A) -> B>): ListK<B> = ff.fix().flatMap { f -> map(f) }.fix()

fun <G, B> traverse(GA: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, ListK<B>> = GA.run {
foldRight(Eval.always { pure(emptyList<B>().k()) }) { a, eval ->
foldRight(Eval.always { just(emptyList<B>().k()) }) { a, eval ->
f(a).map2Eval(eval) { (listOf(it.a) + it.b).k() }
}.value()
}
Expand All @@ -41,11 +41,11 @@ data class ListK<out A> (val list: List<A>) : ListKOf<A>, List<A> by list {
}.fix()

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

companion object {

fun <A> pure(a: A): ListK<A> = listOf(a).k()
fun <A> just(a: A): ListK<A> = listOf(a).k()

fun <A> empty(): ListK<A> = emptyList<A>().k()

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

fun <G, B> traverse(GA: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, MapK<K, B>> = GA.run {
(Foldable.iterateRight(map.iterator(), Eval.always { pure(emptyMap<K, B>().k()) }))({ kv, lbuf ->
(Foldable.iterateRight(map.iterator(), Eval.always { just(emptyMap<K, B>().k()) }))({ kv, lbuf ->
f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).k() }
}).value()
}
Expand Down
Expand Up @@ -92,7 +92,7 @@ class NonEmptyList<out A> private constructor(
fun <A> fromList(l: List<A>): Option<NonEmptyList<A>> = if (l.isEmpty()) None else Some(NonEmptyList(l))
fun <A> fromListUnsafe(l: List<A>): NonEmptyList<A> = NonEmptyList(l)

fun <A> pure(a: A): NonEmptyList<A> = a.nel()
fun <A> just(a: A): NonEmptyList<A> = a.nel()

@Suppress("UNCHECKED_CAST")
private tailrec fun <A, B> go(
Expand Down
12 changes: 6 additions & 6 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/OptionT.kt
Expand Up @@ -20,12 +20,12 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio

operator fun <F, A> invoke(value: Kind<F, Option<A>>): OptionT<F, A> = OptionT(value)

inline fun <F, A> pure(a: A, AF: Applicative<F>): OptionT<F, A> = OptionT(AF.pure(Some(a)))
inline fun <F, A> just(a: A, AF: Applicative<F>): OptionT<F, A> = OptionT(AF.just(Some(a)))

inline fun <F> none(AF: Applicative<F>): OptionT<F, Nothing> = OptionT(AF.pure(None))
inline fun <F> none(AF: Applicative<F>): OptionT<F, Nothing> = OptionT(AF.just(None))

inline fun <F, A> fromOption(AF: Applicative<F>, value: Option<A>): OptionT<F, A> =
OptionT(AF.pure(value))
OptionT(AF.just(value))

fun <F, A, B> tailRecM(MF: Monad<F>, a: A, f: (A) -> OptionTOf<F, Either<A, B>>): OptionT<F, B> = MF.run {
OptionT(tailRecM(a, {
Expand All @@ -52,7 +52,7 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
inline fun <B> flatMap(MF: Monad<F>, crossinline f: (A) -> OptionT<F, B>): OptionT<F, B> = flatMapF(MF, { it -> f(it).value })

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

fun <B> liftF(FF: Functor<F>, fa: Kind<F, B>): OptionT<F, B> = FF.run {
Expand All @@ -68,7 +68,7 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
fun getOrElse(FF: Functor<F>, default: () -> A): Kind<F, A> = FF.run { value.map({ it.getOrElse(default) }) }

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

inline fun filter(FF: Functor<F>, crossinline p: (A) -> Boolean): OptionT<F, A> = FF.run {
Expand All @@ -92,7 +92,7 @@ data class OptionT<F, A>(val value: Kind<F, Option<A>>) : OptionTOf<F, A>, Optio
inline fun orElseF(MF: Monad<F>, crossinline default: () -> Kind<F, Option<A>>): OptionT<F, A> = MF.run {
OptionT(value.flatMap {
when (it) {
is Some<A> -> MF.pure(it)
is Some<A> -> MF.just(it)
is None -> default()
}
})
Expand Down
Expand Up @@ -115,7 +115,7 @@ fun Reader(): ReaderApi = ReaderApi

object ReaderApi {

fun <D, A> pure(x: A): Reader<D, A> = ReaderT.pure(IdBimonad, x)
fun <D, A> just(x: A): Reader<D, A> = ReaderT.just(IdBimonad, x)

fun <D> ask(): Reader<D, D> = ReaderT.ask(IdBimonad)

Expand Down
Expand Up @@ -29,7 +29,7 @@ data class SequenceK<out A> (val sequence: Sequence<A>) : SequenceKOf<A>, Sequen
}

fun <G, B> traverse(GA: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, SequenceK<B>> = GA.run {
foldRight(Eval.always { pure(emptySequence<B>().k()) }) { a, eval ->
foldRight(Eval.always { just(emptySequence<B>().k()) }) { a, eval ->
f(a).map2Eval(eval) { (sequenceOf(it.a) + it.b).k() }
}.value()
}
Expand All @@ -43,7 +43,7 @@ data class SequenceK<out A> (val sequence: Sequence<A>) : SequenceKOf<A>, Sequen

companion object {

fun <A> pure(a: A): SequenceK<A> = sequenceOf(a).k()
fun <A> just(a: A): SequenceK<A> = sequenceOf(a).k()

fun <A> empty(): SequenceK<A> = emptySequence<A>().k()

Expand Down
2 changes: 1 addition & 1 deletion modules/core/arrow-data/src/main/kotlin/arrow/data/SetK.kt
Expand Up @@ -18,7 +18,7 @@ data class SetK<out A>(val set: Set<A>) : SetKOf<A>, Set<A> by set {

companion object {

fun <A> pure(a: A): SetK<A> = setOf(a).k()
fun <A> just(a: A): SetK<A> = setOf(a).k()

fun empty(): SetK<Nothing> = empty

Expand Down
Expand Up @@ -47,7 +47,7 @@ data class SortedMapK<A: Comparable<A>, B>(val map: SortedMap<A, B>) : SortedMap
this.map.foldLeft(c) { m: SortedMap<A, C>, (a, b) -> f(m.k(), Tuple2(a, b)) }.k()

fun <G, C> traverse(GA: Applicative<G>, f: (B) -> Kind<G, C>): Kind<G, SortedMapK<A, C>> = GA.run {
(Foldable.iterateRight(map.iterator(), Eval.always { pure(sortedMapOf<A, C>().k()) }))({ kv, lbuf ->
(Foldable.iterateRight(map.iterator(), Eval.always { just(sortedMapOf<A, C>().k()) }))({ kv, lbuf ->
f(kv.value).map2Eval(lbuf) { (mapOf(kv.key to it.a).k() + it.b).toSortedMap().k() }
}).value()
}
Expand Down
6 changes: 3 additions & 3 deletions modules/core/arrow-data/src/main/kotlin/arrow/data/State.kt
Expand Up @@ -58,7 +58,7 @@ fun <S, A> StateFunOf<S, A>.toState(): State<S, A> = State(this)
fun <S, T, P1, R> State<S, T>.map(sx: State<S, P1>, f: (T, P1) -> R): State<S, R> =
flatMap (IdBimonad, { t -> sx.map { x -> f(t, x) } }).fix()

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

/**
* Alias for [StateT.run] `StateT<ForId, S, A>`
Expand Down Expand Up @@ -89,7 +89,7 @@ fun State() = StateApi

object StateApi {

fun <S, T> pure(t: T): State<S, T> = StateT.pure(IdBimonad, t)
fun <S, T> just(t: T): State<S, T> = StateT.just(IdBimonad, t)

/**
* Return input without modifying it.
Expand Down Expand Up @@ -118,7 +118,7 @@ object StateApi {
fun <S> set(s: S): State<S, Unit> = StateT.set(IdBimonad, s)
}

fun <R, S, T> List<T>.stateTraverse(f: (T) -> State<S, R>): State<S, List<R>> = foldRight(StateApi.pure(emptyList())) { i: T, accumulator: State<S, List<R>> ->
fun <R, S, T> List<T>.stateTraverse(f: (T) -> State<S, R>): State<S, List<R>> = foldRight(StateApi.just(emptyList())) { i: T, accumulator: State<S, List<R>> ->
f(i).map(accumulator, ({ head: R, tail: List<R> ->
listOf(head) + tail
}))
Expand Down

0 comments on commit 0930083

Please sign in to comment.