paiza.ioでelixir その31

# 概要
paiza.ioでelixirやってみた。
Stream使ってみた。

# サンプルコード

```
Stream.repeatedly(fn ->
	0
end)
|> Enum.take(5)
|> IO.inspect
Stream.repeatedly(fn ->
	Enum.random(0..9)
end)
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, fn(n) ->
	n + 1
end)
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, & (&1 + 1))
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, fn(n) ->
	n + 2
end)
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, &(&1 + 1))
|> Stream.reject(&(rem(&1, 2) != 0))
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, &(&1 + 1))
|> Stream.filter(&(rem(&1, 2) == 0))
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, &(&1 + 1))
|> Stream.map(&(&1 * 2))
|> Enum.take(5)
|> IO.inspect
require Integer

Stream.iterate(0, &(&1 + 1))
|> Stream.filter(&(! Integer.is_odd(&1)))
|> Enum.take(5)
|> IO.inspect
Stream.iterate(0, &(&1 + 1))
|> Stream.filter(&(Integer.is_even(&1)))
|> Enum.take(5)
|> IO.inspect
Stream.unfold(0, fn n ->
	{n, 0}
end)
|> Enum.take(5)
|> IO.inspect
Stream.unfold(0, fn n ->
	{n, n + 1}
end)
|> Enum.take(5)
|> IO.inspect
Stream.unfold(0, fn n ->
	{n, n + 2}
end)
|> Enum.take(5)
|> IO.inspect
Stream.unfold({1, 1}, fn {x, y} ->
	{x, {y, x + y}}
end)
|> Enum.take(5)
|> IO.inspect
Stream.unfold({1, 1}, fn {x, y} ->
	{x, {y, x + y}}
end)
|> Enum.take(10)
|> IO.inspect
Stream.unfold({1, 1, 1}, fn {x, y, z} ->
	{x, {y, z, x + y + z}}
end)
|> Enum.take(10)
|> IO.inspect
Stream.unfold({0, 0}, fn {x, y} ->
	{x, {y, 0}}
end)
|> Enum.take(5)
|> IO.inspect
Stream.unfold({0, 0}, fn {_x, _y} ->
	{0, {0, 0}}
end)
|> Enum.take(5)
|> IO.inspect
Stream.cycle([1, 2, 3, 4, 5])
|> Enum.take(10)
|> IO.inspect
Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
|> Enum.to_list
|> IO.inspect
Stream.chunk_every([1, 2, 3, 4, 5, 6], 2) 
|> Enum.to_list()
|> IO.inspect
Stream.concat([1..3, 4..6, 7..9])
|> Enum.to_list
|> IO.inspect
Stream.dedup([1, 2, 3, 3, 2, 1]) 
|> Enum.to_list
|> IO.inspect
Stream.drop(1..10, 5)
|> Enum.to_list
|> IO.inspect
Stream.drop_every(1..10, 2)
|> Enum.to_list
|> IO.inspect
Stream.filter([1, 2, 3], fn x -> 
    rem(x, 2) == 0 
end)
|> Enum.to_list
|> IO.inspect
Stream.flat_map([1, 2, 3], fn x -> 
    [x, x * 2] 
end)
|> Enum.to_list
|> IO.inspect
Stream.intersperse([1, 2, 3], 0) 
|> Enum.to_list
|> IO.inspect
Stream.interval(10) 
|> Enum.take(10)
|> IO.inspect
Stream.map([1, 2, 3], fn x -> 
    x * 2 
end)
|> Enum.to_list
|> IO.inspect
Stream.map_every(1..10, 2, fn x -> 
    x * 2 
end)
|> Enum.to_list
|> IO.inspect
Stream.reject([1, 2, 3], fn x -> 
    rem(x, 2) == 0 
end)
|> Enum.to_list
|> IO.inspect
Stream.scan(1..5, &(&1 + &2))
|> Enum.to_list
|> IO.inspect
Stream.take_every(1..10, 2)
|> Enum.to_list
|> IO.inspect
Stream.take_while(1..100, &(&1 <= 5))
|> Enum.to_list
|> IO.inspect
Stream.timer(10) 
|> Enum.to_list()
|> IO.inspect
enum = 1001..9999
n = 3
Stream.transform(enum, 0, fn i, acc ->
  if acc < n, do: {[i], acc + 1}, else: {:halt, acc}
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.with_index([1, 2, 3])
|> Enum.to_list
|> 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



```

# 実行結果

```
[0, 0, 0, 0, 0]
[4, 4, 1, 8, 9]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 2, 4, 6, 8]
[0, 0, 0, 0, 0]
[0, 1, 2, 3, 4]
[0, 2, 4, 6, 8]
[1, 1, 2, 3, 5]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[1, 1, 1, 3, 5, 9, 17, 31, 57, 105]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
[[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, 2, 1]
[6, 7, 8, 9, 10]
[2, 4, 6, 8, 10]
[2]
[1, 2, 2, 4, 3, 6]
[1, 0, 2, 0, 3]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6]
[2, 2, 6, 4, 10, 6, 14, 8, 18, 10]
[1, 3]
[1, 3, 6, 10, 15]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5]
[0]
[1001, 1002, 1003]
[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]
```

# 成果物

https://paiza.io/projects/BlsYVHPbeafTVWfS3ixr4w


以上。