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
答案 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那么密集。因此,创建“$”和“。”等运算符的收益较少。