Skip to content

Commit abe7ef5

Browse files
Merge pull request #4
* implement maybe monad as maybe extension function for non nullable el… * implement maybe monad as maybe extension function for nullable element * implement either monad as right either extension function * implement either monad as left either extension function * remove shallow from dev test github action
1 parent 9551e50 commit abe7ef5

5 files changed

Lines changed: 71 additions & 2 deletions

File tree

.github/workflows/dev-test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ jobs:
1111
steps:
1212
- name: Check out repository code
1313
uses: actions/checkout@v5
14+
with:
15+
fetch-depth: 0
1416
- name: List files in the repository
1517
run: |
1618
ls ${{ github.workspace }}

library/src/commonMain/kotlin/com/benromdhane/omar/offroadsoft/monad/Either.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,7 @@ fun <ELEMENT> Either<ELEMENT, ELEMENT>.toFilteredMaybe(condition: (ELEMENT) -> B
162162

163163
fun <ELEMENT> Either<ELEMENT, ELEMENT>.toMaybe() =
164164
if (this.right()) this.toMaybeRight()
165-
else this.toMaybeLeft()
165+
else this.toMaybeLeft()
166+
167+
fun <LEFT, ELEMENT> ELEMENT.asRightEither() = Either.Right.of<LEFT, _>(this)
168+
fun <ELEMENT, RIGHT> ELEMENT.asLeftEither() = Either.Left.of<_, RIGHT>(this)

library/src/commonMain/kotlin/com/benromdhane/omar/offroadsoft/monad/Maybe.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.benromdhane.omar.offroadsoft.monad
22

3+
import kotlin.jvm.JvmName
4+
35
sealed interface Maybe<ELEMENT> {
46

57
fun present(): Boolean
@@ -65,4 +67,13 @@ sealed interface Maybe<ELEMENT> {
6567
class EmptyMaybeException : Throwable()
6668
}
6769

68-
fun <ELEMENT> Maybe<Maybe<ELEMENT>>.flatten() = this.flatMap { it }
70+
fun <ELEMENT> Maybe<Maybe<ELEMENT>>.flatten() = this.flatMap { it }
71+
72+
@JvmName("nonNullableAsMaybe")
73+
fun <ELEMENT : Any> ELEMENT.asMaybe() = Maybe.NotEmpty.of(this)
74+
75+
@JvmName("nullableAsMaybe")
76+
fun <ELEMENT : Any?> ELEMENT.asMaybe() =
77+
this
78+
?.let { Maybe.NotEmpty.of<ELEMENT>(it) }
79+
?: Maybe.Empty.of()

library/src/commonTest/kotlin/com/benromdhane/omar/offroadsoft/monad/EitherTest.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,4 +1027,26 @@ class EitherTest {
10271027

10281028
assertEquals(initialElement, result)
10291029
}
1030+
1031+
@Test
1032+
fun `as right either must return right either with initial element`() {
1033+
val initialElement = Uuid.random().toString()
1034+
val result =
1035+
initialElement.asRightEither<Int, _>()
1036+
.toMaybeRight()
1037+
.orNull()!!
1038+
1039+
assertEquals(initialElement, result)
1040+
}
1041+
1042+
@Test
1043+
fun `as left either must return left either with initial element`() {
1044+
val initialElement: String = Uuid.random().toString()
1045+
val result =
1046+
initialElement.asLeftEither<_, Int>()
1047+
.toMaybeLeft()
1048+
.orNull()!!
1049+
1050+
assertEquals(initialElement, result)
1051+
}
10301052
}

library/src/commonTest/kotlin/com/benromdhane/omar/offroadsoft/monad/MaybeTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,35 @@ class MaybeTest {
340340

341341
assertTrue { result }
342342
}
343+
344+
@Test
345+
fun `as maybe must return not empty maybe with the initial element for non nullable element`() {
346+
val initialElement = Uuid.random().toString()
347+
val result =
348+
initialElement.asMaybe()
349+
.orNull()!!
350+
351+
assertEquals(initialElement, result)
352+
}
353+
354+
@Test
355+
fun `as maybe must return not empty maybe with the initial element for nullable non null element`() {
356+
@Suppress("RedundantNullableReturnType")
357+
val initialElement: String? = Uuid.random().toString()
358+
val result =
359+
initialElement.asMaybe()
360+
.orNull()!!
361+
362+
assertEquals(initialElement, result)
363+
}
364+
365+
@Test
366+
fun `as maybe must return empty maybe for nullable null element`() {
367+
val initialElement: String? = null
368+
val result =
369+
initialElement.asMaybe()
370+
.empty()
371+
372+
assertTrue { result }
373+
}
343374
}

0 commit comments

Comments
 (0)