Maxximiliann

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?

Showing Posts 1 to 7

lpil

lpil

Creator of Gleam

foo’s calls Enum.map list 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.

  def foo do
    currencies = currencies()

    Enum.map(currencies, fn map -> do_something_with(map) end)
  end
Maxximiliann

Maxximiliann OP

  def foo do
    currencies = currencies()

    Enum.map(currencies,
      fn record
      ->
        asset =
        Map.keys(record)
        |> List.first

        Map.get(record, asset)
        |> Map.get(:exchange_currencies)
     end)
  end
iex(1)> Sandbox.foo
[
  ["JPY"],
  ["JPY"],
  ["KHR", "JPY"],
  ["JPY"],
  ["KHR", "USD", "JPY", "ZAR"],
  ["KHR", "JPY"],
  ["KHR", "KRW", "LAK", "MGA", "XOF", "USD", "JPY", "AUD", "EUR"],
  ["JPY"],
  ["KHR", "USD", "JPY", "ZAR"],
  ["JPY", "ZAR"],
  ["KHR", "USD", "ZAR"],
  ["KHR", "USD", "JPY", "ZAR"],
  ["KHR", "JPY"],
  ["KHR", "KRW", "USD", "JPY", "ZAR"],
  ["LAK", "ZAR"],
  ["JPY"]
]

Thanks for the insight. As refactored, Sandbox.foo doesn’t seem to be efficient nor idiomatic. What’s a better approach?

al2o3cr

al2o3cr

currencies returns 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 for List.keyfind if the order is important.

BUT

I also see that the values in currencies don’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

lpil

Creator of Gleam

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.first will 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

Maxximiliann OP

Thanks for your input :slight_smile:

So here’s the workflow:

  1. Compile a list of currencies ( C ).
  2. For each currency in C create a record consisting of the following key/value pairs: currency name, exchange rate into some universal currency, its value in that universal currency. Aggregate these results into a new list (V).
  3. For each currency in V compile a list of currencies it can be exchanged into along with their exchange rates. (E)
  4. For each currency in E create a record with the following key/value pairs: its name, its exchange rate into the universal currency used in C, its value in terms of that universal currency. Aggregate these results into a new list F.
  5. Compare the value of each currency in V (initial_value) against the value of each of its corresponding exchange currencies in F (final_value). When final_value is greater than initial_value, add initial_value and final_value records to a new list R.

What data structure(s) would you recommend for this workflow?

al2o3cr

al2o3cr

Nothing special here. You could use atoms for currency names, but that makes things more complicated when interacting with other systems.

all_currencies = ["AUD", "CVE", "USD"]

I’ve bolded the key words that suggest the data structure, a list of maps:

currencies = [
    %{name: "USD", exchange_rate: 1.23, value: 0.87},
    %{name: "CVE", exchange_rate: 1.75, value: 0.75},
    %{name: "AUD", exchange_rate: 1.01, value: 0.99}
  ]

The numbers here are written as floats, but that may not be what you want. Consider the Decimal library 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 of currencies:

currencies_map = Map.new(currencies, fn v -> {v.name, v} end)
  exchange_rates = %{
    "AUD" => %{"USD" => 1.14, "CVN" => 1.2},
    # OR, if an exchange rate is more complicated than just a number
    "USD" => %{"CVN" => %{rate: 0.67, quote_id: 1234}, "AUD" => %{rate: 1.06, quote_id: 4567}}
  }

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.

exchanged_values =
  exchange_rates
  |> Map.new(fn {src_currency, dest_currencies} ->
    {
      src_currency,
      Map.new(dest_currencies, fn {dest_currency, exchange_rate} ->
        # do something with src_currency, dst_currency, and exchange_rate
        # probably look things up in currencies
        result = %{
          name: dst_currency, # or "#{src_currency} -> #{dst_currency}"
          exchange_rate: ... # calculate this
          value: ... # and this
        }

        {dest_currency, result}
      end
    }
  end

This results in a new map of maps; exchanged_values[source_currency][dest_currency] is a map just like the ones in currencies.

That last part about adding initial and final values to the same list seems tricky; how do consumers of R know how to interpret the {name: ..., exchange_rate:..., value:...} results?

One option would be to have R contain tuples:

exchanged_values
|> Enum.flat_map(fn {src_currency, dest_currencies} ->
  dest_currencies
  |> Enum.map(fn {dest_currency, final_value} ->
    # get initial_value for dest_currency from currencies
    {initial_value, final_value}
  end)
  |> Enum.filter(fn {initial_value, final_value} ->
    # decide if record should be included or not
  end)
end)

This uses Enum.flat_map for two reasons:

  • we’re building a list, but a single key of the input may return many values
  • some keys in the input may return NO values
Maxximiliann

Maxximiliann OP

Here’s a crude attempt: https://defuse.ca/b/ZqUSguft

It’s ugly, I know . . .

What improvements do you suggest?

— All posts loaded —

Where Next? Top

Trending in Questions Top

itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
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
KristerV
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
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews