paiza.ioでelixir その184

# 概要
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.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, :discard) 
|> Enum.to_list()
|> IO.inspect

Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 2, [7]) 
|> Enum.to_list()
|> IO.inspect

Stream.chunk_every([1, 2, 3, 4, 5, 6], 3, 3, []) 
|> 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

stream1 = Stream.cycle([1, 2, 3])
stream2 = Stream.cycle([4, 5, 6])
stream = Stream.concat(stream1, stream2)
Enum.take(stream, 6)
|> 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(1..10, -5)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.drop_every(1..10, 2)
|> IO.inspect
Enum.to_list(stream)
|> IO.inspect

stream = Stream.drop_every([1, 2, 3, 4, 5], 0)
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)
#receive do: (x when is_integer(x) -> x)
|> IO.inspect
#receive do: (x when is_integer(x) -> x)
|> IO.inspect
#receive do: (x when is_integer(x) -> x)
|> 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 = 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.intersperse([1], 0) 
|> Enum.to_list()
|> IO.inspect

Stream.intersperse([], 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.map_every([1, 2, 3, 4, 5], 1, fn x -> 
    x * 2 
end)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.map_every(1..5, 0, 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

:rand.seed(:exsss, {1, 2, 3})
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)
|> IO.inspect

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()
#|> IO.inspect

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(1..100, -5)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.cycle([1, 2, 3]) 
|> Stream.take(5)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.take_every(1..10, 2)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.take_every([1, 2, 3, 4, 5], 1)
Enum.to_list(stream)
|> IO.inspect

stream = Stream.take_every(1..1000, 0)
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.uniq_by([a: {:tea, 2}, b: {:tea, 2}, c: {:coffee, 1}], fn {_, y} -> 
    y 
end) 
|> Enum.to_list
|> IO.inspect

stream = Stream.with_index([1, 2, 3], 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, 1..3], fn [a, b, c] -> 
    a + b + c 
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




```

# 実行結果

```
[[1], [2, 2], [3], [4, 4, 6], '\a\a']
[[1, 2], [3, 4], [5, 6]]
[[1, 2, 3], [3, 4, 5]]
[[1, 2, 3], [3, 4, 5], [5, 6, 7]]
[[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, 3]
[1, 2, 3, 1, 2]
[1, 2, 3, 2, 1]
[{1, :x}, {2, :y}, {1, :x}]
[6, 7, 8, 9, 10]
[1, 2, 3, 4, 5]
#Stream<[enum: 1..10, funs: [#Function<37.50989570/1 in Stream.drop_every/2>]]>
[2, 4, 6, 8, 10]
[1, 2, 3, 4, 5]
[6, 7, 8, 9, 10]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]
[2]
[1, 2, 2, 4, 3, 6]
[[1], [2], [3]]
[1, 0, 2, 0, 3]
[1]
[]
[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]
[2, 4, 6, 8, 10]
[1, 2, 3, 4, 5]
[1, 3]
[0.5455598952593053, 0.6039309974353404, 0.6684893034823949]
#Function<52.50989570/2 in Stream.resource/3>
["s", "t", "r", "i", "n", "g"]
[1, 3, 6, 10, 15]
[1, 3, 6, 10, 15]
[1, 2, 3, 4, 5]
'`abcd'
[1, 2, 3, 1, 2]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5]
[]
[1, 2, 3, 4, 5]
[0]
[1001, 1002, 1003]
[5, 4, 3, 2, 1]
[1, 2, 3]
[{1, :x}, {2, :y}]
[a: {:tea, 2}, c: {:coffee, 1}]
[{1, 3}, {2, 4}, {3, 5}]
[{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]
[3, 6, 9]
[2, 4, 6, 8, 10, 12]


```

# 成果物

https://paiza.io/projects/pGR3-NITbNWFmWv9ZXVhKQ

以上。