pertsevds

pertsevds

Hey folks,
I made a tiny macro that lets you assign a piped value to a variable without breaking the flow. For example:

data
|> transform()
|> process()
|> finalize()
|> assign_to(result) # Assign to result variable

or even during the pipeline:

data
|> transform()
|> assign_to(step1)  # Capture without breaking flow
|> process()
|> assign_to(step2)
|> finalize()
|> assign_to(result) # Assign to result variable

Sure, it’s not idiomatic — but we’ve got then/1, tap/1, even throw/1, so I got curious about what assign from pipe could look like.

Had to jump through a few hoops to avoid compiler warnings, but it works smoothly now.

Docs: pipe_assign v1.1.2 — Documentation
Hex.pm: pipe_assign | Hex
Repo:

https://github.com/pertsevds/pipe_assign

Showing Posts 1 to 10

DaAnalyst

DaAnalyst

Bookmarked for the very next time I need it

Thanks!

tomg

tomg

You could achieve the same result and readability by using the with clause in my opinion:

with step1 <- transform(data),
     step2 <- process(step1),
     result <- finalize(step2) do
  # do something with variables
end
pertsevds

pertsevds OP

Yes. But I love pipes :heart:

byu

byu

I look forward to trying this out.

How would you compare and contrast this library’s aims and functionality with the ok library?

pertsevds

pertsevds OP

ok is a syntactically shorter with and then alternative. It’s not suitable to assign to variable in pipe flow.

pipe_assign is an alternative for an equal sign but not before pipe flow, but after. It’s suitable to assign to variable in pipe flow.

derek-zhou

derek-zhou

I wonder what compile warnings you were referring to. The last time I checked, this works:

  defmacro bind_to(value, name) do
    quote do
      unquote(name) = unquote(value)
    end
  end
pertsevds

pertsevds OP

result = 0

[1, 2, 3]
|> Enum.map(&(&1 * 2))
|> Enum.sum()
|> bind_to(result)

warning: variable “result” is unused

derek-zhou

derek-zhou

If you are not going to use the old result why initialize it? If you are not going to use the new result why bind to it? IMHO this warning is useful.

pertsevds

pertsevds OP

Now when I think about it… you’re right! =)
I’ll change the behavior.
Thank you.

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews