I am experimenting with scalaz. I tried writing code in applicative code. I wrote code like this:
val max: Option[Int] = (a |@| b) { math.max(_, _) }
I didn't like this code very much. I would like to code which is closer to Haskell style, something like this:
val max: Option[Int] = { math.max(_, _) } <$> a <*> b
Is this possible. And why scalaz didn't implement it this way?
Scala's type inference, is much more limited than in Haskell (identifier overloading, which comes with the JVM being one of the reasons). Inferences flows from left to right, and the type of the arguments of a function may be deduced from previous context (if at the place of the definition, a function with arg type A is expected), but not from how they are used in the definition. Scalaz syntax makes the arguments types available. Reversing it would most of the time force you to write the function arguments types, e.g.
{math.max(_: Int, _: Int) } <$> a <*> b
You can translate the Haskell version directly into Scala, if you're willing to be a little more verbose:
import scalaz._, Scalaz._
val a = Option(1)
val b = Option(2)
val f: Int => Int => Int = x => math.max(x, _)
val c = b <*> (a map f)
Or, as a one-liner:
val c = 2.some <*> 1.some.map(x => math.max(x, _: Int))
Or:
val c = 2.some <*> (1.some map (math.max _).curried)
The order is reversed because these are method calls instead of infix operators, but it's essentially the same thing as max <$> a <*> b
: we map the function over the first item, and then apply the result to the second.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With