你是如何做这项工作的?为什么它不起作用?

Low*_*ong 1 elixir

我试过这个:

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

我得到了这个:

** (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
Run Code Online (Sandbox Code Playgroud)

有谁知道请告诉我为什么这不起作用?!

Ram*_*nir 6

你应该写:

String.upcase("hello world") |> String.split |> Enum.join(" // ")
Run Code Online (Sandbox Code Playgroud)

请注意这里有两点:

  1. f(x) |> g等于g(f(x)),而f (x) |> g等于f(g(x))(空格影响运算符优先级).

  2. String.split("hello world") => ["hello", "world"]并且String.upcase(["hello", "world"])不会起作用.