作者huei1224 (渣)
看板PLT
标题Re: [比较] 我为何锺情於用 Scala 做为兵刃(三)
时间Sun Jul 10 22:18:41 2011
: ====================== 我是 Scala 程式分隔线 =========================
: val xs = List(-1, -2, -3, 0, 1, 2, 3)
: val square = (n: Int) => n * n
: val isGreaterThan5 = (n: Int) => n > 5
: val result = xs.map(square).filter(isGreaterThan5).length
: // 上面那行和下面这行等价
: // val result = xs.map(n => n * n).filter(n => n > 5).length
: println (result1)
: ======================================================================
最近在学 haskell ,发现 haskell 也可以写成像上面的型式,分享一下
let x -: f = f x
let xs = [-3..3]
let square = (^2)
let isGreaterThan5 = (>5)
let result = xs -: map square -: filter isGreaterThan5 -: length
-- let result = xs -: map (^2) -: filter (>5) -: length
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 1.161.197.144