【问题标题】:How do you make this work ... and why does it not work?你如何使这项工作......为什么它不起作用?
【发布时间】:2014-03-08 17:43:33
【问题描述】:

我在下面尝试了这个:

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

知道的人可以告诉我为什么这不起作用吗?!

【问题讨论】:

  • 不在电脑附近,但试试String.upcase("hello world") |> String.split |> Enum.join(" // ")

标签: elixir


【解决方案1】:

你应该写:

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

这里注意两点:

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

  2. String.split("hello world") => ["hello", "world"]String.upcase(["hello", "world"]) 将不起作用。

【讨论】:

  • 有趣的事实:String.upcase ("hello world") |> String.split |> Enum.join(" // ") 也有效 :)
猜你喜欢
  • 2021-07-27
  • 1970-01-01
  • 2015-09-12
  • 2012-11-19
  • 2012-11-17
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多