Skip to content

Instantly share code, notes, and snippets.

@rsgrafx

rsgrafx/citrusbyte.exs

Last active Dec 18, 2016
Embed
What would you like to do?
Citrusbyte. list reduce in Elixir
defmodule Citrusbyte do
@moduledoc """
This module - demonstrates use of pattern matching in Elixir to modify a list.
[[1,2,[3]],4] -> [1,2,3,4]
Usage:
Citrusbyte.example [[1,2,[3]],4] #=> [1,2,3,4]
"""
def example([h|t], capture \\ []) when is_integer(h) do
example(t, capture ++ [h])
end
def example([h|t], capture) do
example(h ++ t, capture)
end
# Exit function ->
def example([], capture) do
capture
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment