# Tips for golfing in Elixir

Elixir is a relatively new functional language, which I've taken a deep interest in. I'd like to get general tips about golfing in Elixir and learn techniques that can be applied to code-golf problems that are somewhat specific to Elixir in general.

Based on so many similar questions about Python, Ruby, Javascript, etc. I'll also try to post tips as I learn more about the language.

• What does Elixir running on the Erlang VM have to do with the Erlang language? Dec 5, 2015 at 4:03

### String and char arguments don't need spaces

For example, IO.puts"Hello, World!" and IO.puts'cat' are valid programs.

### Don't use the Pipe operator or parenthesis for calling methods

# With Pipe
arg |> M.a |> M.b |> M.c   # 24 Bytes
arg|>M.a|>M.b|>M.c         # 18 Bytes

# With Parenthesis
M.c(M.b(M.a(arg)))         # 18 Bytes

# Only Spaces
M.c M.b M.a arg            # 15 Bytes


## Inject code into strings

Instead of concatenating something into a string, like:

"prefix"<>code<>"suffix"


You can use #{} to insert it into the string:

"prefix#{code}suffix"


This will save 3 bytes.

# Avoid arrow functions where possible

...and use the &<func>(&1,&2...) notation. For example:

each 0..25,fn x->IO.puts slice(concat(a,a),x,26) # 48 bytes
each 0..25,&IO.puts slice(concat(a,a),&1,26)     # 44 bytes


### Map Arguments don't need spaces either

Like LegionMammal978's answer, you can leave out space when passing Map as an argument to a method:

IO.inspect%{a: 1,b: 2}

• Can you similarly remove the spaces after the colons? Dec 5, 2015 at 20:51
• Sadly, no. But you can remove spaces after semicolons and commas. Dec 5, 2015 at 23:12

# use Bitwise

Say you want to import the module Bitwise, usually you would do:

import Bitwise


But there may be an alternative that works in some versions of Elixir:

use Bitwise


to save 3 bytes!

Even better if your only using a bit wise operator once, you can do:

Bitwise.&&&()


&&& being one of the many bitwise operators useable.

• If you only need one bitwise op you can use Bitwise.&&&() or whatever too Jan 12 at 14:58