Skip to content

Instantly share code, notes, and snippets.

@mariomartinezsz
Created April 24, 2017 06:10
Show Gist options
  • Select an option

  • Save mariomartinezsz/3d6874172c95c5fdb1865f7190264710 to your computer and use it in GitHub Desktop.

Select an option

Save mariomartinezsz/3d6874172c95c5fdb1865f7190264710 to your computer and use it in GitHub Desktop.
Fibonacci module for Elixir
defmodule Fibonacci do
@moduledoc """
A Fibonacci series generator.
"""
@doc """
get_series/1 function.
Returns a Fibonacci series in a list.
## Example
iex> Fibonacci.get_series(11)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
"""
def get_series(qty) when qty > 0 do
Stream.unfold({1, 1},
fn {prev, current} -> {prev, {current, prev + current}} end)
|> Enum.take(qty)
end
def get_series(qty) when qty <= 0 do
0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment