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