LoginSignup
5
5

More than 5 years have passed since last update.

Elixirで遅延リスト

Last updated at Posted at 2013-12-06

Streamモジュールで遅延リストが使えます。

iex> Stream.iterate(0, &(&1+1)) |> Enum.take(20)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

特定のリストを繰り返す場合はStream.cycleです。

iex> Stream.cycle([1, 2, 3]) |> Enum.take(10)
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]

みんな大好きフィボナッチ数列を作ってみます。

iex> fib = Stream.iterate({1, 1}, &({ elem(&1, 1), elem(&1, 0) + elem(&1, 1)})) |> Stream.map(&(elem(&1, 0)))
iex> fib |> Enum.take(10)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

その他の関数はドキュメントで
http://elixir-lang.org/docs/stable/Stream.html

明日は @bash0C7 さんです

5
5
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
5
5