Tips for golfing in Elixir

8

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.

Sheharyar

Posted 2015-12-05T03:57:05.020

Reputation: 651

What does Elixir running on the Erlang VM have to do with the Erlang language? – Alex A. – 2015-12-05T04:03:26.220

Answers

1

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.

Okx

Posted 2015-12-05T03:57:05.020

Reputation: 15 025

1

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

Sheharyar

Posted 2015-12-05T03:57:05.020

Reputation: 651

1

String and char arguments don't need spaces

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

LegionMammal978

Posted 2015-12-05T03:57:05.020

Reputation: 15 731

0

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}

Sheharyar

Posted 2015-12-05T03:57:05.020

Reputation: 651

1Can you similarly remove the spaces after the colons? – Alex A. – 2015-12-05T20:51:30.150

Sadly, no. But you can remove spaces after semicolons and commas. – Sheharyar – 2015-12-05T23:12:39.903