paiza.ioでelixir その81

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

# サンプルコード

```

stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))
|> IO.inspect
Enum.to_list(stream)
|> IO.inspect
Stream.chunk_every([1, 2, 3, 4, 5, 6], 2) 
|> Enum.to_list()
|> IO.inspect
chunk_fun = fn element, acc ->
	if rem(element, 2) == 0 do
		{:cont, Enum.reverse([element | acc]), []}
	else
		{:cont, [element | acc]}
	end
end
after_fun = fn
	[] -> {:cont, []}
	acc -> {:cont, Enum.reverse(acc), []}
end
stream = Stream.chunk_while(1..10, [], chunk_fun, after_fun)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.concat([1..3, 4..6, 7..9])
Enum.to_list(stream)
|> IO.inspect
stream = Stream.concat(1..3, 4..6)
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.dedup_by([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) 
|> Enum.to_list()
|> IO.inspect
stream = Stream.drop(1..10, 5)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.drop_every(1..10, 2)
Enum.to_list(stream)
|> IO.inspect
stream = Stream.drop_while(1..10, &(&1 <= 5))
Enum.to_list(stream)
|> IO.inspect
stream = Stream.each([1, 2, 3], fn x -> 
    send(self(), x) 
end)
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, x * 2] 
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.resource(fn -> 
    File.open!("sample") 
end, fn file ->
	case IO.read(file, :line) do
		data when is_binary(data) -> 
		    {[data], file}
		_ -> 
		    {:halt, file}
	end
end, fn file -> 
	File.close(file) 
end)
Stream.resource(fn ->
	{:ok, pid} = StringIO.open("string")
	pid
end, fn pid ->
	case IO.getn(pid, "", 1) do
		 :eof -> 
		    {:halt, pid}
		 char -> 
		    {[char], pid}
	end
end, fn pid -> 
    StringIO.close(pid) 
end) 
|> Enum.to_list()
|> IO.inspect
#File.stream!("/path/to/file")
#|> Stream.map(&String.replace(&1, "#", "%"))
#|> Stream.into(File.stream!("/path/to/other/file"))
#|> Stream.run()
stream = Stream.scan(1..5, &(&1 + &2))
Enum.to_list(stream)

|> IO.inspect
stream = Stream.scan(1..5, 0, &(&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)
cycle = Stream.cycle([:a, :b, :c])
Stream.zip(concat, 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
concat = Stream.concat(1..3, 4..6)
Stream.zip_with(concat, concat, fn a, b -> 
    a + b 
end) 
|> Enum.to_list()
|> IO.inspect

```

# 実行結果

```
#Stream<[
  enum: [1, 2, 2, 3, 4, 4, 6, 7, 7],
  funs: [#Function<4.50989570/1 in Stream.chunk_while/4>]
]>
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
[[1, 2], [3, 4], [5, 6]]
[[1, 2], [3, 4], [5, 6], '\a\b', '\t\n']
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2]
[1, 2, 3, 2, 1]
[{1, :x}, {2, :y}, {1, :x}]
[6, 7, 8, 9, 10]
[2, 4, 6, 8, 10]
[6, 7, 8, 9, 10]
[1, 2, 3]
[2]
[1, 2, 2, 4, 3, 6]
[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.33041812700944506, 0.4865585121900461, 0.6705637198484149]
["s", "t", "r", "i", "n", "g"]
[1, 3, 6, 10, 15]
[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"}]
[{1, :a}, {2, :b}, {3, :c}, {4, :a}, {5, :b}, {6, :c}]
[2, 4, 6, 8, 10, 12]
[2, 4, 6, 8, 10, 12]
×


```

# 成果物

https://paiza.io/projects/B86qIVOSw_1yrjWwluy4rA?language=elixir



以上。