Commit 8010df3a authored by Jan Hauer's avatar Jan Hauer 🌄
Browse files

With Mul and custom flatMap

parent 8339a874
Pipeline #53792 failed with stages
......@@ -3,4 +3,5 @@ package io.hauer.jax19.function
sealed class Expr
class Const(val value: Int) : Expr()
class Mul(val left: Expr, val right: Expr) : Expr()
class Div(val left: Expr, val right: Expr) : Expr()
\ No newline at end of file
......@@ -10,8 +10,8 @@ fun main() {
fun eval(expr: Expr): Optional<Int> = when (expr) {
is Const -> Optional.of(expr.value)
is Div -> eval(expr.left).flatMap{ left -> eval(expr.right).flatMap { right -> left div right }}
is Mul -> (eval(expr.left) to eval(expr.right)).map{ left, right -> left * right}
is Div -> (eval(expr.left) to eval(expr.right)).flatMap { left, right -> left div right }
}
infix fun Int.div (other : Int) = if(other != 0) Optional.of(this / other) else Optional.empty()
\ No newline at end of file
package io.hauer.jax19.function
import java.util.*
fun Any.printResult () = println( "Ergebnis ist $this")
\ No newline at end of file
fun Any.printResult () = println( "Ergebnis ist $this")
fun <A, B, C> Pair<Optional<A>, Optional<B>>.map(f: (A, B) -> C) = this.first.flatMap { first -> second.map { second -> f(first, second) } }
fun <A, B, C> Pair<Optional<A>, Optional<B>>.flatMap(f: (A, B) -> Optional<C>) = this.first.flatMap { first -> second.flatMap { second -> f(first, second) } }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment