11
\$\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\$
1
  • \$\begingroup\$ What does Elixir running on the Erlang VM have to do with the Erlang language? \$\endgroup\$
    – Alex A.
    Dec 5, 2015 at 4:03

6 Answers 6

2
\$\begingroup\$

String and char arguments don't need spaces

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

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

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

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you only need one bitwise op you can use Bitwise.&&&() or whatever too \$\endgroup\$
    – naffetS
    Jan 12 at 14:58

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.