https://github.com/princemaple/elixir-side-effect
https://github.com/princemaple/elixir-side-effect
elixir pipe
Last synced: 10 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/princemaple/elixir-side-effect
- Owner: princemaple
- License: mit
- Created: 2016-10-03T23:27:50.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-06-05T06:44:30.000Z (over 2 years ago)
- Last Synced: 2025-04-04T14:49:48.815Z (10 months ago)
- Topics: elixir, pipe
- Language: Elixir
- Size: 9.77 KB
- Stars: 11
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SideEffect
simple utils for making side effect calls and return the piped-in value in pipelines
## Installation
Add `side_effect` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:side_effect, "~> 0.1.0"}]
end
```
## Why
so instead of writing this:
```elixir
def three_transformations_with_a_side_effect(x) do
temp =
x
|> transform1()
|> transform2()
notify_someone(temp)
transform3(temp)
end
```
I'd just write:
```elixir
def three_transformations_with_a_side_effect(x) do
x
|> transform1()
|> transform2()
|> SideEffect.side_apply(¬ify_someone/1)
|> transform3()
end
```
## Usage
```
iex> 1 |> SideEffect.side_call(MyApp.send_notification()) |> List.wrap
[1]
iex> 1 |> SideEffect.side_apply(&MyApp.send_notification/1) |> List.wrap
[1]
iex> 1 |> SideEffect.side_apply(IO, :inspect) |> List.wrap
[1]
iex> 1 |> SideEffect.side_apply(IO, :inspect, [[base: :hex]]) |> List.wrap
[1]
```
```elixir
defmodule M do
def test(a, b, c, d), do: IO.inspect([a, b, c, d])
end
b = 2
SideEffect.side_apply(b, M, :test, [1, 3, 4], 1)
# prints [1,2,3,4]
# returns 2
```