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’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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #elixirconf-eu
- #metaprogramming
- #hex











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.