Created
April 24, 2017 06:10
-
-
Save mariomartinezsz/3d6874172c95c5fdb1865f7190264710 to your computer and use it in GitHub Desktop.
Fibonacci module for Elixir
This file contains hidden or 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 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