Skip to content

Commit

Permalink
Merge pull request #1 from myeonginwoo/creatEither
Browse files Browse the repository at this point in the history
Impl : Impl LeftProjection, RightProjection And TestCode
  • Loading branch information
myeonginwoo authored Dec 15, 2016
2 parents 29cf8ba + b52531c commit ef3c4c7
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
22 changes: 7 additions & 15 deletions Either/src/main/kotlin/Either.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,25 @@ package main.kotlin
/**
* Created by Lazysoul on 2016. 12. 14..
*/
sealed class Either<L : Any, R : Any> {
sealed class Either<L, R> {

var isLeft: Boolean = false
var isRight: Boolean = false

var left: Left<L, Nothing>? = null
var right: Right<Nothing, R>? = null
abstract fun get(): Any
var left: LeftProjection<L, Nothing>? = null
var right: RightProjection<Nothing, R>? = null

class Left<L : Any, R : Any>(val value: L) : Either<L, R>() {
class Left<L, R>(value: L) : Either<L, R>() {
init {
isLeft = true
left = LeftProjection(value)
}

override fun get(): L {
return value
}

}

class Right<L : Any, R : Any>(val value: R) : Either<L, R>() {
class Right<L, R>(value: R) : Either<L, R>() {
init {
isRight = true
}

override fun get(): R {
return value
right = RightProjection(value)
}
}
}
13 changes: 13 additions & 0 deletions Either/src/main/kotlin/LeftProjection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main.kotlin

/**
* Created by Lazysoul on 2016. 12. 15..
*/
class LeftProjection<out L,out R>(val value: L) {

val get: L get() = value

fun <X> map(f: (L) -> X): LeftProjection<X, R> {
return LeftProjection(f(value))
}
}
13 changes: 13 additions & 0 deletions Either/src/main/kotlin/RightProjection.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main.kotlin

/**
* Created by Lazysoul on 2016. 12. 15..
*/
class RightProjection<out L, out R>(val value: R) {

val get: R get() = value

fun <X : Any> map(f: (R) -> X): RightProjection<L, X> {
return RightProjection(f(value))
}
}
44 changes: 34 additions & 10 deletions Either/src/test/kotlin/EitherTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,42 @@ import org.junit.Test
class EitherTest {

@Test fun leftTest() {
val left: Either<String, Int> = Left("test")
assertEquals("test", left.get())
assertEquals(true, left.isLeft)
assertEquals(false, left.isRight)
assertEquals(null, left.right?.get())
val eitherLeft: Either<String, Int> = Left("test")
assertEquals("test", eitherLeft.left?.get)
assertEquals(true, eitherLeft.isLeft)
assertEquals(false, eitherLeft.isRight)
}

@Test fun rightTest() {
val right: Either<String, Int> = Right(6)
assertEquals(6, right.get())
assertEquals(false, right.isLeft)
assertEquals(true, right.isRight)
assertEquals(null, right.left?.get())
val rightEither: Either<String, Int> = Right(6)
assertEquals(6, rightEither.right?.get)
assertEquals(false, rightEither.isLeft)
assertEquals(true, rightEither.isRight)
}

@Test fun tryTest() {
val stringValue = "test"
val either: Either<String, Int> = try {
Right(stringValue.toInt())
} catch (e: Exception) {
Left(e.toString())
}

assertEquals(true, either.isLeft)
assertEquals(false, either.isRight)
}

@Test fun leftMapTest() {
val either: Either<String, Int> = Left("123")

assertEquals(123, either.left?.map(String::toInt)?.get)
assertEquals("123test", either.left?.map { it + "test" }?.get)
}

@Test fun rightMapTest() {
val either: Either<String, Int> = Right(123)

assertEquals(123.0, either.right?.map(Int::toDouble)?.get)
assertEquals(246, either.right?.map { it * 2 }?.get)
}
}

0 comments on commit ef3c4c7

Please sign in to comment.