Skip to content

Commit 2c4cefe

Browse files
Vizsorojzheaux
authored andcommitted
Arrow in Kotlin
Issue: BAEL-1495
1 parent 4d30ff3 commit 2c4cefe

6 files changed

Lines changed: 352 additions & 0 deletions

File tree

‎kotlin-libraries/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
<artifactId>h2</artifactId>
8888
<version>${h2database.version}</version>
8989
</dependency>
90+
<!-- https://mvnrepository.com/artifact/io.arrow-kt/arrow-core -->
91+
<dependency>
92+
<groupId>io.arrow-kt</groupId>
93+
<artifactId>arrow-core</artifactId>
94+
<version>0.7.3</version>
95+
</dependency>
96+
9097
</dependencies>
9198

9299
<properties>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.kotlin.arrow
2+
3+
import arrow.core.Either
4+
import arrow.core.filterOrElse
5+
import kotlin.math.sqrt
6+
7+
class FunctionalErrorHandlingWithEither {
8+
9+
sealed class ComputeProblem {
10+
object OddNumber : ComputeProblem()
11+
object NotANumber : ComputeProblem()
12+
}
13+
14+
fun parseInput(s : String) : Either<ComputeProblem, Int> = Either.cond(s.toIntOrNull() != null, {-> s.toInt()}, {->ComputeProblem.NotANumber} )
15+
16+
fun isEven(x : Int) : Boolean = x % 2 == 0
17+
18+
fun biggestDivisor(x: Int) : Int = biggestDivisor(x, 2)
19+
20+
fun biggestDivisor(x : Int, y : Int) : Int {
21+
if(x == y){
22+
return 1;
23+
}
24+
if(x % y == 0){
25+
return x / y;
26+
}
27+
return biggestDivisor(x, y+1)
28+
}
29+
30+
fun isSquareNumber(x : Int) : Boolean {
31+
val sqrt: Double = sqrt(x.toDouble())
32+
return sqrt % 1.0 == 0.0
33+
}
34+
35+
fun computeWithEither(input : String) : Either<ComputeProblem, Boolean> {
36+
return parseInput(input)
37+
.filterOrElse(::isEven) {->ComputeProblem.OddNumber}
38+
.map (::biggestDivisor)
39+
.map (::isSquareNumber)
40+
}
41+
42+
fun computeWithEitherClient(input : String) {
43+
val computeWithEither = computeWithEither(input)
44+
45+
when(computeWithEither){
46+
is Either.Right -> "The greatest divisor is square number: ${computeWithEither.b}"
47+
is Either.Left -> when(computeWithEither.a){
48+
is ComputeProblem.NotANumber -> "Wrong input! Not a number!"
49+
is ComputeProblem.OddNumber -> "It is an odd number!"
50+
}
51+
}
52+
}
53+
54+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.kotlin.arrow
2+
3+
import arrow.core.None
4+
import arrow.core.Option
5+
import arrow.core.Some
6+
import kotlin.math.sqrt
7+
8+
class FunctionalErrorHandlingWithOption {
9+
10+
fun parseInput(s : String) : Option<Int> = Option.fromNullable(s.toIntOrNull())
11+
12+
fun isEven(x : Int) : Boolean = x % 2 == 0
13+
14+
fun biggestDivisor(x: Int) : Int = biggestDivisor(x, 2)
15+
16+
fun biggestDivisor(x : Int, y : Int) : Int {
17+
if(x == y){
18+
return 1;
19+
}
20+
if(x % y == 0){
21+
return x / y;
22+
}
23+
return biggestDivisor(x, y+1)
24+
}
25+
26+
fun isSquareNumber(x : Int) : Boolean {
27+
val sqrt: Double = sqrt(x.toDouble())
28+
return sqrt % 1.0 == 0.0
29+
}
30+
31+
fun computeWithOption(input : String) : Option<Boolean> {
32+
return parseInput(input)
33+
.filter(::isEven)
34+
.map(::biggestDivisor)
35+
.map(::isSquareNumber)
36+
}
37+
38+
fun computeWithOptionClient(input : String) : String{
39+
val computeOption = computeWithOption(input)
40+
41+
return when(computeOption){
42+
is None -> "Not an even number!"
43+
is Some -> "The greatest divisor is square number: ${computeOption.t}"
44+
}
45+
}
46+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.baeldung.kotlin.arrow
2+
3+
import arrow.core.*
4+
import org.junit.Assert
5+
import org.junit.Test
6+
7+
class FunctionalDataTypes {
8+
9+
@Test
10+
fun whenIdCreated_thanValueIsPresent(){
11+
val id = Id("foo")
12+
val justId = Id.just("foo");
13+
14+
Assert.assertEquals("foo", id.extract())
15+
Assert.assertEquals(justId, id)
16+
}
17+
18+
fun length(s : String) : Int = s.length
19+
20+
fun isBigEnough(i : Int) : Boolean = i > 10
21+
22+
@Test
23+
fun whenIdCreated_thanMapIsAssociative(){
24+
val foo = Id("foo")
25+
26+
val map1 = foo.map(::length)
27+
.map(::isBigEnough)
28+
val map2 = foo.map { s -> isBigEnough(length(s)) }
29+
30+
Assert.assertEquals(map1, map2)
31+
}
32+
33+
fun lengthId(s : String) : Id<Int> = Id.just(length(s))
34+
35+
fun isBigEnoughId(i : Int) : Id<Boolean> = Id.just(isBigEnough(i))
36+
37+
@Test
38+
fun whenIdCreated_thanFlatMapIsAssociative(){
39+
val bar = Id("bar")
40+
41+
val flatMap = bar.flatMap(::lengthId)
42+
.flatMap(::isBigEnoughId)
43+
val flatMap1 = bar.flatMap { s -> lengthId(s).flatMap(::isBigEnoughId) }
44+
45+
Assert.assertEquals(flatMap, flatMap1)
46+
}
47+
48+
@Test
49+
fun whenOptionCreated_thanValueIsPresent(){
50+
val factory = Option.just(42)
51+
val constructor = Option(42)
52+
val emptyOptional = Option.empty<Integer>()
53+
val fromNullable = Option.fromNullable(null)
54+
55+
Assert.assertEquals(42, factory.getOrElse { -1 })
56+
Assert.assertEquals(factory, constructor)
57+
Assert.assertEquals(emptyOptional, fromNullable)
58+
}
59+
60+
@Test
61+
fun whenOptionCreated_thanConstructorDifferFromFactory(){
62+
val constructor : Option<String?> = Option(null)
63+
val fromNullable : Option<String?> = Option.fromNullable(null)
64+
65+
try{
66+
constructor.map { s -> s!!.length }
67+
Assert.fail()
68+
} catch (e : KotlinNullPointerException){
69+
fromNullable.map { s->s!!.length }
70+
}
71+
Assert.assertNotEquals(constructor, fromNullable)
72+
}
73+
74+
fun wrapper(x : Integer?) : Option<Int> = if (x == null) Option.just(-1) else Option.just(x.toInt())
75+
76+
@Test
77+
fun whenOptionFromNullableCreated_thanItBreaksLeftIdentity(){
78+
val optionFromNull = Option.fromNullable(null)
79+
80+
Assert.assertNotEquals(optionFromNull.flatMap(::wrapper), wrapper(null))
81+
}
82+
83+
@Test
84+
fun whenEitherCreated_thanOneValueIsPresent(){
85+
val rightOnly : Either<String,Int> = Either.right(42)
86+
val leftOnly : Either<String,Int> = Either.left("foo")
87+
88+
Assert.assertTrue(rightOnly.isRight())
89+
Assert.assertTrue(leftOnly.isLeft())
90+
Assert.assertEquals(42, rightOnly.getOrElse { -1 })
91+
Assert.assertEquals(-1, leftOnly.getOrElse { -1 })
92+
93+
Assert.assertEquals(0, rightOnly.map { it % 2 }.getOrElse { -1 })
94+
Assert.assertEquals(-1, leftOnly.map { it % 2 }.getOrElse { -1 })
95+
Assert.assertTrue(rightOnly.flatMap { Either.Right(it % 2) }.isRight())
96+
Assert.assertTrue(leftOnly.flatMap { Either.Right(it % 2) }.isLeft())
97+
}
98+
99+
@Test
100+
fun whenEvalNowUsed_thenMapEvaluatedLazily(){
101+
val now = Eval.now(1)
102+
Assert.assertEquals(1, now.value())
103+
104+
var counter : Int = 0
105+
val map = now.map { x -> counter++; x+1 }
106+
Assert.assertEquals(0, counter)
107+
108+
val value = map.value()
109+
Assert.assertEquals(2, value)
110+
Assert.assertEquals(1, counter)
111+
}
112+
113+
@Test
114+
fun whenEvalLaterUsed_theResultIsMemoized(){
115+
var counter : Int = 0
116+
val later = Eval.later { counter++; counter }
117+
Assert.assertEquals(0, counter)
118+
119+
val firstValue = later.value()
120+
Assert.assertEquals(1, firstValue)
121+
Assert.assertEquals(1, counter)
122+
123+
val secondValue = later.value()
124+
Assert.assertEquals(1, secondValue)
125+
Assert.assertEquals(1, counter)
126+
}
127+
128+
@Test
129+
fun whenEvalAlwaysUsed_theResultIsNotMemoized(){
130+
var counter : Int = 0
131+
val later = Eval.always { counter++; counter }
132+
Assert.assertEquals(0, counter)
133+
134+
val firstValue = later.value()
135+
Assert.assertEquals(1, firstValue)
136+
Assert.assertEquals(1, counter)
137+
138+
val secondValue = later.value()
139+
Assert.assertEquals(2, secondValue)
140+
Assert.assertEquals(2, counter)
141+
}
142+
143+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.kotlin.arrow
2+
3+
import arrow.core.Either
4+
import com.baeldung.kotlin.arrow.FunctionalErrorHandlingWithEither.ComputeProblem.NotANumber
5+
import com.baeldung.kotlin.arrow.FunctionalErrorHandlingWithEither.ComputeProblem.OddNumber
6+
import org.junit.Assert
7+
import org.junit.Test
8+
9+
class FunctionalErrorHandlingWithEitherTest {
10+
11+
val operator = FunctionalErrorHandlingWithEither()
12+
13+
@Test
14+
fun givenInvalidInput_whenComputeInvoked_NotANumberIsPresent(){
15+
val computeWithEither = operator.computeWithEither("bar")
16+
17+
Assert.assertTrue(computeWithEither.isLeft())
18+
when(computeWithEither){
19+
is Either.Left -> when(computeWithEither.a){
20+
NotANumber -> "Ok."
21+
else -> Assert.fail()
22+
}
23+
else -> Assert.fail()
24+
}
25+
}
26+
27+
@Test
28+
fun givenOddNumberInput_whenComputeInvoked_OddNumberIsPresent(){
29+
val computeWithEither = operator.computeWithEither("121")
30+
31+
Assert.assertTrue(computeWithEither.isLeft())
32+
when(computeWithEither){
33+
is Either.Left -> when(computeWithEither.a){
34+
OddNumber -> "Ok."
35+
else -> Assert.fail()
36+
}
37+
else -> Assert.fail()
38+
}
39+
}
40+
41+
@Test
42+
fun givenEvenNumberWithoutSquare_whenComputeInvoked_OddNumberIsPresent(){
43+
val computeWithEither = operator.computeWithEither("100")
44+
45+
Assert.assertTrue(computeWithEither.isRight())
46+
when(computeWithEither){
47+
is Either.Right -> when(computeWithEither.b){
48+
false -> "Ok."
49+
else -> Assert.fail()
50+
}
51+
else -> Assert.fail()
52+
}
53+
}
54+
55+
@Test
56+
fun givenEvenNumberWithSquare_whenComputeInvoked_OddNumberIsPresent(){
57+
val computeWithEither = operator.computeWithEither("98")
58+
59+
Assert.assertTrue(computeWithEither.isRight())
60+
when(computeWithEither){
61+
is Either.Right -> when(computeWithEither.b){
62+
true -> "Ok."
63+
else -> Assert.fail()
64+
}
65+
else -> Assert.fail()
66+
}
67+
}
68+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.kotlin.arrow
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class FunctionalErrorHandlingWithOptionTest {
7+
8+
val operator = FunctionalErrorHandlingWithOption()
9+
10+
@Test
11+
fun givenInvalidInput_thenErrorMessageIsPresent(){
12+
val useComputeOption = operator.computeWithOptionClient("foo")
13+
Assert.assertEquals("Not an even number!", useComputeOption)
14+
}
15+
16+
@Test
17+
fun givenOddNumberInput_thenErrorMessageIsPresent(){
18+
val useComputeOption = operator.computeWithOptionClient("539")
19+
Assert.assertEquals("Not an even number!",useComputeOption)
20+
}
21+
22+
@Test
23+
fun givenEvenNumberInputWithNonSquareNum_thenFalseMessageIsPresent(){
24+
val useComputeOption = operator.computeWithOptionClient("100")
25+
Assert.assertEquals("The greatest divisor is square number: false",useComputeOption)
26+
}
27+
28+
@Test
29+
fun givenEvenNumberInputWithSquareNum_thenTrueMessageIsPresent(){
30+
val useComputeOption = operator.computeWithOptionClient("242")
31+
Assert.assertEquals("The greatest divisor is square number: true",useComputeOption)
32+
}
33+
34+
}

0 commit comments

Comments
 (0)