Djordjenp
I am new to elixir and I am trying to make a recursive anonymous function, but for some reason my anynymous function that works on it’s own as expected, throws me “2nd argument: not valid character data (an iodata term)” error.
Here is code:
calcTip = fn bill ->
if bill >= 50 and bill <= 300, do: bill * 0.15, else: bill * 0.2
end
bills = [ 22, 295, 176, 440, 37, 105, 10, 1100, 86, 52 ]
calcTipsAndTotals = fn index, tips, totals, recursiveFn ->
case index < length(bills) do
true ->
new_tip = calcTip.(Enum.at(bills, index, 0))
new_tips = tips ++ [new_tip]
new_totals = totals ++ [Enum.at(new_tips, index) + Enum.at(bills, index)]
recursiveFn.(index + 1, new_tips, new_totals, recursiveFn)
false -> [tips, totals]
end
end
IO.puts(
calcTipsAndTotals.(0, [], [], calcTipsAndTotals)
)
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
adamu
al2o3cr
Not directly related to your question, but a general tip for new Elixir devs: calling functions like
lengthandEnum.atinside a loop should make you slightly worried about performance.The reason is that both of those functions take time that’s proportional to the size of the input (
billshere usually), unlike other languages where arrays can be accessed in a constant amount of time. This means that calculating something likelength(bills)inside a recursion overbillswill immediately be accidentally quadratic.IMO a good general principle is to avoid using indexes as much as possible. For instance, in your code above, every call to
Enum.atuses the same index so the whole thing can be rewritten as anEnum.map:or an alternate version with explicit recursion, if that’s a requirement:
Some general notes from the above:
length(which is expensive) this pattern-matches on[bill | rest]vs[](which is super-cheap)totals ++ [new_value], this adds to the beginning of the list (super-cheap again) and then reverses at the end. See the BEAM Efficiency Guide for some additional discussion on this.tipsandtotals, but you may want to consider keeping those things together either as a tuple (omit theEnum.unzip) or even a map/struct. That way the values for a particular bill are always in one place, instead of spread across multiple lists.