【问题标题】:Updating named-function variable inside anonymous function Elixir更新匿名函数 Elixir 中的命名函数变量
【发布时间】:2018-03-07 09:34:37
【问题描述】:

我是 Elixir 的新手,正在尝试解决吉他谱问题。 我的代码:

def sumTabs([head|tail], result) do
        nextLine = hd(tail)
        tail = List.delete(tail, nextLine)
        head
        |> Enum.with_index
        |> Enum.each(fn({x, i}) ->
            s = [x <> Enum.at(nextLine, i) |> to_charlist]
            IO.inspect s
            result = [result | s] 
            IO.inspect result end)
        result
    end

我想更新结果,一开始是一个空列表,但是在 Enum.each 函数的每次迭代中,结果都是空的。我认为这是因为匿名函数。

首先,我希望它至少可以使用两行代码。

输入:

tab = """
  e|-7-----7-----7-----7-----5-----3-----3-----2-----0-----0-----|
  B|---0-----0-----0-----0-----0-----0-----0-----0-----0-----0---|
  G|-----0-----0-----0-----0-----0-----0-----0-----0-----0-----0-|
  D|-------------------------------------------------------------|
  A|-------------------------------------------------------------|
  E|-------------------------------------------------------------|
  """

输出:

['eB']
[[], 'eB']
['||']
[[], '||']
['--']
[[], '--']
['7-']
[[], '7-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['7-']
[[], '7-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['7-']
[[], '7-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['7-']
[[], '7-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['5-']
[[], '5-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['3-']
[[], '3-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['3-']
[[], '3-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['2-']
[[], '2-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['0-']
[[], '0-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['0-']
[[], '0-']
['--']
[[], '--']
['-0']
[[], '-0']
['--']
[[], '--']
['--']
[[], '--']
['--']
[[], '--']
['||']
[[], '||']

sumTabs 函数之前的代码:

defmodule TabPlayer do
    def parse(tab) do
        String.split(tab, "\n") 
            |> Enum.map(fn n -> to_string n end)
            |> List.delete([])
            |> Enum.map(fn n -> String.graphemes n end)
            |> sumTabs([])
            |> IO.inspect
    end

【问题讨论】:

    标签: function variables functional-programming elixir


    【解决方案1】:

    Elixir 中的变量是不可变的。您不能重新分配 Enum.each 内部的值并期望它在外部发生变化。这段代码的作用是创建一个新的result 值,该值一直持续到匿名函数结束。

    您可以在此处使用Enum.reduce/3,在每次迭代时返回新的result 值。假设您的其余逻辑是正确的,以下是如何重写代码以使用Enum.reduce/3。请注意,我正在利用 IO.inspect 打印一个值然后返回它的事实。所以IO.inspect(x); x 和IO.inspect(x) 是一样的。

    def sumTabs([head|tail], result) do
      nextLine = hd(tail)
      tail = List.delete(tail, nextLine)
      head
      |> Enum.with_index
      |> Enum.reduce([], fn({x, i}, acc) ->
        s = IO.inspect [x <> Enum.at(nextLine, i) |> to_charlist]
        IO.inspect [s | acc]
      end)
      # We need to reverse at the end since we're creating a reversed list in
      # `Enum.reduce` for efficiency.
      |> Enum.reverse
    end
    

    【讨论】:

    • 这很好用,谢谢!但是我怎样才能获得“acc”变量以将其提供给下一个函数调用或稍后使用它呢?
    • 您可以像这样存储最终值:result = head |&gt; ...,然后使用它。 Enum.reduce 返回 acc 的最终值。你是这个意思吗?
    • 顺便说一句,@Dogbert,您实际上可以像 sumTabs([first, next | tail]) do ... 那样在函数头中获取 nextLine 变量,然后您不必从尾部删除它。此外,重新分配变量是不好的做法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2018-08-16
    • 2016-11-05
    • 2011-04-20
    • 2014-03-25
    • 2012-07-10
    • 1970-01-01
    相关资源
    最近更新 更多