tomr
Hey elixirists,
another of my questions regarding piping:
The following code works perfectly well for what I want to achieve, but I don’t like the reuse of the var - it looks like I want to use pipes. I have not been able to make it work as I need to access the piped value in between. How can I improve the code?
def show(conn, %{"id" => id}) do
project = Clients.get_project!(id)
project = Map.put_new(project, :budget, total_budget(project.budget_items))
project = Map.put_new(project, :budget_used, budget_used(project.work_items))
project = Map.put_new(project, :budget_remaining, project.budget - project.budget_used)
changeset = Clients.change_budget_item(%BudgetItem{project_id: project.id})
render(conn, "show.html", project: project, changeset: changeset)
end
Thanks ![]()
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
code-shoily
Look up
Map.merge/3. Probably something like:Also maybe extract it out as a separate function (
get_or_add_budgets?) inClientscontext or something?ericgray
Instead of reusing the variable you can take
Map.putand wrap it into three separate functions. Each functionwill update the struct. Not sure what your struct looks like so I’ll just use
%Project{}Now you can set up a pipeline for your struct.
axelson
I agree that Map.merge would be good to use here, I just would like to point out that it will override any existing values (unlike Map.put_new). But I would probably restructure the code so that Map.merge would work well.
tomr
Thanks everyone, my current approach is now:
I still quite like @ericgray s idea of splitting this up into separate functions. I think I would go this way.
srowley
I think here the function getting passed to
Map.merge/3makes sure there are no overrides.Just judging from the code it seems odd to me that you wouldn’t want to override existing values (especially if
projectis a struct), but the OP says that works for them so I’ll take them at their word.I also want to +1 the recommendation to put all of this in a function in a context module as opposed to in a controller, which is what appears to be happening.
axelson
Ah, good point, you’re correct. I didn’t notice that it was
Map.merge/3being used instead ofMap.merge/2.srowley
For what it’s worth, I might do:
ericgray
Map.merge/3is nice succinct solution but I think he wishes to see how to use pipes to transform data. I’m sure he’ll revisit it once he gains more experience. It’s a little harder to understand whatMap.mergedoes as opposed toMap.putsrowley
That’s fine. I think there is a practical reason not to create separate functions for each operation - you never want those functions to be called in isolation. You would never call just the function to update the budget and the budget used without also updating the budget remaining, because you would have values that don’t tie (i.e.,
budget-budget_usedmight not equalbudget_remaining.)So while normally I would say the degree to which you extract smaller functions from larger functions is ultimately a matter of personal style, in this case it is probably better not to make it possible to use any of these three update functions in isolation.
ericgray
Good point you’d have to be careful not to call those functions in isolation. I’m not sure exactly what he’s trying to accomplish. Just gave an example of how to pipe it. Great input tho, something he should keep in mind.