First-class functions and closures

In this section, we will demonstrate the power and flexibility of functions (example code can be found in chapter 3\first_class.jl). First, functions have their own type: typing typeof(mult) in the REPL returns Function. Functions can also be assigned to a variable by their name:

julia> m = mult 
julia> m(6, 6) #> 36.

This is useful when working with anonymous functions, such as c = x -> x + 2, or as follows:

julia> plustwo = function (x)
                     x + 2
                 end
(anonymous function)
julia> plustwo(3)
5

Operators are just functions written with their arguments in an infix form, for example, x + y is equivalent to +(x, y). In fact, the first form is parsed to the second form when it is evaluated. We can confirm it in the ...

Get Getting Started with Julia now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.