Skip to content

myeonginwoo/KtEither

Repository files navigation

KtEither

Kotlin Either Lib

Installation

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
	
dependencies {
    	compile 'com.github.myeonginwoo:KtEither:1.0.0'
}

Examples

Left

val eitherLeft: Either<String, Int> = Left("test")
println(eitherLeft.left?.get)   // "test"

Right

val rightEither: Either<String, Int> = Right(6)
println(rightEither.right?.get) // 6

try-catch

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

fold

val leftEither: Either<String, Int> = Left("test")
println(leftEither.fold({ "Left is $it" }, { it * 3 }))  // "Left is test"

val rightEither: Either<String, Int> = Right(6)
pirntln(rightEither.fold({ "Left is $it" }, { it * 3 })) // 18)

map

//left
val either: Either<String, Int> = Left("123")
println(either.left?.map(String::toInt)?.get)   // 123

   
//right
val either: Either<String, Int> = Right(123)
println(either.right?.map(Int::toDouble)?.get)  // 123.0

filter

//left
val either: Either<String, Int> = Left("left")
println(either.left?.filter { it.length > 3 }.get)  //left
println(either.left?.filter { it.length < 3 }.get)  //null

//right
val either: Either<String, Int> = Right(126)
println(either.right?.filter { it > 100 }?.get)     //126
println(either.right?.filter { it < 100 }?.get)     //null

exists

val leftEither: Either<String, Int> = Left("test")
assertEquals(leftEither.left?.exists { it.length == 4 }, true)
assertEquals(leftEither.left?.exists { it.length > 4 }, false)

val rightEither: Either<String, Int> = Right(6)
assertEquals(rightEither.right?.exists { it > 5 }, true)
assertEquals(rightEither.right?.exists { it < 5 }, false)

flatmap

val leftEither: Either<String, Int> = Left("test")
assertEquals(leftEither.left?.flatMap { Left<Int, Long>(3) }?.left?.get, 3)

val rightEither: Either<String, Int> = Right(6)
assertEquals(rightEither.right?.flatMap { Right<Int, String>("test") }?.right?.get, "test")