Hi,
Suppose the Repo query example is wrapped in a function that should return
{:ok, [%User{}]} | {:error, :any},
how would one specify what is returned in the "else" case of a "pipe_if"?
Is nothing returned?
I am not sure if I am missing something but all the use-cases I can think
of can be solved with a "with" statement,
which adds the benefit of clean syntax, pattern matching and reacting
differently to different "else" cases.
Best,
Sven
On Wed, 2 Feb 2022 at 10:52, xward <[email protected]> wrote:
> 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.
>
> - Usecases
>
> - 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 !
>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/elixir-lang-core/97c0c216-99d1-4849-a56b-ca4ffab698f3n%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-core/97c0c216-99d1-4849-a56b-ca4ffab698f3n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/CAFuVuxyRwJPL888iYiRZUH_twtkZA%3DXEObT5xhx37WSRmm6ozw%40mail.gmail.com.