Getting started with Julia Multiple Dispatch - julia

Here's what looks to me the simplest imaginable example of multiple dispatch in Julia - it's the entire (8 line) contents of a file called adhoc.jl.
f = function(x::String)
println("Called first version of f")
end
f = function(x::Float64)
println("Called second version of f")
end
f("x")
f(1.0)
and yet when I run that (via include("Adhoc.jl")) julia complains:
ERROR: LoadError: MethodError: no method matching
(::getfield(Main, Symbol("##17#18")))(::String)
With screenshot here
If I change that second instance of f to g things work, but that's no longer making use of multiple dispatch. How come I can't get to first base with multiple dispatch?

This is the corrected version:
function f(x::String)
println("Called first version of f")
end
function f(x::Float64)
println("Called second version of f")
end
f("x")
f(1.0)
The problem with your code is that your original code created an anonymous function and assigned it to a variable f. And you did it twice, thus f pointed only at function(x::Float64).
You can see the problem with your original code by running it in Julia REPL:
julia> f = function(x::String)
println("Called first version of f")
end
#3 (generic function with 1 method)
julia> f = function(x::Float64)
println("Called second version of f")
end
#5 (generic function with 1 method)
julia> methods(f)
# 1 method for generic function "#5":
[1] (::getfield(Main, Symbol("##5#6")))(x::Float64) in Main at REPL[2]:2
and you see that f points at an anonymous function which has only one method.
Running my code (you need to restart Julia REPL as f variable name will be already taken and it cannot be reassigned):
julia> function f(x::String)
println("Called first version of f")
end
f (generic function with 1 method)
julia> function f(x::Float64)
println("Called second version of f")
end
f (generic function with 2 methods)
julia> f("x")
Called first version of f
julia> f(1.0)
Called second version of f
julia> methods(f)
# 2 methods for generic function "f":
[1] f(x::Float64) in Main at REPL[2]:2
[2] f(x::String) in Main at REPL[1]:2

Related

Most idiomatic solution to function currying in Julia

What is the most idiomatic way to achieve function currying?
Eg. in Haskell:
times a b = a * b
-- This can then be used uncurried:
times 2 3 -- Result is 6
-- But there is also auto-currying:
(times 2) 3 -- This works too
In Julia, some built-ins support this:
<(8, 7) # Result is false
<(8)(7) # Same
7 |> <(8) # Same
However, user-defined functions don't automatically have this functionality:
times(a, b) = a * b
times(2, 3) # Result is 6
3 |> times(2) # MethodError: no method matching times(::Int64)
I can manually define a one-argument version and then it works:
times(a) = b -> a * b
But my question is, is there a better way?
why not use curry.jl
times(a, b) = a * b
times_curry = curry(times)
times_curry(5)(2) ---> gives 10
This would be pretty tricky (impossible?) to solve in Julia without a macro, as multiple dispatch means you won't know which function you'll ultimately end up dispatching to until you see all of the arguments. I think the simplest way to implement currying is just with a struct like this:
julia> struct Curry
func::Function
args::Tuple
Curry(func, args...) = new(func, args)
end
julia> (curry::Curry)(x, args...) = Curry(curry.func, curry.args..., x, args...)
julia> (curry::Curry)() = curry.func(curry.args...)
julia> Curry(<)
Curry(<, ())
julia> Curry(<)(2)()
(::Base.Fix2{typeof(<), Int64}) (generic function with 1 method)
julia> Curry(<)(2)(3)()
true
julia> Curry(<, 2)
Curry(<, (2,))
julia> Curry(<, 2, 3)()
true
Basically, calling the Curry with at least one argument creates a new Curry with the new arguments, and calling it with 0 arguments “executes” the whole thing.

How to alias composite function with keyword arguments?

I can alias a single function like so:
julia> f(y;x=1) = x * y
f (generic function with 2 methods)
julia> const g = f
f (generic function with 1 method)
julia> g(3,x=2)
6
But if I try to do that with a composite function, the kwarg causes trouble:
julia> const gg= sqrt ∘ f
#62 (generic function with 1 method)
julia> gg(3,x=2)
ERROR: MethodError: no method matching (::Base.var"#62#63"{typeof(sqrt),typeof(f)})(::Int64; x=2)
Closest candidates are:
#62(::Any...) at operators.jl:875 got unsupported keyword argument "x"
Is there a solution to this? I am trying to pass the arguments to f and then transform that result via a shortcut (gg in the above MWE).
You have the following definition:
∘(f, g) = (x...)->f(g(x...))
and as you can see it does not support keyword arguments. Probably you can open an issue to discuss allowing them.
For now you can define your own version of it like this:
julia> ⋄(f, g) = (x...; kw...)->f(g(x...; kw...))
⋄ (generic function with 1 method)
julia> f(y;x=1) = x * y
f (generic function with 1 method)
julia> const gg = sqrt ⋄ f
#1 (generic function with 1 method)
julia> gg(3,x=2)
2.449489742783178
EDIT:
I have switched to the other OS, and I see that ⋄ does not render very sharp here. I used the following symbol:
help?> ⋄
"⋄" can be typed by \diamond<tab>
which is defined to be allowed to be an infix operator and is relatively easy to remember while not overshadowing ∘.

Deflation Method for Multiple Root Finding

I'm trying to implement the deflation method for finding multiple roots of a polynomial on Julia. In the deflation method, new functions are generated from the actual function divided by x - x_roots[i], and the root of new generated function must be found. But g(x) = g(x) / (x - x_root) gives me a Stack Overflow error, as probably it generated an infinite recursive relation. How may I generate a new function in each step?
function deflation(f::Function, order)
roots=[]
n=1
g(x)=f(x)
x_root=Muller_method(f,-5,0,5,1e-5,50)
while n<=order
x_root=Muller_method(a,-5,0,5,1e-5,50)
g(x)=g(x)/(x-x_root)
append!(roots,x_root)
n=n+1
end
return (roots)
Something like this causes infinite recursion:
julia> g(x) = x
g (generic function with 1 method)
julia> g(1)
1
julia> g(x) = g(x) / 2
g (generic function with 1 method)
julia> g(1)
ERROR: StackOverflowError:
Stacktrace:
[1] g(::Int64) at ./REPL[3]:1 (repeats 79984 times)
This is because function (or method) definitions do not work like variable assignment: each re-definition of g(x) overwrites the previous one (note how g above only ever has one method). When a method definition refers to itself, it is recursion, i.e. it refers to its own version at the time when the function gets called.
As for your deflation method, perhaps you could define a new function that closes over the vector of currently found roots. Consider the following example to see how things would work:
julia> f(x) = (x-1) * (x-2)
f (generic function with 1 method)
julia> roots = Float64[]
Float64[]
# g is defined once, and accounts for all currently found roots
julia> g(x) = f(x) / reduce(*, x-root for root in roots; init=one(x))
g (generic function with 1 method)
# No roots are identified at the beginning
julia> g(1+eps())
-2.2204460492503126e-16
julia> g(2+eps())
0.0
# But the results produced by g update as roots are identified
julia> push!(roots, 1.)
1-element Array{Float64,1}:
1.0
julia> g(1+eps())
-0.9999999999999998
julia> g(2+eps())
0.0

Access function defined in another function

Is it possible to access function defined in another function in julia? For example:
julia> function f(x)
function g(x)
x^2
end
x * g(x)
end
f (generic function with 1 method)
julia> f(2)
8
julia> f.g(2)
ERROR: type #f has no field g
in eval_user_input(::Any, ::Base.REPL.REPLBackend) at ./REPL.jl:64
in macro expansion at ./REPL.jl:95 [inlined]
in (::Base.REPL.##3#4{Base.REPL.REPLBackend})() at ./event.jl:68
No. In julia, it is often more ideomatic to use a module for local functions
module F
function g(x)
x^2
end
function f(x)
x * g(x)
end
export f
end
using F
f(2)
F.g(2)
What's the use case? You can define a custom type, give it a function field, and then make the type callable (a closure) to achieve the behaviour you want. But whether that is the best way of solving your issue in julia is a different question.
You can do this if your function f is an instance of a callable type, containing a function g as an accessible field:
julia> type F
g::Function
end
julia> function(p::F)(x) # make type F a callable type
x * p.g(x)
end
julia> f = F(function (x) return x.^2 end) # initialise with a function
F(#1)
julia> f(2)
8
julia> f.g
(::#1) (generic function with 1 method)
If g is always a fixed function, then you can introduce it via an internal constructor.
But to echo Lyndon's comments above, a better question is, why would you do this instead of something relying more on julia's dynamic dispatch features?

How do I create a method that takes any iterable collection of strings?

I have a function, f. I want to add a method that takes any container of Strings. For example, I want to write a method that generates the following when needed:
f(xs::Array{String, 1}) = ...
f(xs::DataArray{String, 1}) = ...
f(xs::ITERABLE{String}) = ...
Is this possible to do in Julia's type system? Right now, I'm using a macro to write a specialized method when I need it.
#make_f(Array{String, 1})
#make_f(DataArray{String, 1})
This keeps things DRY, but it feels...wrong.
Can't you just use duck typing? I.e., just assume that you're feeding the function an object of the right type and throw an error if at some point e.g. you don't have a string in your iterable.
This should improve once you can really talk about iterables using traits; currently there is no iterable type. Scott's answer, for example, will not work with a tuple of strings, even though that is iterable.
E.g.
julia> f(x) = string(x...) # just concatenate the strings
f (generic function with 1 method)
julia> f(("a", "á"))
"aá"
julia> f(["a", "á"])
"aá"
julia> f(["a" "b"; "c" "d"]) # a matrix of strings!
"acbd"
At least in Julia 0.4, the following should work:
julia> abstract Iterable{T} <: AbstractVector{T}
julia> f{T<:Union{Vector{String},Iterable{String}}}(xs::T) = 1
f (generic function with 1 method)
julia> x = String["a", "é"]
2-element Array{AbstractString,1}:
"a"
"é"
julia> f(x)
1

Resources