mayel

mayel

Arrows - extended pipe operators (e.g. pipe to any argument position)

Provides OK-piping (similar to libraries like ok) and adds the ability to pipe into any argument position (similar to magritte) of the following function (or nested function). By inserting ... where you would like the value to be inserted, Arrows will override where it is placed.

This library was developed as part of the Bonfire project, which has some open bounties for any help improving performance: Bonfire Networks: An open-source framework for building federated digital spaces

So instead of only being limited to:

iex> 4 |> div(2)
2

You can also do:

iex> 4 |> div(2, ...)
0

Or even:

iex> 4 |> div(2, min(..., 1))
2

Links:

https://github.com/bonfire-networks/arrows

First 9 of 9 Posts Switch mode

Nefcairon

Nefcairon

Ah, the three dots indicate where the object is put. Now I understand.

mayel

mayel OP

That’s right! The documentation do with a rewrite… (have now edited the description to mention that)

tomekowal

tomekowal

It is quite nice and readable :+1: However, it is also not so hard to replace with Kernel.then/2

could become

iex> 4 |> then(&div(2, &1))
0
4 |> then(&div(2, min(&1, 1)))
2
mayel

mayel OP

Yeah this was created before then was introduced, so probably not needed if you’re used to that syntax.

hauleth

hauleth

I see that the main difference between this and Magritte is that it do not have my limitations. Actually I have intentionally added these limitations to avoid things like:

foo
|> bar(fn a ->
  a
  |> baz(2137, ...) # what value should be inserted there?
end)

Also, when you want to use ... twice within single pipe there is question whether there should be 2 separate parent pipes or single one. Aka whether this code:

:rand.uniform()
|> Kernel.==(..., ...)

Should always return true?

I probably should have describe reasoning better in Magritte documentation.

mayel

mayel OP

Yeah that makes sense, though FWIW we’ve been happily using it without running into confusion on these topics so far.

In our case it pipes the same thing twice in that scenario:

    assert true = :rand.uniform()
    |> Kernel.==(..., ...)
jam

jam

Thanks for making this package! The ~> looks like a nice alternative to with. Any gotchas or downsides to be aware of? Looks like it could always be used in place of the |> or are there still scenarios where you’d want to use |>?

Also, would you mind documenting the other functions in more detail in the hex doc?

sodapopcan

sodapopcan

with serves a very different purpose. <- is a match operator much like = with the difference that it returns the value of the expression if there is no match instead of raising. with is about running its do block only when all of its clauses match. This package is just about being able to pipe into other positional arguments.

Oops, sorry I misread what ~> was. There is another library like this that uses ~>. My bad.

mayel

mayel OP

I’ve added some more docs and examples: Arrows — arrows v0.2.1

Regarding ~> I tend to use it mostly for short pipes where with may feel verbose, but for longer ones I prefer to be more explicit. In part because of this gotcha:

      # Note that the following would pass :error to `String.pad_leading` breaking our pipe: 
      # :error ~> Integer.to_string() |> String.pad_leading(2, "0")
      :error

      # Instead we want to do:
      iex> :error ~> Integer.to_string() ~> String.pad_leading(2, "0")
      :error
— All posts loaded —

Where Next?

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 14960 120
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

Other Trending Topics Top

type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
bjorng
We want to introduce a new native datatype to Erlang: native records. Although replacing all tuple records with native records is not our...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New

We're in Beta

About us Mission Statement