函数 – Scala是否具有与Haskell的“$”类似的运算符?
发布时间:2020-12-16 09:13:01 所属栏目:安全 来源:网络整理
导读: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 sometime
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 解决方法
是的,它写成“适用”
fn apply arg 没有标准的标点符号操作符,但是通过库创建添加一个可以很容易. 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那么密集.因此,创建像“$”和“”这样的操作符的回报较少. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |