Maxximiliann
defmodule Sandbox do
def currencies do
[
%{
"USD" => %{
asset: "USD",
exchange_currencies: ["JPY"],
priced_in: "KHR"
}
},
%{
"CVE" => %{
asset: "CVE",
exchange_currencies: ["JPY"],
priced_in: "KHR"
}
},
%{
"CVE" => %{
asset: "CVE",
exchange_currencies: ["KHR", "JPY"],
priced_in: "USD"
}
},
%{
"AUD" => %{
asset: "AUD",
exchange_currencies: ["JPY"],
priced_in: "KHR"
}
},
%{
"AUD" => %{
asset: "AUD",
exchange_currencies: ["KHR", "USD", "JPY", "ZAR"],
priced_in: "KRW"
}
},
%{
"AUD" => %{
asset: "AUD",
exchange_currencies: ["KHR", "JPY"],
priced_in: "USD"
}
},
%{
"AUD" => %{
asset: "AUD",
exchange_currencies: ["KHR", "KRW", "LAK", "MGA", "XOF", "USD", "JPY", "AUD", "EUR"],
priced_in: "ZAR"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["JPY"],
priced_in: "KHR"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["KHR", "USD", "JPY", "ZAR"],
priced_in: "KRW"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["JPY", "ZAR"],
priced_in: "LAK"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["KHR", "USD", "ZAR"],
priced_in: "MGA"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["KHR", "USD", "JPY", "ZAR"],
priced_in: "XOF"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["KHR", "JPY"],
priced_in: "USD"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["KHR", "KRW", "USD", "JPY", "ZAR"],
priced_in: "AUD"
}
},
%{
"ZAR" => %{
asset: "ZAR",
exchange_currencies: ["LAK", "ZAR"],
priced_in: "EUR"
}
},
%{
"MNT" => %{
asset: "MNT",
exchange_currencies: ["JPY"],
priced_in: "KHR"
}
}
]
end
def foo do
currencies = currencies()
Enum.map(currencies, fn {_asset, record} -> record end)
end
def bar do
data = %{"USD" => %{asset: "USD", exchange_currencies: ["JPY"], priced_in: "KHR"}}
Enum.map(data, fn {_asset, record} -> record end)
end
end
iex(1)> Sandbox.bar
[%{asset: "USD", exchange_currencies: ["JPY"], priced_in: "KHR"}]
iex(2)> Sandbox.foo
** (FunctionClauseError) no function clause matching in anonymous fn/1 in Sandbox.foo/0
The following arguments were given to anonymous fn/1 in Sandbox.foo/0:
# 1
%{"USD" => %{asset: "USD", exchange_currencies: ["JPY"], priced_in: "KHR"}}
(arbit 0.1.0) lib/Sandbox.ex:122: anonymous fn/1 in Sandbox.foo/0
(elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
Why exactly does Sandbox.foo fail but Sandbox.bar succeed? How shouldSandbox.foo be corrected for it to succeed as well?
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
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
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lpil
foo’s callsEnum.maplist of maps, but the anonymous function pattern matches on the argument as if it were a tuple. The tuple pattern doesn’t match the map, so it crashes.You’ll need to remove the tuple pattern for this not to crash.
Maxximiliann
Thanks for the insight. As refactored,
Sandbox.foodoesn’t seem to be efficient nor idiomatic. What’s a better approach?al2o3cr
currenciesreturns an unusual shape - a list of maps, each with exactly one key. I’d typically expect either a map of currency name → some value, or a list of tuples ready forList.keyfindif the order is important.BUT
I also see that the values in
currenciesdon’t have unique keys ("AUD"appears more than once, for instance). What’s the intent of these separate records? That might suggest a better data structure.lpil
It’s fairly idiomatic and relatively efficient, but you might be surprised by the behaviour.
Maps are not ordered so the one you get from
List.firstwill be semi-random depending on how many keys are in the map. If this is ok (i.e. if the map only has one key) then that is not a problem.Maxximiliann
Thanks for your input
So here’s the workflow:
What data structure(s) would you recommend for this workflow?
al2o3cr
Nothing special here. You could use atoms for currency names, but that makes things more complicated when interacting with other systems.
I’ve bolded the key words that suggest the data structure, a list of maps:
The numbers here are written as floats, but that may not be what you want. Consider the
Decimallibrary for doing financial arithmetic.“Record” could also be satisfied with a
defstruct(or an Erlang record if you’re feeling exotic).Looking up a record is spelled
Enum.find(currencies, & &1.name == "USD"). If the code does this a lot, consider making a map out ofcurrencies:The benefit of using nested maps here is that looking up an exchange rate is spelled
exchange_rates[source_currency][destination_currency].If there is more than one exchange rate for a given currency pair, this approach will not work. Consider using lists for that case.
This results in a new map of maps;
exchanged_values[source_currency][dest_currency]is a map just like the ones incurrencies.That last part about adding initial and final values to the same list seems tricky; how do consumers of
Rknow how to interpret the{name: ..., exchange_rate:..., value:...}results?One option would be to have
Rcontain tuples:This uses
Enum.flat_mapfor two reasons:Maxximiliann
Here’s a crude attempt: https://defuse.ca/b/ZqUSguft
It’s ugly, I know . . .
What improvements do you suggest?