Skip to content

Commit

Permalink
[arrow-core] Make param names more dev friendly (arrow-kt#853)
Browse files Browse the repository at this point in the history
* Updated param names for Try

* Updated param names for Either

* Finished other data types in core module.
  • Loading branch information
aballano authored and RawToast committed Jul 18, 2018
1 parent df20027 commit dc286fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
30 changes: 15 additions & 15 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Either.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
fun isRight(): Boolean = isRight

/**
* Applies `fa` if this is a [Left] or `fb` if this is a [Right].
* Applies `ifLeft` if this is a [Left] or `ifRight` if this is a [Right].
*
* Example:
* ```
Expand All @@ -45,13 +45,13 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
* )
* ```
*
* @param fa the function to apply if this is a [Left]
* @param fb the function to apply if this is a [Right]
* @param ifLeft the function to apply if this is a [Left]
* @param ifRight the function to apply if this is a [Right]
* @return the results of applying the function
*/
inline fun <C> fold(crossinline fa: (A) -> C, crossinline fb: (B) -> C): C = when (this) {
is Right<A, B> -> fb(b)
is Left<A, B> -> fa(a)
inline fun <C> fold(crossinline ifLeft: (A) -> C, crossinline ifRight: (B) -> C): C = when (this) {
is Right<A, B> -> ifRight(b)
is Left<A, B> -> ifLeft(a)
}

@Deprecated(DeprecatedUnsafeAccess, ReplaceWith("getOrElse { ifLeft }"))
Expand All @@ -60,19 +60,19 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
is Left -> throw NoSuchElementException("Disjunction.Left")
}

fun <C> foldLeft(b: C, f: (C, B) -> C): C =
fun <C> foldLeft(initial: C, rightOperation: (C, B) -> C): C =
this.fix().let { either ->
when (either) {
is Right -> f(b, either.b)
is Left -> b
is Right -> rightOperation(initial, either.b)
is Left -> initial
}
}

fun <C> foldRight(lb: Eval<C>, f: (B, Eval<C>) -> Eval<C>): Eval<C> =
fun <C> foldRight(initial: Eval<C>, rightOperation: (B, Eval<C>) -> Eval<C>): Eval<C> =
this.fix().let { either ->
when (either) {
is Right -> f(either.b, lb)
is Left -> lb
is Right -> rightOperation(either.b, initial)
is Left -> initial
}
}

Expand Down Expand Up @@ -120,8 +120,8 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
/**
* Map over Left and Right of this Either
*/
inline fun <C, D> bimap(crossinline fa: (A) -> C, crossinline fb: (B) -> D): Either<C, D> =
fold({ Left(fa(it)) }, { Right(fb(it)) })
inline fun <C, D> bimap(crossinline leftOperation: (A) -> C, crossinline rightOperation: (B) -> D): Either<C, D> =
fold({ Left(leftOperation(it)) }, { Right(rightOperation(it)) })

/**
* Returns `false` if [Left] or returns the result of the application of
Expand Down Expand Up @@ -208,7 +208,7 @@ sealed class Either<out A, out B> : EitherOf<A, B> {
}
}

fun <L, R> cond(test: Boolean, r: () -> R, l: () -> L): Either<L, R> = if (test) right(r()) else left(l())
fun <L, R> cond(test: Boolean, ifTrue: () -> R, ifFalse: () -> L): Either<L, R> = if (test) right(ifTrue()) else left(ifFalse())

}
}
Expand Down
4 changes: 2 additions & 2 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Id.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ data class Id<out A>(val value: A) : IdOf<A> {

inline fun <B> flatMap(f: (A) -> IdOf<B>): Id<B> = f(value).fix()

fun <B> foldLeft(b: B, f: (B, A) -> B): B = f(b, this.fix().value)
fun <B> foldLeft(initial: B, operation: (B, A) -> B): B = operation(initial, this.fix().value)

fun <B> foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> = f(this.fix().value, lb)
fun <B> foldRight(initial: Eval<B>, operation: (A, Eval<B>) -> Eval<B>): Eval<B> = operation(this.fix().value, initial)

fun <B> coflatMap(f: (IdOf<A>) -> B): Id<B> = this.fix().map({ f(this) })

Expand Down
16 changes: 8 additions & 8 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Option.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ sealed class Option<out A> : OptionOf<A> {
p1.map { pp1 -> f(get(), pp1) }
}

inline fun <R> fold(ifEmpty: () -> R, some: (A) -> R): R = when (this) {
inline fun <R> fold(ifEmpty: () -> R, ifSome: (A) -> R): R = when (this) {
is None -> ifEmpty()
is Some<A> -> some(t)
is Some<A> -> ifSome(t)
}

/**
Expand Down Expand Up @@ -132,19 +132,19 @@ sealed class Option<out A> : OptionOf<A> {
if (nonEmpty()) f(get())
}

fun <B> foldLeft(b: B, f: (B, A) -> B): B =
fun <B> foldLeft(initial: B, operation: (B, A) -> B): B =
this.fix().let { option ->
when (option) {
is Some -> f(b, option.t)
is None -> b
is Some -> operation(initial, option.t)
is None -> initial
}
}

fun <B> foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> =
fun <B> foldRight(initial: Eval<B>, operation: (A, Eval<B>) -> Eval<B>): Eval<B> =
this.fix().let { option ->
when (option) {
is Some -> f(option.t, lb)
is None -> lb
is Some -> operation(option.t, initial)
is None -> initial
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ infix fun <A, B> PartialFunction<A, B>.orElse(f: PartialFunction<A, B>): Partial

}

fun <A, B> PartialFunction(definedAt: (A) -> Boolean, f: (A) -> B): PartialFunction<A, B> =
fun <A, B> PartialFunction(definedAt: (A) -> Boolean, ifDefined: (A) -> B): PartialFunction<A, B> =
object : PartialFunction<A, B>() {
override fun invoke(p1: A): B {
if (definedAt(p1)) {
return f(p1)
return ifDefined(p1)
} else {
throw IllegalArgumentException("Value: ($p1) isn't supported by this function")
}
Expand Down
18 changes: 9 additions & 9 deletions modules/core/arrow-core/src/main/kotlin/arrow/core/Try.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ sealed class Try<out A> : TryOf<A> {
)

/**
* Applies `fa` if this is a `Failure` or `fb` if this is a `Success`.
* Applies `ifFailure` if this is a `Failure` or `ifSuccess` if this is a `Success`.
*/
inline fun <B> fold(fa: (Throwable) -> B, fb: (A) -> B): B =
inline fun <B> fold(ifFailure: (Throwable) -> B, ifSuccess: (A) -> B): B =
when (this) {
is Failure -> fa(exception)
is Success -> fb(value)
is Failure -> ifFailure(exception)
is Success -> ifSuccess(value)
}

abstract fun isFailure(): Boolean
Expand Down Expand Up @@ -130,9 +130,9 @@ sealed class Try<out A> : TryOf<A> {
@Deprecated("arrow.data.Either is already right biased. This function will be removed in future releases", ReplaceWith("toEither()"))
fun toDisjunction(): Disjunction<Throwable, A> = toEither().toDisjunction()

fun <B> foldLeft(b: B, f: (B, A) -> B): B = this.fix().fold({ b }, { f(b, it) })
fun <B> foldLeft(initial: B, operation: (B, A) -> B): B = this.fix().fold({ initial }, { operation(initial, it) })

fun <B> foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> = this.fix().fold({ lb }, { f(it, lb) })
fun <B> foldRight(initial: Eval<B>, operation: (A, Eval<B>) -> Eval<B>): Eval<B> = this.fix().fold({ initial }, { operation(it, initial) })

/**
* The `Failure` type represents a computation that result in an exception.
Expand Down Expand Up @@ -202,10 +202,10 @@ fun <B> TryOf<B>.recover(f: (Throwable) -> B): Try<B> = fix().fold({ Success(f(i
fun <A> TryOf<A>.handle(f: (Throwable) -> A): Try<A> = fix().recover(f)

/**
* Completes this `Try` by applying the function `f` to this if this is of type `Failure`,
* or conversely, by applying `s` if this is a `Success`.
* Completes this `Try` by applying the function `ifFailure` to this if this is of type `Failure`,
* or conversely, by applying `ifSuccess` if this is a `Success`.
*/
fun <A, B> TryOf<A>.transform(s: (A) -> TryOf<B>, f: (Throwable) -> TryOf<B>): Try<B> = fix().fold({ f(it).fix() }, { fix().flatMap(s) })
fun <A, B> TryOf<A>.transform(ifSuccess: (A) -> TryOf<B>, ifFailure: (Throwable) -> TryOf<B>): Try<B> = fix().fold({ ifFailure(it).fix() }, { fix().flatMap(ifSuccess) })

fun <A> (() -> A).try_(): Try<A> = Try(this)

Expand Down

0 comments on commit dc286fb

Please sign in to comment.