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

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
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
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
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
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement