Skip to content

Commit cd85761

Browse files
committed
add Enum.sum_by/2
1 parent b030bb1 commit cd85761

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
\matrix [list=a] {
2+
\node [index=1]; &
3+
\node [index=2]; &
4+
\node [elements between]; &
5+
\node [index=n]; \\
6+
};
7+
8+
\foreach \i in {1,2,n}{
9+
\node (fun\i) [fun, below=.5 of a\i.south];
10+
\draw [->] (a\i.south) -- (fun\i.in);
11+
\draw [->] (fun\i.out) -- +(0, -.5)
12+
node (b\i) [below] {$b_{\i}$};
13+
};
14+
15+
16+
17+
\draw [brace] (bn.south east) -- (b1.south west);
18+
19+
\draw [->] (last brace) -- +(0, -1)
20+
node [below]
21+
{$b_1 + b_2 + \dots + b_n = \displaystyle\sum_{i=1}^n b_i$};
22+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
title: sum_by/2
3+
url: Enum/sum_by/2
4+
---
5+
6+
Maps and computes the sum of the given `enumerable` in one pass.
7+
8+
Raises `ArithmeticError` if `fun` returns a non-numeric value.
9+
10+
{{< figure src="images/functions/Enum/sum_by-2.svg" >}}

data/signatures.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@
364364
defmodule Enum do
365365
def sum(enumerable)
366366
end
367+
- name: sum_by/2
368+
signature: |
369+
defmodule Enum do
370+
def sum_by(enumerable, fun)
371+
end
367372
- name: take/2
368373
signature: |
369374
defmodule Enum do

0 commit comments

Comments
 (0)