Maxximiliann

Maxximiliann

(FunctionClauseError) no function clause matching in anonymous fn/1

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?

Marked As Solved

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

Also Liked

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.

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

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement