概要
paiza.ioでelixirやってみた。
Stream使ってみた。
サンプルコード
stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
Enum.to_list(stream)
|> IO.inspect
Stream.chunk_every([1, 2, 3, 4, 5, 6], 2)
|> Enum.to_list()
|> IO.inspect
stream = Stream.concat([1..3, 4..6, 7..9])
Enum.to_list(stream)
|> IO.inspect
stream = Stream.cycle([1, 2, 3])
Enum.take(stream, 5)
|> IO.inspect
Stream.dedup([1, 2, 3, 3, 2, 1])
|> Enum.to_list()
|> IO.inspect
stream = Stream.drop(1..10, 5)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.filter([1, 2, 3], fn x ->
rem(x, 2) == 0
end)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.flat_map([1, 2, 3], fn x ->
[[x]]
end)
Enum.to_list(stream)
|> IO.inspect
Stream.intersperse([1, 2, 3], 0)
|> Enum.to_list()
|> IO.inspect
Stream.interval(10)
|> Enum.take(10)
|> IO.inspect
Stream.iterate(0, &(&1 + 1))
|> Enum.take(5)
|> IO.inspect
stream = Stream.map([1, 2, 3], fn x ->
x * 2
end)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.map_every(1..10, 2, fn x ->
x * 2
end)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.reject([1, 2, 3], fn x ->
rem(x, 2) == 0
end)
Enum.to_list(stream)
|> IO.inspect
Stream.repeatedly(&:rand.uniform/0)
|> Enum.take(3)
|> IO.inspect
stream = Stream.scan(1..5, &(&1 + &2))
Enum.to_list(stream)
|> IO.inspect
stream = Stream.take(1..100, 5)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.take_every(1..10, 2)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.take_while(1..100, &(&1 <= 5))
Enum.to_list(stream)
|> IO.inspect
Stream.timer(10)
|> Enum.to_list()
|> IO.inspect
enum = 1001..9999
n = 3
stream = Stream.transform(enum, 0, fn i, acc ->
if acc < n, do: {[i], acc + 1}, else: {:halt, acc}
end)
Enum.to_list(stream)
|> IO.inspect
Stream.unfold(5, fn
0 -> nil
n -> {n, n - 1}
end) |> Enum.to_list()
|> IO.inspect
Stream.uniq([1, 2, 3, 3, 2, 1])
|> Enum.to_list()
|> IO.inspect
Stream.uniq_by([{1, :x}, {2, :y}, {1, :z}], fn {x, _} ->
x
end)
|> Enum.to_list()
|> IO.inspect
stream = Stream.with_index([1, 2, 3])
Enum.to_list(stream)
|> IO.inspect
concat = Stream.concat(1..3, 4..6)
cycle = Stream.cycle(["foo", "bar", "baz"])
Stream.zip([concat, [:a, :b, :c], cycle])
|> Enum.to_list()
|> IO.inspect
concat = Stream.concat(1..3, 4..6)
Stream.zip_with([concat, concat], fn [a, b] ->
a + b
end)
|> Enum.to_list()
|> IO.inspect
実行結果
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
[[1, 2], [3, 4], [5, 6]]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 1, 2]
[1, 2, 3, 2, 1]
[6, 7, 8, 9, 10]
[2]
[[1], [2], [3]]
[1, 0, 2, 0, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4]
[2, 4, 6]
[2, 2, 6, 4, 10, 6, 14, 8, 18, 10]
[1, 3]
[0.29093681512387826, 0.23805665492554406, 0.3631628956357238]
[1, 3, 6, 10, 15]
[1, 2, 3, 4, 5]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5]
[0]
[1001, 1002, 1003]
[5, 4, 3, 2, 1]
[1, 2, 3]
[{1, :x}, {2, :y}]
[{1, 0}, {2, 1}, {3, 2}]
[{1, :a, "foo"}, {2, :b, "bar"}, {3, :c, "baz"}]
[2, 4, 6, 8, 10, 12]
成果物
以上。