Redactado por: Felipe Juarez

Before we pass to how we can make a FizzBuzz game, we need to know something more basic about Elixir. Today we are going to learn something called Anonymous Function (a.k.a Lambda Expression)

An Anonymous Function is a function without a name and is usually used for applying some sort of transformation to the arguments passed.

According to Wikipedia an Anonymous Function is:

A function definition that is not bound to an identifier. Anonymous functions are often

  • Arguments being passed to higher-order functions, or
  • Used for constructing the result of a higher-order function that needs to return a function.

So, in elixir the way that we can create an Anonymous Function is with the fn keyword:

  argument -> body
  argument -> body

An easy example of an Anonymous Function is a multiplication of a number by 5.

iex(1)> times_5 = fn(a) -> a * 5 end
#Function<6.52032458/1 in :erl_eval.expr/5>
iex(1)> times_5.(3)

The line (times_5 = fn(a) -> a * 5 end) creates a function that takes one parameter and the implementation of the function is after the -> and is terminated by the end keyword.

And the other line is the execution of the function using a dot . between the variable(let’s call it that way) and the parenthesis.

Keep in mind a variable assigned inside a function does not affect its surrounding environment.

So given this explanation we can make a little game called FizzBuzz. If you never heard about it this is a brief description:

  • Any number divisible by three is replaced by the word Fizz
  • Any number divisible by five is replaced by the word Buzz.
  • Any number divisible by both become FizzBuzz.

For example

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz

With our example we are going to make use of the Anonymous Functions that we already explain above.

iex(1)> fizz_buzz = fn
...(1)>   0, 0, _ -> "FizzBuzz"
...(1)>   0, _, _ -> "Fizz"
...(1)>   _, 0, _ -> "Buzz"
...(1)>   _, _, c -> c
...(1)> end
#Function<18.52032458/3 in :erl_eval.expr/5>
iex(2)> number_to_eval = fn
...(2)>   n -> fizz_buzz.(rem(n, 3), rem(n, 5), n)
...(2)> end
#Function<6.52032458/1 in :erl_eval.expr/5>
iex(3)> 1..16 |> Enum.to_list |>
[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14,
 "FizzBuzz", 16]

The number_to_eval is an Anonymous Function that wrap the implementation of another Anonymous Function fizz_buzz. number_to_eval is going to take one argument and pass the remnant of that number divided by 3 and 5 and the number itself to the fizz_buzz function. In the fizz_buzz function we are going to make use of the patter match.

  • If the remnant by 3 and 5is 0 we are going to return FizzBuzz.
  • If the remnant by 3 is 0 we are going to return Fizz.
  • If the remnant by 5 is 0 we are going to return Buzz.
  • If doesn’t match to any of above we are going to return the same number.

And in the next lines we are going to eval sixteen numbers.

In this example we can make a good use for Anonymous Functions and how we can apply on a module. This is brief and of course you can make a good use of it in another situations but is a little beyond the scope of this post.

So I hope you enjoy this can help you to make something else and for me that’s all. Good Luck, Have Fun and GG!

comments powered by Disqus