Scala有一个类似于Haskell的`$`的运算符吗?

时间:2010-09-15 17:32:50

标签: function scala operators

Scala是否有类似于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

1 个答案:

答案 0 :(得分:18)

是的,写的是“申请”

fn apply arg

没有标准的标点符号操作符,但通过库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)

一般来说,虽然Scala代码比Java更密集,但它并不像Haskell那么密集。因此,创建“$”和“。”等运算符的收益较少。