pertsevds
PipeAssign - Keep the Pipe, Capture the Value
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:
Most Liked
benwilson512
Because those are local to the pipe, they aren’t binding values.
This inverts the normal order of binding where the thing being bound is normally on the LHS and now it’s on the RHS. The bit where binding a value interrupts a pipe is a Good Thing. You want that visual cue to say “this value has been shadowed or set”
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
sodapopcan
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









