【问题标题】:Is there an Elixir function which is equivalent to Python's itertools.accumulate?是否有相当于 Python 的 itertools.accumulate 的 Elixir 函数?
【发布时间】:2021-04-05 18:20:43
【问题描述】:

我想使用一个函数,它接受一个可枚举和一个函数,并与 Python 的 itertools.accumulate 做同样的事情。例如,

iex> accumulate([1,3,7], &Kernel.+)
[1, 4, 11]

为了解释,它等于[1, 1+2, 1+4+7]。 Elixir 的标准库中是否存在这样的函数?

【问题讨论】:

    标签: functional-programming elixir


    【解决方案1】:

    Enum.scan/2 做同样的事情。

    Enum.scan([1, 3, 7], &+/2)
    #⇒ [1, 4, 11]
    

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      相关资源
      最近更新 更多