Skip to content

Instantly share code, notes, and snippets.

@osdrv
Created December 16, 2015 23:02
Show Gist options
  • Save osdrv/8e3ca53a3ef88c1bcd63 to your computer and use it in GitHub Desktop.
Save osdrv/8e3ca53a3ef88c1bcd63 to your computer and use it in GitHub Desktop.
defmodule SmartJoin do
defp _join([ el ]), do: el
defp _join([ el2, el1 ]), do: "#{el1} and #{el2}"
defp _join([ el2, el1 | tail ]), do: "#{Enum.join( Enum.reverse(tail), ", " )}, #{join([el1, el2])}"
def join(list), do: _join Enum.reverse(list)
end
Enum.each([
["foo"],
["foo", "bar"],
["foo", "bar", "baz"],
["foo", "bar", "baz", "etc"],
], &IO.inspect(SmartJoin.join(&1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment