我试过这个:
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)
有谁知道请告诉我为什么这不起作用?!
你应该写:
String.upcase("hello world") |> String.split |> Enum.join(" // ")
Run Code Online (Sandbox Code Playgroud)
请注意这里有两点:
f(x) |> g等于g(f(x)),而f (x) |> g等于f(g(x))(空格影响运算符优先级).
String.split("hello world") => ["hello", "world"]并且String.upcase(["hello", "world"])不会起作用.
| 归档时间: |
|
| 查看次数: |
361 次 |
| 最近记录: |