I wonder about a design pattern that may be used for setting for the function execution pipeline.
Lets say i have users from different countries and i want to calculate salary tax - the calculation changes for every country so I dont want to hardcode it but have a way to set it using json or something similar.
At a high level you’re already on the right track: Pass things as data instead of trying to “pass logic around”. How you encode your invariants into data however depends quite a bit on what really is different between your cases. For simple cases just a struct/map with some keys might be enough. For more complex cases you can create a behaviour and require a module to be supplied, which implements the behaviour (like how genservers work).
Thanks-can you maybe think of a open source project where something similar is implemented? Does not have to be elixir
Depending on how dynamic you want the calculations to be, you could use something like this: https://github.com/narrowtux/abacus - it can parse and evaluate mathematical expressions at run time. That would allow you to carry the expressions around as data and match & apply them at run time.
For the behaviour/implementation part: Any email library, especially when allowing you to select the sending service via the parameters supplied. Also as already mentioned - genservers - otp provides the code, which runs it, while the module you pass in is in charge of implementing how it behaves.
thank you for the replays - I think abacus is exactly what I’m looking for:
iex> Abacus.eval("a * b", %{"a" => 10, "b" => 10})
{:ok, 100}
BTW - there are a couple of Abacus forks on guthub, one of which (I can’t recall which - it’s easy enough to find as it has the most recent commits) has support for aggregations over lists (e.g. SUM, MIN, MAX, COUNT)
Edit: This is the fork with aggregations: https://github.com/valiot/abacus