plymio_funcio v0.1.0 Plymio.Funcio.Enum.Map View Source
Map Functions for an Enumerable.
See Plymio.Funcio
for overview and documentation terms.
The concurrent functions currently produce a
List
rather than aStream
. But this could change in the future. The examples below assumes aStream
was returned.
Link to this section Summary
Functions
map_concurrent_enum/3
takes an enum and a map/1, reduces the
map/1 to a single, composite function and applied the composite to each value in
the enum in its own, separate task using using
Task.Supervisor.async_stream_nolink/4
map_concurrent_with_index_enum/2
take a enum and a index map/1 and
maps the enum with Stream.with_index/1
and then map_concurrent_enum/2
returning
{:ok, stream}
map_enum/3
takes an enum and a map/1, reduces the
map/1 to a single, composite function and applied the composite to each value in
the enum returning {:ok, values}
where values
may be a Stream
map_with_index_enum/2
take a enum and a index map/1 and
maps the enum with Stream.with_index/1
and then Stream.map/2
returning
{:ok, stream}
Link to this section Types
Link to this section Functions
map_concurrent_enum/3
takes an enum and a map/1, reduces the
map/1 to a single, composite function and applied the composite to each value in
the enum in its own, separate task using using
Task.Supervisor.async_stream_nolink/4
.
It returns {:ok, values}
where the values
may be a Stream
.
Examples
iex> enum = 0 .. 4
...> fun_map = fn v -> v * v end
...> {:ok, values} = enum
...> |> map_concurrent_enum(fun_map)
...> values |> Enum.to_list
[0, 1, 4, 9, 16]
iex> enum = 0 .. 4
...> fun_map1 = fn v -> v + 1 end
...> fun_map2 = fn v -> v * v end
...> fun_map3 = fn v -> v - 1 end
...> {:ok, values} = enum
...> |> map_concurrent_enum([fun_map1, fun_map2, fun_map3])
...> values |> Enum.to_list
[0, 3, 8, 15, 24]
iex> enum = 0 .. 99
...> fun_map = fn v -> v end
...> {:ok, values} = enum |> map_concurrent_enum(fun_map)
...> values |> Enum.reduce(0, fn v, s -> s + v end)
4950
iex> enum = :not_an_enum
...> fun_map = fn v -> v * v end
...> {:error, error} = enum |> map_concurrent_enum(fun_map)
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true
map_concurrent_with_index_enum/2
take a enum and a index map/1 and
maps the enum with Stream.with_index/1
and then map_concurrent_enum/2
returning
{:ok, stream}
.
The index map/1 is called with {value, index}
for each value
in the enum.
Examples
iex> {:ok, stream} = [1,2,3] |> map_concurrent_with_index_enum(fn {v,_i} -> v * v end)
...> stream |> Enum.to_list
[1, 4, 9]
iex> {:ok, stream} = [1,2,3] |> map_concurrent_with_index_enum(fn {_v,i} -> i * i end)
...> stream |> Enum.to_list
[0, 1, 4]
iex> {:ok, stream} = [a: 1, b: 2, c: 3]
...> |> map_concurrent_with_index_enum(fn {{_k,v},i} -> v * v * i end)
...> stream |> Enum.to_list
[0, 4, 18]
iex> {:ok, stream} = [a: 1, b: 2, c: 3]
...> |> map_concurrent_with_index_enum([
...> fn {{_k,v},i} -> {i, v * v} end,
...> fn {i, v} -> i + v end,
...> ])
...> stream |> Enum.to_list
[1, 5, 11]
iex> {:error, error} = 42 |> map_concurrent_with_index_enum(&(&1))
...> error |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for 42")
true
iex> {:error, error} = [1,2,3] |> map_concurrent_with_index_enum(:not_a_fun)
...> error |> Exception.message
"map/1 function invalid, got: :not_a_fun"
map_enum/3
takes an enum and a map/1, reduces the
map/1 to a single, composite function and applied the composite to each value in
the enum returning {:ok, values}
where values
may be a Stream
.
Examples
iex> enum = 0 .. 4
...> fun_map = fn v -> v * v end
...> {:ok, values} = enum
...> |> map_enum(fun_map)
...> values |> Enum.to_list
[0, 1, 4, 9, 16]
iex> enum = 0 .. 4
...> fun_map1 = fn v -> v + 1 end
...> fun_map2 = fn v -> v * v end
...> fun_map3 = fn v -> v - 1 end
...> {:ok, values} = enum
...> |> map_enum([fun_map1, fun_map2, fun_map3])
...> values |> Enum.to_list
[0, 3, 8, 15, 24]
iex> enum = 0 .. 99 |> Stream.map(&(&1))
...> fun_map = fn v -> v end
...> {:ok, values} = enum
...> |> map_enum(fun_map)
...> values |> Enum.reduce(0, fn v,s -> s + v end)
4950
iex> enum = :not_an_enum
...> fun_map = fn v -> v * v end
...> {:ok, stream} = enum |> map_enum(fun_map)
...> try do
...> stream |> Enum.to_list
...> rescue
...> error -> error
...> end
...> |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true
map_with_index_enum/2
take a enum and a index map/1 and
maps the enum with Stream.with_index/1
and then Stream.map/2
returning
{:ok, stream}
.
The index map/1 is called with {value, index}
for each value
in the enum.
Examples
iex> {:ok, stream} = [1,2,3] |> map_with_index_enum(fn {v,_i} -> v * v end)
...> stream |> Enum.to_list
[1, 4, 9]
iex> {:ok, stream} = [1,2,3] |> map_with_index_enum(fn {_v,i} -> i * i end)
...> stream |> Enum.to_list
[0, 1, 4]
iex> {:ok, stream} = [a: 1, b: 2, c: 3]
...> |> map_with_index_enum(fn {{_k,v},i} -> v * v * i end)
...> stream |> Enum.to_list
[0, 4, 18]
iex> {:ok, stream} = [a: 1, b: 2, c: 3]
...> |> map_with_index_enum([
...> fn {{_k,v},i} -> {i, v * v} end,
...> fn {i, v} -> i + v end,
...> ])
...> stream |> Enum.to_list
[1, 5, 11]
iex> enum = :not_an_enum
...> fun_map = fn v -> v * v end
...> {:ok, stream} = enum |> map_with_index_enum(fun_map)
...> try do
...> stream |> Enum.to_list
...> rescue
...> error -> error
...> end
...> |> Exception.message
...> |> String.starts_with?("protocol Enumerable not implemented for :not_an_enum")
true
iex> {:error, error} = [1,2,3] |> map_with_index_enum(:not_a_fun)
...> error |> Exception.message
"map/1 function invalid, got: :not_a_fun"