Last active
June 21, 2018 18:43
-
-
Save RadoBuransky/040e751e498b8a46dbe748c2e8d0ce0e to your computer and use it in GitHub Desktop.
Scala flatMap DSL syntax challenge
This file contains hidden or 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
// The goal is to find "the best" syntax for line #21 | |
trait M[A] { | |
def map[B](f: A => B): M[B] | |
def flatMap[B](f: A => M[B]): M[B] | |
} | |
class UseParams2[P1, P2](p1: M[P1], p2: M[P2]) { | |
def toCall[R](f: Function2[P1, P2, R]): M[R] = | |
for { | |
v1 <- p1 | |
v2 <- p2 | |
r = f(v1, v2) | |
} yield r | |
} | |
object UltimateSyntax { | |
def entry(a: M[Int], b: M[String]): M[Boolean] = { | |
// How can I make this look better? | |
// Can I somehow write "logic(a, b)"? | |
useParams(a, b).toCall(logic) | |
} | |
private def logic(a: Int, b: String): Boolean = b.toString.length < b.length | |
private def useParams[P1, P2](p1: M[P1], p2: M[P2]) = new UseParams2[P1, P2](p1, p2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment