-
-
Save pulkit110/1c477da9e47dbc5fdb6df582b7999e59 to your computer and use it in GitHub Desktop.
Fibonacci Number Generator in Elixir
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
defmodule Fib do | |
def number(n) do | |
cache = | |
2..n | |
|> Enum.reduce(%{prev: 0, current: 1}, fn _i, acc -> | |
%{prev: acc.current, current: acc.prev + acc.current} | |
end) | |
|> Map.get(:current) | |
end | |
end | |
Fib.number(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment