Code Golf in Elixir

Elixir has some great short forms of functions:

This is the identity function:

& &1

You can use this to turn a list into a map of counts:

~w[a b c]a |> Map.new(&{&1, 0})

The if statement is an expression and will return nil for the else clause

a = if foo == "hello", do: "yes", else: nil end

There is also an unless that is the reverse of if

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s