Skip to content

Commit 9b667ef

Browse files
committed
Reversed the arguments and removed the use of binding so that atoms are no longer necessary
1 parent 71ebce5 commit 9b667ef

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@ end
4949
And the second example
5050

5151
```elixir
52-
something |> something_else |> tap(:result, {:ok, result})
52+
something |> something_else |> tap({:ok, result}, result)
5353
```
5454

5555
### Usage
5656

57-
The `tap/3` macro takes `data, return_variable, pattern` for its three parameters. This takes advantage of Elixir's `binding` call. The variables you create in your pattern will be available even after the tap call. Take this use case for example.
57+
The `tap/3` macro takes `data, pattern, return_variable` for its three parameters. The variables you create in your pattern will be available even after the tap call. Take this use case for example.
5858

5959
```elixir
60-
[:a] |> tap(:a, [a]) # => Returns :a
60+
[:a] |> tap([a], a) # => Returns :a
6161
IO.puts "#{a}" # The variable a is available
6262
```
6363

64-
The symbol `:a` passed into `tap` is what variable to return. All other variables will be available after the `tap` call, though `tap` will only return a single variable. This means `tap({:ok, 1}, :r, {e, r})` will return `r` (which has the value 1) but in the next statement, the variable `e` will be available (which has the value `:ok`).
64+
All other variables will be available after the `tap` call, though `tap` will only return a single variable. This means `tap({:ok, 1}, {e, r}, r)` will return `r` (which has the value 1) but in the next statement, the variable `e` will be available (which has the value `:ok`).

lib/pattern_tap.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ defmodule PatternTap do
66
end
77
end
88

9-
defmacro tap(data, var, pattern) do
9+
defmacro tap(data, pattern, var) do
1010
quote do
1111
unquote(pattern) = unquote(data)
12-
binding[unquote(var)]
12+
unquote(var)
1313
end
1414
end
1515

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule PatternTap.Mixfile do
33

44
def project do
55
[app: :pattern_tap,
6-
version: "0.1.0",
6+
version: "0.2.0",
77
elixir: "~> 1.0",
88
description: """
99
Macro for tapping into a pattern match while using the pipe operator

test/pattern_tap_test.exs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@ defmodule PatternTapTest do
33
use PatternTap
44

55
test "can do simple pattern matching" do
6-
assert tap(:ok, nil, :ok) == nil
6+
assert tap(:ok, :ok, nil) == nil
77
end
88

99
test "can do list pattern matching" do
10-
assert tap([:a], :a, [a]) == :a
10+
assert tap([:a], [a], a) == :a
1111
assert a == :a
1212
end
1313

1414
test "can do tuple pattern matching" do
15-
assert tap({:b}, :b, {b}) == :b
15+
assert tap({:b}, {b}, b) == :b
1616
assert b == :b
1717
end
1818

1919
@data [:a, :b, :c]
2020
test "can match with the |> operator" do
21-
assert @data |> Enum.map(&(to_string(&1))) |> tap(:b, [_, b, _]) == "b"
21+
assert @data |> Enum.map(&(to_string(&1))) |> tap([_, b, _], b) == "b"
2222
end
2323

2424
@data [key: :val, key2: :val2]
2525
test "can match |> with keyword lists" do
26-
assert @data |> tap(:v, [_, {:key2, v}]) == :val2
26+
assert @data |> tap([_, {:key2, v}], v) == :val2
2727
end
2828

2929
test "can match typical {:ok, result}" do
30-
assert {:ok, 1} |> tap(:result, {:ok, result}) == 1
30+
assert {:ok, 1} |> tap({:ok, result}, result) == 1
3131
end
32-
32+
3333
end

0 commit comments

Comments
 (0)