Frage

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 ?!

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top