Skip to content

Commit

Permalink
Deprecate map after reduce
Browse files Browse the repository at this point in the history
Closes #109.
  • Loading branch information
josevalim committed Feb 10, 2022
1 parent 720792d commit fe4a2a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
11 changes: 8 additions & 3 deletions lib/flow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1171,9 +1171,9 @@ defmodule Flow do
## Examples
iex> flow = Flow.from_enumerable([foo: 1, foo: 2, bar: 3, foo: 4, bar: 5], stages: 1)
iex> flow |> Flow.group_by_key() |> Flow.map_values(&Enum.sort/1) |> Enum.sort()
[bar: [3, 5], foo: [1, 2, 4]]
iex> flow = Flow.from_enumerable([a: 1, b: 2, c: 3, d: 4, e: 5], stages: 1)
iex> flow |> Flow.map_values(& &1 * 2) |> Enum.sort()
[a: 2, b: 4, c: 6, d: 8, e: 10]
"""
def map_values(flow, value_fun) when is_function(value_fun) do
Expand Down Expand Up @@ -1912,6 +1912,11 @@ defmodule Flow do
"since events have already been emitted"
end

if has_any_reduce?(flow) do
IO.warn("Using a mapper operation, such as map/filter/reject, after reduce/3 is deprecated. " <>
"Use Flow.on_trigger/2 instead")
end

add_operation(flow, {:mapper, name, args})
end

Expand Down
28 changes: 0 additions & 28 deletions test/flow_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -727,34 +727,6 @@ defmodule FlowTest do
assert Enum.sort(flow) == [6, 10, 14, 18, 22]
end

test "keeps ordering after reduce" do
flow =
@flow
|> Flow.reduce(fn -> [] end, &[&1 | &2])
|> Flow.filter(&(rem(&1, 2) == 0))
|> Flow.map(fn x -> x + 1 end)
|> Flow.map(fn x -> x * 2 end)

assert Enum.sort(flow) == [6, 10, 14, 18, 22]
end

test "keeps ordering after reduce + map_state" do
flow =
@flow
|> Flow.reduce(fn -> [] end, &[&1 | &2])
|> Flow.filter(&(rem(&1, 2) == 0))
|> Flow.map(fn x -> x + 1 end)
|> Flow.map(fn x -> x * 2 end)
|> Flow.on_trigger(&{[{&2, Enum.sort(&1)}], &1})

assert Enum.sort(flow) == [
{{0, 4}, [6, 14, 18]},
{{1, 4}, [22]},
{{2, 4}, []},
{{3, 4}, [10]}
]
end

test "start_link/2" do
parent = self()

Expand Down

0 comments on commit fe4a2a5

Please sign in to comment.