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
Also Liked
kokolegorille
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
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
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 ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










