Skip to content

Commit

Permalink
rc
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioAriasC committed Feb 4, 2016
1 parent ac68530 commit a7f1149
Show file tree
Hide file tree
Showing 11 changed files with 701 additions and 937 deletions.
24 changes: 22 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>org.funktionale</groupId>
<artifactId>funktionale</artifactId>
<version>0.6_1.0.0-beta-3594</version>
<version>0.6_1.0.0-rc</version>
<packaging>jar</packaging>
<name>funKTionale</name>
<description>Functional constructs and utilities for Kotlin</description>
Expand All @@ -34,6 +34,26 @@
<distribution>repo</distribution>
</license>
</licenses>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-kotlin-eap</id>
<name>bintray</name>
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>bintray-kotlin-kotlin-eap</id>
<name>bintray-plugins</name>
<url>http://dl.bintray.com/kotlin/kotlin-eap</url>
</pluginRepository>
</pluginRepositories>
<scm>
<url>https://github.com/MarioAriasC/funKTionale</url>
<connection>[email protected]:MarioAriasC/funKTionale.git</connection>
Expand All @@ -48,7 +68,7 @@
</developer>
</developers>
<properties>
<kotlin.version>1.0.0-beta-3594</kotlin.version>
<kotlin.version>1.0.0-rc-1036</kotlin.version>
</properties>
<dependencies>
<dependency>
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/org/funktionale/composition/namespace.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package org.funktionale.composition

infix public fun<P1, IP, R> Function1<P1, IP>.andThen(f: (IP) -> R): (P1) -> R = forwardCompose(f)
infix fun<P1, IP, R> Function1<P1, IP>.andThen(f: (IP) -> R): (P1) -> R = forwardCompose(f)

infix public fun<P1, IP, R> Function1<P1, IP>.forwardCompose(f: (IP) -> R): (P1) -> R {
infix fun<P1, IP, R> Function1<P1, IP>.forwardCompose(f: (IP) -> R): (P1) -> R {
return { p1: P1 -> f(this(p1)) }
}

infix public fun<IP, R, P1> Function1<IP, R>.compose(f: (P1) -> IP): (P1) -> R {
infix fun<IP, R, P1> Function1<IP, R>.compose(f: (P1) -> IP): (P1) -> R {
return { p1: P1 -> this(f(p1)) }
}
84 changes: 42 additions & 42 deletions src/main/kotlin/org/funktionale/currying/namespace.kt

Large diffs are not rendered by default.

55 changes: 26 additions & 29 deletions src/main/kotlin/org/funktionale/either/Either.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,36 @@ import org.funktionale.either.Either.Right
* Time: 19:01
*/
@Suppress("BASE_WITH_NULLABLE_UPPER_BOUND")
sealed public class Either<out L, out R> {
sealed class Either<out L, out R> {

public fun left(): LeftProjection<L, R> = LeftProjection(this)
public fun right(): RightProjection<L, R> = RightProjection(this)
fun left(): LeftProjection<L, R> = LeftProjection(this)
fun right(): RightProjection<L, R> = RightProjection(this)

operator public abstract fun component1(): L?
operator public abstract fun component2(): R?
operator abstract fun component1(): L?
operator abstract fun component2(): R?

public abstract fun isLeft(): Boolean
public abstract fun isRight(): Boolean
abstract fun isLeft(): Boolean
abstract fun isRight(): Boolean

public fun<X> fold(fl: (L) -> X, fr: (R) -> X): X {
fun<X> fold(fl: (L) -> X, fr: (R) -> X): X {
return when (this) {
is Left<L, R> -> fl(this.l)
is Right<L, R> -> fr(this.r)
}
}

public fun swap(): Either<R, L> {
fun swap(): Either<R, L> {
return when (this) {
is Left<L, R> -> Right(this.l)
is Right<L, R> -> Left(this.r)
}
}

@Suppress("BASE_WITH_NULLABLE_UPPER_BOUND")
public class Left<out L, out R>(val l: L) : Either<L, R>() {
public override fun component1(): L? = l
public override fun component2(): R? = null
public override fun isLeft(): Boolean = true
public override fun isRight(): Boolean = false
@Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") class Left<out L, out R>(val l: L) : Either<L, R>() {
override fun component1(): L? = l
override fun component2(): R? = null
override fun isLeft(): Boolean = true
override fun isRight(): Boolean = false

override fun equals(other: Any?): Boolean {
return when (other) {
Expand All @@ -76,12 +75,11 @@ sealed public class Either<out L, out R> {
}
}

@Suppress("BASE_WITH_NULLABLE_UPPER_BOUND")
public class Right<out L, out R>(val r: R) : Either<L, R>() {
public override fun component1(): L? = null
public override fun component2(): R? = r
public override fun isLeft(): Boolean = false
public override fun isRight(): Boolean = true
@Suppress("BASE_WITH_NULLABLE_UPPER_BOUND") class Right<out L, out R>(val r: R) : Either<L, R>() {
override fun component1(): L? = null
override fun component2(): R? = r
override fun isLeft(): Boolean = false
override fun isRight(): Boolean = true

override fun equals(other: Any?): Boolean {
return when (other) {
Expand All @@ -100,36 +98,35 @@ sealed public class Either<out L, out R> {
}
}

public fun<T> Either<T, T>.merge(): T {
fun<T> Either<T, T>.merge(): T {
return when (this) {
is Left<T, T> -> this.l
is Right<T, T> -> this.r
else -> throw UnsupportedOperationException()
}
}

public fun<L, R> Pair<L, R>.toLeft(): Left<L, R> {
fun<L, R> Pair<L, R>.toLeft(): Left<L, R> {
return Left(this.component1())
}

public fun<L, R> Pair<L, R>.toRight(): Right<L, R> {
fun<L, R> Pair<L, R>.toRight(): Right<L, R> {
return Right(this.component2())
}

@Deprecated("Use eitherTry", ReplaceWith("eitherTry(body)"))
public fun<T> either(body: () -> T): Either<Exception, T> {
@Deprecated("Use eitherTry", ReplaceWith("eitherTry(body)")) fun<T> either(body: () -> T): Either<Exception, T> {
return eitherTry(body)
}

public fun<T> eitherTry(body: () -> T): Either<Exception, T> {
fun<T> eitherTry(body: () -> T): Either<Exception, T> {
return try {
Right(body())
} catch(e: Exception) {
Left(e)
}
}

public fun<T, L, R> List<T>.traverse(f: (T) -> Either<L, R>): Either<L, List<R>> {
fun<T, L, R> List<T>.traverse(f: (T) -> Either<L, R>): Either<L, List<R>> {
return foldRight(Right(emptyList())) { i: T, accumulator: Either<L, List<R>> ->
val either = f(i)
when (either) {
Expand All @@ -142,7 +139,7 @@ public fun<T, L, R> List<T>.traverse(f: (T) -> Either<L, R>): Either<L, List<R>>
}
}

public fun<L, R> List<Either<L, R>>.sequential(): Either<L, List<R>> {
fun<L, R> List<Either<L, R>>.sequential(): Either<L, List<R>> {
return traverse { it }
}

22 changes: 11 additions & 11 deletions src/main/kotlin/org/funktionale/either/LeftProjection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ import java.util.*
* Date: 17/05/13
* Time: 20:20
*/
public class LeftProjection<out L, out R>(val e: Either<L, R>) {
class LeftProjection<out L, out R>(val e: Either<L, R>) {

public fun get(): L {
fun get(): L {
return when (e) {
is Left<L, R> -> e.l
else -> throw NoSuchElementException("Either.left.value on Right")
}
}

public fun forEach(f: (L) -> Unit) {
fun forEach(f: (L) -> Unit) {
return when (e) {
is Left<L, R> -> f(e.l)
else -> {
Expand All @@ -47,19 +47,19 @@ public class LeftProjection<out L, out R>(val e: Either<L, R>) {
}


public fun exists(predicate: (L) -> Boolean): Boolean {
fun exists(predicate: (L) -> Boolean): Boolean {
return when (e) {
is Left<L, R> -> predicate(e.l)
else -> false
}
}


public fun<X> map(f: (L) -> X): Either<X, R> {
fun<X> map(f: (L) -> X): Either<X, R> {
return flatMap { Left<X, R>(f(it)) }
}

public fun filter(predicate: (L) -> Boolean): Option<Either<L, R>> {
fun filter(predicate: (L) -> Boolean): Option<Either<L, R>> {
return when (e) {
is Left<L, R> -> {
if (predicate(e.l)) {
Expand All @@ -72,14 +72,14 @@ public class LeftProjection<out L, out R>(val e: Either<L, R>) {
}
}

public fun toList(): List<L> {
fun toList(): List<L> {
return when (e) {
is Left<L, R> -> listOf(e.l)
else -> listOf()
}
}

public fun toOption(): Option<L> {
fun toOption(): Option<L> {
return when (e) {
is Left<L, R> -> Some(e.l)
else -> None
Expand All @@ -88,18 +88,18 @@ public class LeftProjection<out L, out R>(val e: Either<L, R>) {

}

public fun<L, R, X> LeftProjection<L, R>.flatMap(f: (L) -> Either<X, R>): Either<X, R> {
fun<L, R, X> LeftProjection<L, R>.flatMap(f: (L) -> Either<X, R>): Either<X, R> {
return when (e) {
is Left<L, R> -> f(e.l)
is Right<L, R> -> Right(e.r)
}
}

public fun<L, R, X, Y> LeftProjection<L, R>.map(x: Either<X, R>, f: (L, X) -> Y): Either<Y, R> {
fun<L, R, X, Y> LeftProjection<L, R>.map(x: Either<X, R>, f: (L, X) -> Y): Either<Y, R> {
return flatMap { l -> x.left().map { xx -> f(l, xx) } }
}

public fun<R, L> LeftProjection<L, R>.getOrElse(default: () -> L): L {
fun<R, L> LeftProjection<L, R>.getOrElse(default: () -> L): L {
return when (e) {
is Left<L, R> -> e.l
else -> default()
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/org/funktionale/either/RightProjection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ import java.util.*
* Date: 17/05/13
* Time: 20:20
*/
public class RightProjection<out L, out R>(val e: Either<L, R>) {
class RightProjection<out L, out R>(val e: Either<L, R>) {

public fun get(): R {
fun get(): R {
return when (e) {
is Right<L, R> -> e.r
else -> throw NoSuchElementException("Either.right.value on Left")
}
}

public fun forEach(f: (R) -> Unit) {
fun forEach(f: (R) -> Unit) {
return when (e) {
is Right<L, R> -> f(e.r)
else -> {
Expand All @@ -47,18 +47,18 @@ public class RightProjection<out L, out R>(val e: Either<L, R>) {
}


public fun exists(predicate: (R) -> Boolean): Boolean {
fun exists(predicate: (R) -> Boolean): Boolean {
return when (e) {
is Right<L, R> -> predicate(e.r)
else -> false
}
}

public fun<X> map(f: (R) -> X): Either<L, X> {
fun<X> map(f: (R) -> X): Either<L, X> {
return flatMap { Right<L, X>(f(it)) }
}

public fun filter(predicate: (R) -> Boolean): Option<Either<L, R>> {
fun filter(predicate: (R) -> Boolean): Option<Either<L, R>> {
return when (e) {
is Right<L, R> -> {
if (predicate(e.r)) {
Expand All @@ -71,14 +71,14 @@ public class RightProjection<out L, out R>(val e: Either<L, R>) {
}
}

public fun toList(): List<R> {
fun toList(): List<R> {
return when (e) {
is Right<L, R> -> listOf(e.r)
else -> listOf()
}
}

public fun toOption(): Option<R> {
fun toOption(): Option<R> {
return when (e) {
is Right<L, R> -> Some(e.r)
else -> None
Expand All @@ -87,21 +87,21 @@ public class RightProjection<out L, out R>(val e: Either<L, R>) {

}

public fun<L, R> RightProjection<L, R>.getOrElse(default: () -> R): R {
fun<L, R> RightProjection<L, R>.getOrElse(default: () -> R): R {
return when (e) {
is Right<L, R> -> e.r
else -> default()
}
}

public fun<X, L, R> RightProjection<L, R>.flatMap(f: (R) -> Either<L, X>): Either<L, X> {
fun<X, L, R> RightProjection<L, R>.flatMap(f: (R) -> Either<L, X>): Either<L, X> {
return when (e) {
is Left<L, R> -> Left(e.l)
is Right<L, R> -> f(e.r)
}
}


public fun<L, R, X, Y> RightProjection<L, R>.map(x: Either<L, X>, f: (R, X) -> Y): Either<L, Y> {
fun<L, R, X, Y> RightProjection<L, R>.map(x: Either<L, X>, f: (R, X) -> Y): Either<L, Y> {
return flatMap { r -> x.right().map { xx -> f(r, xx) } }
}
Loading

0 comments on commit a7f1149

Please sign in to comment.