简体   繁体   English

求和药剂

[英]Summation Elixir

I am trying to recreate this equation in Elixir:我试图在 Elixir 中重新创建这个等式:

在此处输入图片说明

For now I am working on an easy example and I have something like this:现在我正在研究一个简单的例子,我有这样的事情:

Enum.each(1..2, fn x -> :math.pow(1 + 1/1, -x) end)

However, while using Enum.each I am getting an :ok output, and therefore I can't inject it later to Enum.sum()但是,在使用 Enum.each 时,我得到了一个 :ok 输出,因此我以后无法将其注入 Enum.sum()

I will be grateful for help.我将不胜感激。

While the answer by @sabiwara is perfectly correct, one'd better either use Stream.map/2 to avoid building the intermediate list that might be huge, or directly Enum.reduce/3 to the answer.虽然@sabiwara 的答案是完全正确的,但最好使用Stream.map/2来避免构建可能很大的中间列表,或者直接使用Enum.reduce/3来回答。

#                 ⇓ initial value
Enum.reduce(1..2, 0, &:math.pow(1 + 1/1, -&1) + &2)

Enum.each/2 is for side effects, but does not return a transformed list. Enum.each/2用于副作用,但不返回转换后的列表。

You are looking for Enum.map/2 .您正在寻找Enum.map/2

Alternatively, you could use a for comprehension: for x <- 1..2, do: :math.pow(1 + 1/1, -x)或者,您可以使用 a for理解: for x <- 1..2, do: :math.pow(1 + 1/1, -x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM