Last active
February 21, 2018 07:21
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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