What Is Elixir Closure — Tentamen Software Testing Blog

Image for post
Image for post
Elixir Closure

TL;DR

Closure

In the above screenshot, you can see our iEx session with closures. We first defined variable tax with value for the Croatian price tax. Then we defined lambda closure that references tax value 0.25. When we call variable tax_in_dubrovnik with 50 hrk, we get a tax value of 12.5 hrk.

Then we rebind the tax variable to value 0.20, and we check closure immutability property by calling again tax_in_dubrovnik that returns again 12.5. We create new closure around tax value 0.20, tax_in_london and we get the amount of 10. We call closure again tax_in_dubrovnik and confirm that it is still a closure around the tax value of 0.25

Remember

  • reference memory location, not variable itself

Originally published at https://blog.tentamen.eu on September 19, 2020.

Written by

Founder of Tentamen, software testing agency.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store