2006-12-30
FP中减少括号的语法糖
关键字: f# ocaml haskell ruby
在F#中内置了两个很有用的运算符 |> 和 >> ,用来串联数据和函数,有效减少括号的使用,让代码不那么lispy。这两个符号的定义很简单,在Ocaml中也一样可以方便使用。
运算符 |> 定义:
例子:形如
依此类推(抱歉,例子抽象了点)
运算符 >> 定义:
例子:形如
注意这个类似Haskell里所谓point free style的.运算符,但是运算顺序正好相反。
运算符.的Haskell定义:
这点从它们的类型签名也能看出来。所以,Haskell的定义符合复合函数的数学定义,但是读起来得从右向左读。而F#的定义符合从左向右读的直觉习惯,各有千秋。
这种函数串联然后再整体作用到数据上的编程风格好像也叫tacit programming。。。
Ruby中在Proc类中定义运算符也能达到类似的串联效果,当然Ruby只能重定义那么几个已经存在的运算符,不能凭空造新运算符:(
运算符 |> 定义:
let (|>) x f = f x
val (|>) : 'a -> ('a -> 'b) -> 'b
例子:形如
f(g(h(i(x))))可以写成
x |> i |> h |> g |> f
依此类推(抱歉,例子抽象了点)
运算符 >> 定义:
let (>>) f g x = g(f(x))
val (>>) : ('a -> 'b) -> ('b -> 'c) -> ('a -> 'c)
例子:形如
f(g(h(i(x))))可以写成
i >> h >> g >> f >> x
注意这个类似Haskell里所谓point free style的.运算符,但是运算顺序正好相反。
运算符.的Haskell定义:
let (.) f g x = f(g(x)) (.) :: (b -> c) -> (a -> b) -> a -> c
这点从它们的类型签名也能看出来。所以,Haskell的定义符合复合函数的数学定义,但是读起来得从右向左读。而F#的定义符合从左向右读的直觉习惯,各有千秋。
这种函数串联然后再整体作用到数据上的编程风格好像也叫tacit programming。。。
Ruby中在Proc类中定义运算符也能达到类似的串联效果,当然Ruby只能重定义那么几个已经存在的运算符,不能凭空造新运算符:(
评论
Thanks for the useful comment. Yes, |> is a reversed version of $ in Haskell, though |> gives a better feeling regarding the direction of pipelines. I'm pretty some neat syntax sugar of F# is borrowed from Haskell and we will see more in future (such as zip family). However, there's another voice saying Haskell Prelude is too fat and should be narrowed down a bit, hehe.
I personally feel Haskell has defined too many special symbols which can make code line-noisy easily, like Perl style syntax.
I personally feel Haskell has defined too many special symbols which can make code line-noisy easily, like Perl style syntax.
FYI.
(|>) is "reverse" of ($).
The (|>) also shows up as euro ( to compare with $ ) on Haskell wikibook.
Haskell/Understanding monads
1.2 euro ( http://en.wikibooks.org/wiki/Haskell/Understanding_monads#euro )
...
N.B.: the euro symbol isn't valid Haskell... if you want to try this, use (|>) as an operator instead
...
(>>) uses by monad, in the book Haskell - The Craft of Functional Programming
they defines a (>.>) as "reverse" of (.)
Page 170 (Forward Composition) shows a "reverse" of function composition definition:
infixl 9 >.>
(>.>) :: (a -> b) -> (b -> c) -> (a -> c)
g >.> f = f . g
(|>) is "reverse" of ($).
The (|>) also shows up as euro ( to compare with $ ) on Haskell wikibook.
Haskell/Understanding monads
1.2 euro ( http://en.wikibooks.org/wiki/Haskell/Understanding_monads#euro )
...
N.B.: the euro symbol isn't valid Haskell... if you want to try this, use (|>) as an operator instead
...
(>>) uses by monad, in the book Haskell - The Craft of Functional Programming
they defines a (>.>) as "reverse" of (.)
Page 170 (Forward Composition) shows a "reverse" of function composition definition:
infixl 9 >.>
(>.>) :: (a -> b) -> (b -> c) -> (a -> c)
g >.> f = f . g
发表评论
- 浏览: 332515 次
- 性别:

- 来自: Montreal

- 详细资料
搜索本博客
我的相册
20059805856241
共 10 张
共 10 张
最新评论
-
Darcs简介
good 3x
-- by 夜鸣猪 -
Pratical Ocaml作者采访
现在主要用F#分析数据,因为比较舒服(人懒啊)。其实也只用到很少的FP特性,Ru ...
-- by cookoo -
Pratical Ocaml作者采访
一年多了,呵呵,cookoo能说说看,学习使用OCaml的进展和体会吗?
-- by billgui -
Memory - 柿岛伸次
还不错啊。
-- by hazzy -
Memory - 柿岛伸次
我很想下这个,可就是不能下。LZ能否提供链接
-- by yeshucheng






评论排行榜