I love the pipe operator. I really do. However, I worry that some people are
overusing it. What's wrong with this?:
def return_first(id_list) do
{:ok, List.first!(id_list)}
end
I really don't see put_ok/1 and put_error/1 being worth putting in std lib. I
don't want to encourage over-use of the pipe operator.
Granted you gave a simplistic example, but even if things were warranting a
more complex pipeline, you can still do:
def return_first(id_list) do
{
:ok,
id_list |> List.first!
}
end
with no addition to the standard library.
-Greg Vaughn
> On May 19, 2016, at 4:47 PM, Chris <[email protected]> wrote:
>
> These are a couple of small wrapper functions which I think would be very
> useful to developers. They create an idiomatic pathway for returning tuples
> from a pipeline.
>
>
>
> The intended use case is in any module-level function that is entirely a
> pipeline, and where you would like to return the value that is the result of
> a pipeline of functions in tuple form, and
> where you know the result of your pipeline will be either :ok or :error with
> certainty.
>
> For a simplistic example, suppose I have a list of ids and I write a simple
> function to take the first id and return it in an
> :ok tuple. Normally I would have to do something like this:
>
>
>
> def return_first(id_list) do
> first_id = id_list |> List.first!()
> {:ok, first_id}
> end
>
>
>
> And that is okay, but somehow it doesn't feel like idiomatic
> Elixir. Here you could just do this:
>
>
>
> def return_first(id_list), do: id_list |> List.first!() |> put_ok()
>
>
>
> To me, that feels a lot cleaner and clearer. It allows me to express my
> intention the in a way that feels closer to how Elixir
> asks me to think about programming.
>
>
> --
> 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/60d8054e-7f88-48ad-9ead-5ccc8ba1eccf%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
--
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/B4DC6675-D85A-4CE8-B13C-D49D6674FEAE%40gmail.com.
For more options, visit https://groups.google.com/d/optout.