概要
paiza.ioでelixirやってみた。
thenとtapに、なじめないので、訓練してみた。
サンプルコード
#then/2は第1引数の値を第2引数の関数に渡し、その結果を返します。
#tap/2は第1引数の値を第2引数の関数に渡して実行しますが、第1引数に渡した値自体をそのまま返します。
then(5, &IO.puts(&1))
|> IO.inspect
IO.puts("ok0")
tap(5, &IO.puts(&1))
|> IO.inspect
IO.puts("ok1")
tap(1, fn x ->
x + 1
end)
|> IO.inspect
IO.puts("ok2")
then(2, fn x ->
x + 2
end)
|> IO.inspect
IO.puts("ok3")
then(5, fn x ->
x + 1
end)
|> IO.inspect()
tap(3, fn x ->
x + 1
end)
|> IO.inspect()
"hello world"
|> tap(&IO.puts/1)
|> then(&Regex.scan(~r/\w+/, &1))
"hello world"
|> (fn x ->
IO.puts(x)
x
end).()
|> (&Regex.scan(~r/\w+/, &1)).()
実行結果
5
:ok
ok0
5
5
ok1
1
ok2
4
ok3
6
3
hello world
hello world
成果物
以上。