Hi. Currently, I try to go trough all products and count the price of all of then and update order price. However, I struggle with this for some while, because I, in the end, receive different values in different loop stages.
The end result what I want to archive, is to have order.price with total price of all products.
def add(variant_ids \\ [], order \\ %{}) do
# If price is nil, then set inital value as 0.00
case order.price do
nil -> order = %{order | price: Decimal.new(0)}
_ ->
end
for variant_id <- variant_ids do
{_, combination} = variant_id
variant = Variant
|> Repo.get!(combination["variant_id"])
|> Repo.preload([:color, :product, :product_size, :product_type, :photo])
for _i <- 1..String.to_integer(combination["count"]) do
order = %{order | price: Decimal.add(order.price, variant.product.price)}
end
IO.inspect order # Price is updated, however only for one iteration, the last interation result is lost
end
IO.inspect order # Price stays decimal 0
end
Also, why are you setting order to new bindings all over the place? It looks like you are trying to use it like a mutable variable, but there is no mutation here, should remove those. Also, why are you setting order inside a for when there is nothing done with that order, thus making that binding useless? o.O?
Note that it’s not tested! Aside from that I would suggest you read up on some Elixir books / tutorials, it seems your previous experience in other languages is what’s making the code look strange.
The defaults in the add function make no sense, you will never call this action without params (hence invoking the defaults).
Don’t preload relations that you are not going to use, color, photo and etc are not needed to do what you need, calculate the total order price.
Sure, currently I do that, this was just one try to do it in this way. And I did this only because I wanted to learn more how to do these things in Elixir.
Oy! My last message was more terse than I wanted! I thought I rewrote it with the solutions, heh. ^.^
Remember that Elixir/Erlang is a functional language (impure). Haskell is a pure functional language (although the operator overloading is crazy there), but it would be a great language to learn how to program functionally properly (since you have no choice but to do it the proper way in Haskell) and it will help your code when you program in other more mainstream languages too, it is worth learning (with plenty of tutorials! Learn you a Haskell is a fun one).
OCaml is definitely easier to read than Haskell but it is not quite ‘pure’ either (it is more like Elixir/Erlang), however if you ignore ref’s and Array’s in OCaml then it might be a better way to learn.
It is not the language that is important though, but rather the patterns of programming, these functional patterns once learned will help you in any language you use.