Sunday, 28 February 2016

Julia: Anonymous functions

Function without name is called anonymous function.

Syntax
() -> expression    (zero argument anonymous function)
var -> expression  (One argument anonymous function) 
(var1,var2,var3) -> expression (Multi argument anonymous function)

julia> ()->12
(anonymous function)

julia> x->x^2+2x+10
(anonymous function)

julia> (x,y,z)->2x+3y+5z
(anonymous function)

Where can I use anonymous functions
In Julia, you can pass a function as an argument to other function. For example, you can pass a function as an argument to map function.

julia> map((x) -> x^2, [1, 2, 3, 4, 5])
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

julia> map((x) -> x^3, [1, 2, 3, 4, 5])
5-element Array{Int64,1}:
   1
   8
  27
  64
 125



Previous                                                 Next                                                 Home

No comments:

Post a Comment