Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl : Impl LeftProjection, RightProjection And TestCode #1

Merged
merged 1 commit into from
Dec 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}