Skip to content

Instantly share code, notes, and snippets.

@rsgrafx
Last active December 18, 2016 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsgrafx/d3103a4d7f7e3400fac24242192e5d87 to your computer and use it in GitHub Desktop.
Save rsgrafx/d3103a4d7f7e3400fac24242192e5d87 to your computer and use it in GitHub Desktop.
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