Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have an operator similar to Haskell's `$`?

Does Scala have an operator similar to Haskell's $?

-- | Application operator.  This operator is redundant, since ordinary
-- application @(f x)@ means the same as @(f '$' x)@. However, '$' has
-- low, right-associative binding precedence, so it sometimes allows
-- parentheses to be omitted; for example:
--
-- >     f $ g $ h x  =  f (g (h x))
--
-- It is also useful in higher-order situations, such as @'map' ('$' 0) xs@,
-- or @'Data.List.zipWith' ('$') fs xs@.
{-# INLINE ($) #-}
($)                     :: (a -> b) -> a -> b
f $ x                   =  f x
like image 441
kolchanov Avatar asked Sep 15 '10 17:09

kolchanov


People also ask

Why Scala is better than Haskell?

Haskell is concise, safe and faster to use, whereas Scala is also concise, fast and safer with many libraries support. Haskell has first-class functions and pure, whereas Scala is strict and impure to use in terms of functional programming features.

Which is better Scala or Haskell?

Both Scala and Haskell are statically typed languages, but Haskell's type system is arguably more powerful, in the sense that it provides more guarantees that are checked by the compiler. It also has superior type inference, which means that it places a lower burden on the programmer.


1 Answers

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 '.'

like image 140
Dave Griffith Avatar answered Sep 17 '22 12:09

Dave Griffith