作者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