0

Elixir lets me write this:

"Elixir rocks" |> String.upcase() |> String.split()

And I can introduce a function to combine the upcasing and the split:

upcase_split = fn (data) -> data |> String.upcase() |> String.split() end
"Elixir rocks" |> upcase_split()

Or

upcase_split_shorter = &(&1 |> String.upcase() |> String.split())
"Elixir rocks" |> upcase_split_shorter()

Is there a way to do this point-free? That is, without having to give a name to that first argument that I need to pass through the whole way?

This doesn't work:

nope = String.upcase() |> String.split()

but I was hoping there was something along those lines that might?

1 Answer 1

2

Anonymous functions are called differently than regular module functions. It's better to define your function in a module and call it from there but if you insist on using an anonymous function, you have to use the dot to call it. Like fun.():

"Elixir rocks" |> (&(&1 |> String.upcase() |> String.split())).()

or simply:

upcase_split_shorter = &(&1 |> String.upcase() |> String.split())
"Elixir rocks" |> upcase_split_shorter.()

While Elixir doesn't officially support currying, you can take a look at some packages and other resources that implement it:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.