samuelludwig

samuelludwig

Testing functions that return other functions

  • I have a function (myfun/1) that takes in a list of characters, and depending on the characters in the list, returns a list of named functions (that are defined in the same module)–which will be applied at a later time.
Enum.reduce(com_chars, [], fn x, acc ->
  case x do
    "." -> [&to_lowercase/1 | acc]
    "^" -> [&to_uppercase/1 | acc]
    _ -> [:error | acc]
  end
end)
|> Enum.reverse()
  • when I test this function in ExUnit (after importing MyMod) with assert myfun([".", "^"]) == [&to_lowercase/1, &to_uppercase/1], the test fails for the following reasons:
    1. in the test, &to_lowercase/1 and &to_uppercase/1 are expanded to &MyMod.the_function/1
    2. myfun seems to return a list of lambdas and not the named functions: [#Function..., #Function...]
  • I can get around this if I use fully qualified names in myfun, however this does not work if I pass in a function with a set argument, like &get_slice(&1, "[0-5]"). In this case the function shows up as a lambda on both sides of the assert (and not the same lambda either).

My question is: how exactly should I be testing functions that return other functions?

Marked As Solved

dimitarvp

dimitarvp

Function references aren’t compared by value or which function do they point at so you’re out of luck here.

I’d also recommend you don’t return direct function references but rather a structure that describes how to call the function later. A MFA would indeed be perfect as @lucaong said, and they can easily be compared with each other where the same module/function/arguments combo will compare equal to another identical combo.

Also Liked

lucaong

lucaong

On the other hand, tuples like {module, function, arguments} are very idiomatic ways in Elixir (and Erlang) to pass around functions. They even have their own type (mfa, for module function arguments).

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I think perhaps instead of returning a real function you could return an atom that you later use apply with or even just pattern match and call the right function directly. If you need to have values included you could return a tuple of {:function_atom, [list, of, args]}.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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

We're in Beta

About us Mission Statement