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). Firstly, functions have their own type: 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 REPL: +(3,4) returns 7 and typeof(+) ...

Get Julia 1.0 Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.