crisefd

crisefd

Can't use function calls in pattern matching

Given

f = fn -> 2 end
g = fn -> 2 end

How can I match the result of g with f’s ?
If I do it like this, it fails because function calls are not allowed in pattern matching.

case g.() do
  f.() -> 1
  4 -> 2,
   5 -> 3,
  _ -> 6
end

Note:
This is dummy version of what I need to do in my project. Basically I need to pattern match the result of a function with the result of other functions. But don’t know how to go about it.

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
f_result = f.()

case g.() do
  ^f_result -> 1
  4 -> 2,
   5 -> 3,
  _ -> 6
end

Also Liked

kokolegorille

kokolegorille

What about this?

case {f.(), g.()} do
  {x, y} -> #whatever
end
NobbZ

NobbZ

a = fast_op()
b = long_running_thing()

case c do
  ^a -> 1
  ^b -> 2
end

Is very explicit that no matching would happen before both calls have returned, whereas the “sugared” version you suggest might hide this fact and a naive reader would assume that long_running_thing() never gets called when cmatches the result of fast_op().

OvermindDL1

OvermindDL1

Matches are run in linear time, they can’t allow for arbitrary function calling or anything of the sort as they get compiled to a fairly efficient jumplist (sometimes).

Last Post!

silverdr

silverdr

That’s exactly what I was looking for as a replacement for the

which keeps getting over-verbose especially when more than two options are at play :slight_smile:

Where Next?

Popular in Questions Top

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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
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
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

Other popular topics 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 records...
New
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
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
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New