NoviceStat
NoviceStat

Reputation: 135

Julia: Passing arguments for a function into a function

I was wondering if Julia has an easily already built in capability to pass arguments meant for a function in a function?

For example,

I'm working with Gadfly but I want to create a function that makes a specific plot, let's say one that does a line graph with the plots pointed already.

So for a working example

using Gadfly, Random
Random.seed!(50)
x = randn(10)
y = 10 * x .+ 2 .+ randn(10)/10
function dummy1(x, y; plotOptionsToPass...)
    plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
end

And I want to be able to pass in all different types of Gadfly plot options such as

dummy1(x, y; Theme(panel_fill = nothing))

so that it makes the dummy1 function turn into something like

plot(x = x, y = y, Geom.point, Geom.line; Theme(panel_fill = nothing))

without me having to actually prespecify all of the types of options Gadfly allows plot() to take.

Upvotes: 2

Views: 255

Answers (1)

Evgeny
Evgeny

Reputation: 4551

Not sure what you are after, but maybe it helps to see that you can define a new function inside dummy1 and return it. The retruned fucntion will use less arguments. dummy1 becomes a drawing function 'constructor'.

function dummy1(;plotOptionsToPass...)
    function foo(x, y) 
        plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
    end 
    return foo
end 

# create new drawing function
new_artist = dummy1(Theme(panel_fill = nothing))
# draw something
new_artist(x, y) 

Upvotes: 1

Related Questions