Last active
April 22, 2024 11:17
-
-
Save ademar111190/ad3dac7a3eb26ed23ab54792f3d857f1 to your computer and use it in GitHub Desktop.
An Either implementation in kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package functional | |
sealed class Either<L, R> { | |
abstract fun fold(left: (L) -> Unit, right: (R) -> Unit) | |
abstract fun <ML> mapLeft(f: (L) -> ML): Either<ML, R> | |
abstract fun <MR> mapRight(f: (R) -> MR): Either<L, MR> | |
abstract fun <ML, MR> map(leftF: (L) -> ML, rightF: (R) -> MR): Either<ML, MR> | |
abstract fun <ML> flatMapLeft(f: (L) -> Either<ML, R>): Either<ML, R> | |
abstract fun <MR> flatMapRight(f: (R) -> Either<L, MR>): Either<L, MR> | |
abstract fun <ML, MR> flatMap(leftF: (L) -> Either<ML, MR>, rightF: (R) -> Either<ML, MR>): Either<ML, MR> | |
abstract fun filterLeft(filter: (L) -> Boolean, supplier: () -> R): Either<L, R> | |
abstract fun filterRight(filter: (R) -> Boolean, supplier: () -> L): Either<L, R> | |
} | |
data class Left<L, R>( | |
private val value: L | |
) : Either<L, R>() { | |
override fun fold(left: (L) -> Unit, right: (R) -> Unit) = left(value) | |
override fun <ML> mapLeft(f: (L) -> ML) = Left<ML, R>(f(value)) | |
override fun <MR> mapRight(f: (R) -> MR) = Left<L, MR>(value) | |
override fun <ML, MR> map(leftF: (L) -> ML, rightF: (R) -> MR) = Left<ML, MR>(leftF(value)) | |
override fun <ML> flatMapLeft(f: (L) -> Either<ML, R>) = f(value) | |
override fun <MR> flatMapRight(f: (R) -> Either<L, MR>) = Left<L, MR>(value) | |
override fun <ML, MR> flatMap(leftF: (L) -> Either<ML, MR>, rightF: (R) -> Either<ML, MR>) = leftF(value) | |
override fun filterLeft(filter: (L) -> Boolean, supplier: () -> R) = if (filter(value)) this else Right<L, R>(supplier()) | |
override fun filterRight(filter: (R) -> Boolean, supplier: () -> L) = this | |
} | |
data class Right<L, R>( | |
private val value: R | |
) : Either<L, R>() { | |
override fun fold(left: (L) -> Unit, right: (R) -> Unit) = right(value) | |
override fun <ML> mapLeft(f: (L) -> ML) = Right<ML, R>(value) | |
override fun <MR> mapRight(f: (R) -> MR) = Right<L, MR>(f(value)) | |
override fun <ML, MR> map(leftF: (L) -> ML, rightF: (R) -> MR) = Right<ML, MR>(rightF(value)) | |
override fun <ML> flatMapLeft(f: (L) -> Either<ML, R>) = Right<ML, R>(value) | |
override fun <MR> flatMapRight(f: (R) -> Either<L, MR>) = f(value) | |
override fun <ML, MR> flatMap(leftF: (L) -> Either<ML, MR>, rightF: (R) -> Either<ML, MR>) = rightF(value) | |
override fun filterLeft(filter: (L) -> Boolean, supplier: () -> R) = this | |
override fun filterRight(filter: (R) -> Boolean, supplier: () -> L) = if (filter(value)) this else Left<L, R>(supplier()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package functional | |
class EitherTest { | |
object LEFT | |
object RIGHT | |
object MAP | |
private var calls = 0 | |
@Before | |
fun setUp() { | |
calls = 0 | |
} | |
@Test | |
fun `fold with Left should call only the left consumer`() { | |
makeLeft().fold(this::assertLeft, this::failRight) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `fold with Right should call only the right consumer`() { | |
makeRight().fold(this::failLeft, this::assertRight) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `mapLeft with Left should map correctly`() { | |
val either = makeLeft().mapLeft(this::mapLeft) | |
assertEquals(Left(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `mapLeft with Right should pass`() { | |
val either = makeRight().mapLeft(this::failLeft) | |
assertEquals(Right(RIGHT), either) | |
} | |
@Test | |
fun `mapRight with Left should pass`() { | |
val either = makeLeft().mapRight(this::failRight) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `mapRight with Right should map correctly`() { | |
val either = makeRight().mapRight(this::mapRight) | |
assertEquals(Right(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `map with Left should map correctly through leftMapper`() { | |
val either = makeLeft().map(this::mapLeft, this::failRight) | |
assertEquals(Left(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `map with Right should map correctly through rightMapper`() { | |
val either = makeRight().map(this::failLeft, this::mapRight) | |
assertEquals(Right(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `flatMapLeft with Left should map correctly`() { | |
val either = makeLeft().flatMapLeft(this::flatMapLeft) | |
assertEquals(Left(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `flatMapLeft with Right should pass`() { | |
val either = makeRight().flatMapLeft(this::flatMapLeft) | |
assertEquals(Right(RIGHT), either) | |
} | |
@Test | |
fun `flatMapRight with Left should pass`() { | |
val either = makeLeft().flatMapRight(this::flatMapRight) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `flatMapRight with Right should map correctly`() { | |
val either = makeRight().flatMapRight(this::flatMapRight) | |
assertEquals(Right(MAP), either) | |
} | |
@Test | |
fun `flatMap with Left should map correctly through leftMapper`() { | |
val either = makeLeft().flatMap(this::flatMapLeft, this::failRight) | |
assertEquals(Left(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `flatMap with Right should map correctly through rightMapper`() { | |
val either = makeRight().flatMap(this::failLeft, this::flatMapRight) | |
assertEquals(Right(MAP), either) | |
assertEquals(1, calls) | |
} | |
@Test | |
fun `filterLeft on Left when filter stop should return the supplied`() { | |
val either = makeLeft().filterLeft(this::filterStop, this::supplyRight) | |
assertEquals(Right(RIGHT), either) | |
} | |
@Test | |
fun `filterLeft on Left when filter pass should return the original`() { | |
val either = makeLeft().filterLeft(this::filterPass, this::supplyRight) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `filterLeft on Right when filter stop should return the original right`() { | |
val either = makeRight().filterLeft(this::filterStop, this::supplyRight) | |
assertEquals(Right(RIGHT), either) | |
} | |
@Test | |
fun `filterLeft on Right when filter pass should return the original right`() { | |
val either = makeRight().filterLeft(this::filterPass, this::supplyRight) | |
assertEquals(Right(RIGHT), either) | |
} | |
@Test | |
fun `filterRight on Left when filter stop should return the original left`() { | |
val either = makeLeft().filterRight(this::filterStop, this::supplyLeft) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `filterRight on Left when filter pass should return the original left`() { | |
val either = makeLeft().filterRight(this::filterPass, this::supplyLeft) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `filterRight on Right when filter stop should return the supplied`() { | |
val either = makeRight().filterRight(this::filterStop, this::supplyLeft) | |
assertEquals(Left(LEFT), either) | |
} | |
@Test | |
fun `filterRight on Right when filter pass should return the original`() { | |
val either = makeRight().filterRight(this::filterPass, this::supplyLeft) | |
assertEquals(Right(RIGHT), either) | |
} | |
private fun supplyLeft() = LEFT | |
private fun supplyRight() = RIGHT | |
private fun makeLeft(): Either<LEFT, RIGHT> = Left(supplyLeft()) | |
private fun makeRight(): Either<LEFT, RIGHT> = Right(supplyRight()) | |
private fun failLeft(it: Any = Any()): Nothing = fail("Left shouldn't be called, $it") | |
private fun failRight(it: Any = Any()): Nothing = fail("Right shouldn't be called, $it") | |
private fun filterPass(it: Any = Any()) = true | |
private fun filterStop(it: Any = Any()) = false | |
private fun flatMapLeft(it: LEFT): Either<MAP, RIGHT> { | |
assertLeft(it) | |
return Left(MAP) | |
} | |
private fun flatMapRight(it: RIGHT): Either<LEFT, MAP> { | |
assertRight(it) | |
return Right(MAP) | |
} | |
private fun mapLeft(it: LEFT): MAP { | |
assertLeft(it) | |
return MAP | |
} | |
private fun mapRight(it: RIGHT): MAP { | |
assertRight(it) | |
return MAP | |
} | |
private fun assertLeft(it: LEFT) { | |
assertEquals(LEFT, it) | |
calls++ | |
} | |
private fun assertRight(it: RIGHT) { | |
assertEquals(RIGHT, it) | |
calls++ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <http://unlicense.org/> |
Thank you @BKAUTO I did that while studying so I'm sure there are improvement opportunities.
I added a license file so feel free to use ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. This is a nice implementation.
Do you have any copyright/license specified for the usage?