Question

I tried this below:

iex(7)> String.split ("hello world") |> String.upcase |> Enum.join(" // ")

and I got this :

** (Protocol.UndefinedError) protocol Enumerable not implemented for "HELLO WORLD"
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:112: Enumerable.reduce/3
(elixir) lib/enum.ex:1124: Enum.reduce/3

Can someone who knows please enlighten me on why this does not work ?!

Was it helpful?

Solution

You should write:

String.upcase("hello world") |> String.split |> Enum.join(" // ")

Notice here two points:

  1. f(x) |> g equals to g(f(x)), while f (x) |> g equals to f(g(x)) (the whitespace affects operator precedence).

  2. String.split("hello world") => ["hello", "world"] and String.upcase(["hello", "world"]) will not work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top