Created
December 16, 2015 23:02
-
-
Save osdrv/8e3ca53a3ef88c1bcd63 to your computer and use it in GitHub Desktop.
This file contains 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 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