0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paiza.ioでelixir その336

Posted at

概要

paiza.ioでelixirやってみた。
Streamに、なじめないので、訓練してみた。

サンプルコード

#Stream.unfold(カウンタ初期値, fn カウンタ変数 -> {リストに出力される各要素, カウンタの次の値} end)

Stream.unfold(5, fn
    0 -> 
        nil
    n -> 
        {n, n - 1}
end)
|> Enum.take(10)
|> IO.inspect

Stream.unfold(0, fn
    x -> 
        {x + 1, x + 1}
end)
|> Enum.take(10)
|> IO.inspect

Stream.unfold(0, fn
    x -> 
        {x, x + 2}
end)
|> Enum.take(10)
|> IO.inspect



 f = fn list, default_value ->
        Stream.unfold(list, fn
            [] -> 
                {default_value, []}
            [h | t] -> 
                {h, t}
        end)
    end
s = f.([1, 2], 0)
Enum.take(s, 10)
|> IO.inspect

s = f.([9, 8, 7, 6, 5, 4, 3, 2, 1], 0)
Enum.take(s, 10)
|> IO.inspect

Stream.unfold(5, fn
    0 -> 
        nil
    n -> 
        {n, n - 1}
end) 
|> Enum.to_list()
|> IO.inspect



Enum.map(1..10_000, &(&1 + 1)) 
|> Enum.take(5)
|> IO.inspect

Stream.map(1..10_000, &(&1 + 1)) 
|> Enum.take(5)
|> IO.inspect


Stream.map([1, 2, 3], fn x -> 
    x * 2 
end)
|> Enum.to_list()
|> IO.inspect

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

Stream.iterate(0, &(&1 + 1)) 
|> Enum.take(10)
|> IO.inspect


fib = Stream.iterate({1, 1}, &({ elem(&1, 1), elem(&1, 0) + elem(&1, 1)})) 
    |> Stream.map(&(elem(&1, 0)))
fib 
|> Enum.take(10)
|> IO.inspect

Stream.filter([1, 2, 3], fn x -> 
    rem(x, 2) == 0 
end)
|> 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

defmodule FizzBuzz do
  def print(n) do
    generate2
    |> Stream.take(n)
    |> Stream.each(&IO.puts/1)
    |> Stream.run
  end
  def generate do
    fizzes = Stream.cycle(["Fizz", "", ""])
    buzzes = Stream.cycle(["Buzz", "", "", "", ""])
    Stream.zip(fizzes, buzzes)
    |> Stream.map(&concat/1)
    |> Stream.with_index
    |> Stream.map(&translate/1)
    |> Stream.drop(1)
  end
  def generate2 do
    fizzes  = Stream.cycle(["", "", "Fizz"])
    buzzes  = Stream.cycle(["", "", "", "", "Buzz"])
    indexes = Stream.iterate(1, &(&1 + 1))
    Stream.zip(fizzes, buzzes)
    |> Stream.map(&concat/1)
    |> Stream.zip(indexes)
    |> Stream.map(&translate/1)
  end
  defp concat({f, b}), do: f <> b
  defp translate({"",   n}), do: to_string(n)
  defp translate({str, _n}), do: str
end


FizzBuzz.print(15)

実行結果

[5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
[5, 4, 3, 2, 1]
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]
[2, 4, 6]
[1, 2, 3, 1, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
[2]
[{1, :a}, {2, :b}, {3, :c}, {4, :a}, {5, :b}, {6, :c}]
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

成果物

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?