Re: [比較] 我為何鍾情於用 Scala 做為兵刃(三)
※ 引述《huei1224 (渣)》之銘言:
: : ====================== 我是 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
通常用 Haskell 的人會這樣寫
result = length $ filter (>5) $ map (^2) $ xs
不過如果想用 flip ($) 的話不如試試
import Control.Monad.List
result = length $ xs >>= return . (^2) >>= guard . (>5)
--
And in that line now was a whiskered old man,
with a linen cap and a crooked nose,
who waited in a place called the Stardust Band Shell
to share his part of the secret of heaven:
that each affects the other and the other affects the next,
and the world is full of stories, but the stories are all one.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 128.36.232.45
※ 編輯: scwg 來自: 128.36.232.45 (07/12 11:29)
→
07/12 11:29, , 1F
07/12 11:29, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 8 之 11 篇):
PLT 近期熱門文章
PTT數位生活區 即時熱門文章