Skip to content

Commit

Permalink
Document zip/2 functions to create keyword lists (#13356)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLavin committed Feb 27, 2024
1 parent 80eef8c commit d2276d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3889,13 +3889,20 @@ defmodule Enum do
Zips corresponding elements from two enumerables into a list
of tuples.
Because a list of two-element tuples with atoms as the first
tuple element is a keyword list (`Keyword`), zipping a first list
of atoms with a second list of any kind creates a keyword list.
The zipping finishes as soon as either enumerable completes.
## Examples
iex> Enum.zip([1, 2, 3], [:a, :b, :c])
[{1, :a}, {2, :b}, {3, :c}]
iex> Enum.zip([:a, :b, :c], [1, 2, 3])
[a: 1, b: 2, c: 3]
iex> Enum.zip([1, 2, 3, 4, 5], [:a, :b, :c])
[{1, :a}, {2, :b}, {3, :c}]
Expand Down
7 changes: 7 additions & 0 deletions lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,11 @@ defmodule Stream do
@doc """
Zips two enumerables together, lazily.
Because a list of two-element tuples with atoms as the first
tuple element is a keyword list (`Keyword`), zipping a first `Stream`
of atoms with a second `Stream` of any kind creates a `Stream`
that generates a keyword list.
The zipping finishes as soon as either enumerable completes.
## Examples
Expand All @@ -1208,6 +1213,8 @@ defmodule Stream do
iex> cycle = Stream.cycle([:a, :b, :c])
iex> Stream.zip(concat, cycle) |> Enum.to_list()
[{1, :a}, {2, :b}, {3, :c}, {4, :a}, {5, :b}, {6, :c}]
iex> Stream.zip(cycle, concat) |> Enum.to_list()
[a: 1, b: 2, c: 3, a: 4, b: 5, c: 6]
"""
@spec zip(Enumerable.t(), Enumerable.t()) :: Enumerable.t()
Expand Down

0 comments on commit d2276d8

Please sign in to comment.