summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMackenzie <macoafi@gmail.com>2023-05-12 05:11:28 -0400
committerGitHub <noreply@github.com>2023-05-12 11:11:28 +0200
commitae0b3a4fa08aedc08fa9a03e7714b6d1bbb82135 (patch)
treef0a71ca86c18ffbe6a27967e724ceaf6a24c2669
parent08647385a6e4da572cbe508df9d73cc48704a382 (diff)
downloadelixir-ae0b3a4fa08aedc08fa9a03e7714b6d1bbb82135.tar.gz
Add map examples to Enum.reduce/3 and Map.new/2 (#12560)
-rw-r--r--lib/elixir/lib/enum.ex3
-rw-r--r--lib/elixir/lib/map.ex3
2 files changed, 6 insertions, 0 deletions
diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex
index bdc83a20e..b40df675e 100644
--- a/lib/elixir/lib/enum.ex
+++ b/lib/elixir/lib/enum.ex
@@ -2478,6 +2478,9 @@ defmodule Enum do
iex> Enum.reduce([1, 2, 3], 0, fn x, acc -> x + acc end)
6
+ iex> Enum.reduce(%{a: 2, b: 3, c: 4}, 0, fn {_key, val}, acc -> acc + val end)
+ 9
+
## Reduce as a building block
Reduce (sometimes called `fold`) is a basic building block in functional
diff --git a/lib/elixir/lib/map.ex b/lib/elixir/lib/map.ex
index 87be460b4..e23c8a2d4 100644
--- a/lib/elixir/lib/map.ex
+++ b/lib/elixir/lib/map.ex
@@ -235,6 +235,9 @@ defmodule Map do
iex> Map.new([:a, :b], fn x -> {x, x} end)
%{a: :a, b: :b}
+ iex> Map.new(%{a: 2, b: 3, c: 4}, fn {key, val} -> {key, val * 2} end)
+ %{a: 4, b: 6, c: 8}
+
"""
@spec new(Enumerable.t(), (term -> {key, value})) :: map
def new(enumerable, transform)