Skip to content

Instantly share code, notes, and snippets.

@eiel
Last active December 31, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiel/7956834 to your computer and use it in GitHub Desktop.
Save eiel/7956834 to your computer and use it in GitHub Desktop.
合同勉強会大都会のLTで使用したコード http://gbdaitokai.doorkeeper.jp/events/5725 捕捉はこちら http://blog.eiel.info/blog/2013/12/15/gbdaitokai-2013/ スライドはこちら。 https://speakerdeck.com/eiel/liu-reruyounipuroguramingusitai
-- 利用例
-- $ seq 10 | runghc flow.hs
-- 15
-- 20
-- 25
-- 30
-- 35
--
-- 入力を待たないけどコードを逆から読まないといけない
main = do
getContents >>= (
return .
take 5 . -- 最初の5個
filter (>10) . -- 10より大きいものだけに
map (*5) . -- 5倍する
map read . -- 数値に
lines
) >>= mapM_ print
# 利用例
# $ seq 10 | ruby flow.rb
# 15
# 20
# 25
# 30
# 35
#
# 標準入力の読み込みが終わらないと動いてくれない
puts ARGF.each_line
.map(&:to_i) # 数値に
.map { |n| n * 5} # 5倍する
.select { |n| n > 10 } # 10より大きいものだけに
.first(5) # 最初の5つ
-- 利用例
-- $ seq 10 | runghc flow_arrow.hs
-- 15
-- 20
-- 25
-- 30
-- 35
--
-- Haskell には Control.Arrow があるじゃない
import Control.Arrow
main = getContents >>= (
lines
>>> map read -- 数値に変換
>>> map (*5) -- 5倍する
>>> filter (>10) -- 10より大きいものだけに
>>> take 5 -- 最初の5個
>>> return
) >>= mapM_ print
# 利用例
# $ seq 10 | runghc flow_lazy.hs
# 15
# 20
# 25
# 30
# 35
#
# ruby 2.0 以降なら Enumerator::Lazy があるじゃない
ARGF.each_line
.lazy
.map(&:to_i) # 数値に
.map { |n| n * 5 } # 5倍する
.select { |n| n > 10 } # 10より大きいものだけに
.map { |n| puts n } # 出力
.first(5) # 最初の5個
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment