【问题标题】:How to pipe Enum output to another Enum function that takes multiple arguments?如何将 Enum 输出通过管道传输到另一个采用多个参数的 Enum 函数?
【发布时间】:2016-06-11 19:00:54
【问题描述】:

我只是在学习 Elixir,我正在尝试这样做:

list = Enum.with_index ~w[a n b e c r z b d]

#=> [{"a", 0}, {"n", 1}, {"b", 2}, {"e", 3}, {"c", 4}, {"r", 5}, {"z", 6}, {"b", 7},
 {"d", 8}]

Enum.into(list, %{})

#=> %{"a" => 0, "b" => 7, "c" => 4, "d" => 8, "e" => 3, "n" => 1, "r" => 5,
  "z" => 6}

我想把它当成一个管道......像这样:

Enum.with_index ~w[a n b e c r z b d] |> Enum.into(%{})

Enum.with_index ~w[a n b e c r z b d] |> Enum.into(&1, %{})

但这些都不起作用。这甚至可能吗?

【问题讨论】:

    标签: elixir


    【解决方案1】:

    您缺少括号:

    Enum.with_index(~w[a n b e c r z b d]) |> Enum.into(%{})
    

    或者更习惯的说法:

    ~w[a n b e c r z b d] |> Enum.with_index() |> Enum.into(%{})
    

    您的原始版本将被执行为:

    Enum.with_index(~w[a n b e c r z b d] |> Enum.into(%{}))
    

    你可以看Why Can't I Chain String.replace?对此的详细解释。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-17
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多