Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining Functions within Function in Julia

Tags:

julia

I am wondering if there's any guidance out there about whether defining a function within a function is something recommended or to be avoided in Julia.

I read the Function section in the Julia Docs but did not see this topic mentioned.

A side note is that someone mentioned to me that this is a suggested behavior in Python which is what sparked this question in my mind.

like image 216
logankilpatrick Avatar asked Jan 26 '20 15:01

logankilpatrick


People also ask

How do you use a function in Julia?

Functions in Julia can be combined by composing or piping (chaining) them together. Function composition is when you combine functions together and apply the resulting composition to arguments. You use the function composition operator ( ∘ ) to compose the functions, so (f ∘ g)(args...) is the same as f(g(args...)) .

What is multiple dispatch Julia?

Using all of a function's arguments to choose which method should be invoked, rather than just the first, is known as multiple dispatch.

What is a generic function Julia?

Method Tables Every function in Julia is a generic function. A generic function is conceptually a single function, but consists of many definitions, or methods. The methods of a generic function are stored in a method table. Method tables (type MethodTable ) are associated with TypeName s.

What is Union in Julia?

Type Unions A type union is a special abstract type which includes as objects all instances of any of its argument types, constructed using the special Union keyword: julia> IntOrString = Union{Int,AbstractString} Union{Int64, AbstractString} julia> 1 :: IntOrString 1 julia> "Hello!" :: IntOrString "Hello!"


1 Answers

You usually only define a function inside another function if you want to return a closure. A closure is a function with some data associated to it. When you return a function from a function, the returned (inner) function "captures" the variables that are defined in the same local function scope in which the inner function is defined. Here is an example of the use of closures:

julia> function make_adder(amount)
           function add(x)
               return x + amount
           end
       end;

julia> add_one = make_adder(1);

julia> add_two = make_adder(2);

julia> 10 |> add_one
11

julia> 10 |> add_two
12

Notice that the function returned by make_adder captures the value of amount, which is provided as an argument to make_adder.

It works equally well to return an anonymous function:

julia> function make_adder(amount)
           return x -> x + amount
       end;

julia> add_three = make_adder(3);

julia> 10 |> add_three
13

There can sometimes be performance issues with closures in Julia. If necessary, the performance issues can be addressed with FastClosures.jl.

Here are a couple more examples of closures:

julia> function make_counter()
           x = 0
           return () -> (x = x + 1; x)
       end;

julia> counter = make_counter();

julia> counter()
1

julia> counter()
2

julia> counter()
3
julia> function numerical_derivative(f, dx)
           function dfdx(x)
               (f(x + dx) - f(x)) / dx
           end
       end;

julia> numerical_sine_derivative = numerical_derivative(sin, 0.1);

julia> numerical_sine_derivative(0) # Derivative of sin(x) at 0 equals 1
0.9983341664682815
like image 154
Cameron Bieganek Avatar answered Sep 21 '22 12:09

Cameron Bieganek