DivM
DivM

Reputation: 137

Run Julia function every time Julia environment launches

I am moving from R, and I use the head() function a lot. I couldn't find a similar method in Julia, so I wrote one for Julia Arrays. There are couple other R functions that I'm porting to Julia as well.

I need these methods to be available for use in every Julia instance that launches, whether through IJulia or through command line. Is there a "startup script" of sorts for Julia? How can I achieve this?

PS: In case someone else is interested, this is what I wrote. A lot needs to be done for general-purpose use, but it does what I need it to for now.

function head(obj::Array; nrows=5, ncols=size(obj)[2])
     if (size(obj)[1] < nrows)
       println("WARNING: nrows is greater than actual number of rows in the obj Array.")
       nrows = size(obj)[1]
     end
     obj[[1:nrows], [1:ncols]]
   end

Upvotes: 3

Views: 472

Answers (1)

IainDunning
IainDunning

Reputation: 11644

You can make a ~/.juliarc.jl file, see the Getting Started section of the manual.

As for you head function, here is how I'd do it:

function head(obj::Array; nrows=5, ncols=size(obj,2))
    if size(obj,1) < nrows
        warn("nrows is greater than actual number of rows in the obj Array.")
        nrows = size(obj,1)
    end
    obj[1:nrows, 1:ncols]
end

Upvotes: 7

Related Questions