8
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ What does Elixir running on the Erlang VM have to do with the Erlang language? \$\endgroup\$ – Alex A. Dec 5 '15 at 4:03
1
\$\begingroup\$

String and char arguments don't need spaces

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

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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}
\$\endgroup\$
  • 1
    \$\begingroup\$ Can you similarly remove the spaces after the colons? \$\endgroup\$ – Alex A. Dec 5 '15 at 20:51
  • \$\begingroup\$ Sadly, no. But you can remove spaces after semicolons and commas. \$\endgroup\$ – Sheharyar Dec 5 '15 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.