kol*_*nov 12 scala function operators
Scala有一个类似于Haskell的$运算符吗?
-- | 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
Run Code Online (Sandbox Code Playgroud)
Dav*_*ith 18
是的,写的是"申请"
fn apply arg
Run Code Online (Sandbox Code Playgroud)
这没有标准的标点符号操作符,但通过库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)
Run Code Online (Sandbox Code Playgroud)
通常,虽然Scala代码比Java密集得多,但它并不像Haskell那么密集.因此,创建"$"和"."等运算符的收益较少.