Skip to content

Instantly share code, notes, and snippets.

@novikserg
Last active February 21, 2018 07:21
Show Gist options
  • Save novikserg/d10d93ae74ce9988128c09406554e279 to your computer and use it in GitHub Desktop.
Save novikserg/d10d93ae74ce9988128c09406554e279 to your computer and use it in GitHub Desktop.
A quick example of Fibbonaci sequence using Elixir's Stream (unlike eager Enums, Streams are lazy, generated 1 by 1 during execution, and highly composable)
def fibbonaci_iterate do
Stream.unfold([0, 1], fn [prev, curr] -> {curr, [curr, (prev + curr)]} end)
end
# fibbonaci_iterate |> Stream.take_every(10) |> Stream.map(fn(x) -> x * 2 end) |> Enum.take(5)
# [2, 178, 21892, 2692538, 331160282]
# note that the list was actually only enumerated once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment