Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More haskell-like applicative syntax in scalaz

Tags:

scala

scalaz

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?

like image 565
Konstantin Solomatov Avatar asked Jul 16 '12 09:07

Konstantin Solomatov


2 Answers

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
like image 115
Didier Dupont Avatar answered Nov 13 '22 01:11

Didier Dupont


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.

like image 27
Travis Brown Avatar answered Nov 13 '22 00:11

Travis Brown