Hi !
A few months ago we added a conditional pipe in our code in our 120k+ line of elixir code and it turns out to be very useful.
The proposition is to contribute and add it to Elixir kernel, also to find out if we can do it better thanks to your feedback here.
- avoid breaking a pipe chain to make an if test
```
def clean_list(list, real?) do
new_list =
list
|> do_something()
if real?, do: new_list |> do_stuff(), else: new_list
end
```
- avoid creating a bunch of pattern matching functions to do the same thing (even though this one is quite clean)
```
def clean_list(list, real?) do
list
|> do_something()
|> maybe_do_stuff(real?)
end
def maybe_do_stuff(list, true), do: list |> do_stuff()
def maybe_do_stuff(list, _), do: list
```
- proposition usage example
```
def clean_list(list, real?) do
list
|> do_something()
# this can be named differently, or even create something funky like `|>?`
|> pipe_if(real?, do_stuff())
end
```
```
# Repo queries
MySchema
|> where(user_id: 12)
|> pipe_if(page_number > 0, offset(^(page_number * @page_size)))
|> Repo.all()
User
|> where([u], u.organisation_id == ^organisation_id)
|> pipe_if(only_admins?, where([u], u.admin == true))
|> Repo.all()
```
What do you think ?
Have a nice day !