baxterw3b

baxterw3b

Anonymous functions with multiple body

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 multiple body.

Basically if i have a function like this

handle_result = fn
{:ok, result} → IO.puts “handling the result…”
{:error} → IO.puts “an error is occurred”
end

and then

some_result = 1
handle_result.({:ok, some_result})

“handling the result…”

to me is ok, i pass a tuple and if match i return the string.

The point is that to me is like i’m using a switch statement, or a sort of case statement, and if the value that i pass match a case, i print the string, so what’s the difference to use a function like this or
use a switch in other languages? and another thing is, how can i use multiple bodies to compare if two values are equal or not equal with operators like && == || using multiple bodies? cause everytime i try it cause me errors.

last thing that i don’t get is, the argument of the function is implicit? because usually if i do

fn a,b →
a == b
end

is clear, i pass two values and then i compare a to b, but i don’t get it this thing of multiple bodies with the tuples and pattern matching.

sorry guys, but i’m a lot confuse. :slight_smile:

thanks.

Most Liked

tyro

tyro

The point is that to me is like i’m using a switch statement, or a sort of case statement, and if the value that i pass match a case, i print the string, so what’s the difference to use a function like this or
use a switch in other languages?

Function clauses perform the control flow of switch statements (i.e. they allow branching on conditions) but they also bind values to variables at the same time. e.g. In the second line of:

handle_result = fn
  {:ok, result} -> IO.puts "handling the result..."
  {:error} -> IO.puts "an error is occurred"
end

a condition is checked (is the argument of the form {:ok, result}?), AND, if the condition is true, a value is bound to the variable ‘result’. A switch case does not assign values to variables but pattern matching does.

how can i use multiple bodies to compare if two values are equal or not equal with operators

You can use ‘when’ to add conditions to clauses:

fn 
  a,b when a > b -> ...
  a,b when a < b -> ...
  a,b -> 
end

last thing that i don’t get is, the argument of the function is implicit?

Not exactly. The arguments are pattern matched with each function clause. So for your example

fn a,b -> #  This is equivalent to fn a = arg1, b = arg2 ->
  a == b
end
Qqwy

Qqwy

TypeCheck Core Team

Let me try to answer your questions:

Using multiple function heads is ‘sort of’ the same thing as a switch-case, but more powerful.
In languages like C and C++, you are only allowed to use int-like values in a switch case, as these can be optimized by the compiler to a direct jump at best, or a binary search at worst. Therefore, things like strings are not allowed.

In most imperative or object-oriented interpreted languages, however, this optimization does not happen. You are allowed to use any value in a switch statement, but this is treated as just syntactic sugar for an if ()... else if () ... else if ()...-chain.

In languages like Elixir that support pattern-matching, however, the case statement is a lot more powerful than a plain 'ol switch: The compiler will recognize what properties you are trying to ascertain of your data types, and underwater write the appropriate checks that only check what needs to be checked; redundant checks are not performed.
While this is of course marginally less fast than the ‘direct jump’ approach that C/C++ support, it is a lot faster than a simple if... else if-chain. And on the other hand, it makes for extremely readable code.

The compiler will take function with multiple heads (it does not matter if it is a named or anonymous function, it works the same way), and rewrite these multiple heads using a case-statement underwater. So yes, in this way, they are doing the same thing.

But from a code organization perspective, it usually is better to separate your functionality in many small named functions, because it makes it easier in the future to change only a small part.

In the case that a pattern is matched in a function head or in a case statement, only the variables that are part of that statement are available (also available are the ones before the function or case statement, but notably not the ones named in the other functin heads / case matches).

EDIT: @tyro was a little bit faster than me :smiley: , but I hope you still find my background explanation helpful or at least entertaining :stuck_out_tongue_winking_eye: .

baxterw3b

baxterw3b

Thank you guys! Now i understand everything! And Thank you for Your time

voger

voger

You need to use guard clauses. Here is an example:
http://elixirplayground.com?gist=58b573a1ae2de40bd468e78ccac38e96

Function #1 always matches 100, 99. So whenever you call PositivePrinter.print_positive_diference(100, 99) that is the function that will be called. No guard clauses here because they are not needed.
Functions #2 match only if the expression after the when clause evaluates to true.
Function #3 matches whatever as long it is called with 2 parameters.

The commented function will rise an error because we call it with one parameter but we haven’t define any function that it is called with one parameter. So none of our functions matches

Here is an example with maps:
http://elixirplayground.com?gist=c9060fb9b54bfd00053c6c50e75c2138

Functions at #1 match the map only if it has a key day with the appropriate value
Function at #2 matches the map if it has a key day with any value. At the same time it binds the value with the variable the_day so it can be used inside the function. Although the function head matches any value, we use a guard in it, so valid values are only :saturday and :sunday. This reads: match any map that has key :day with any value as long as the value is one of :saturday, :sunday

Function at #3 matches whatever value you throw at it. Even if today = 42

Where Next?

Popular in Questions 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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
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 45766 226
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement