views:

256

answers:

1
+4  Q: 

Scala $ operator

Has Scala a Haskell-like $ operator?

+9  A: 

Yes, it's written "apply"

fn apply arg

There's no standard punctuation operator for this, but it would be easy enough to add one via library pimping.

  class RichFunction[-A,+B](fn: Function1[A, B]){ def $(a:A):B = fn(a)}
  implicit def function2RichFunction[-A,+B](t: Function1[A, B]) = new RichFunction[A, B](t)

In general, while Scala code is much denser than Java, it's not quite as dense as Haskell. Thus, there's less payoff to creating operators like '$' and '.'

Dave Griffith
One of the bigger wins of `$` is its use in operator sections: `(f $)` and `($ x)`, which don't exist in Scala (you'd write `f(_)` and `_(x)` instead, but you can't do that in Haskell).
Alexey Romanov