merch
merch

Reputation: 945

Julia: methods and DataArrays.DataArray

I would like to write a function fun1 with a DataArrays.DataArray y as unique argument. y can be either an integer or a float (in vector or in matrix form).

I have tried to follow the suggestions I have found in stackoverflow (Functions that take DataArrays and Arrays as arguments in Julia) and in the official documentation (http://docs.julialang.org/en/release-0.5/manual/methods/). However, I couldn't write a code enought flexible to deal with the uncertainty around y.

I would like to have something like (but capable of handling numerical DataArrays.DataArray):

function fun1(y::Number)
   println(y);
end

Any suggestion?

Upvotes: 0

Views: 70

Answers (1)

Dan Getz
Dan Getz

Reputation: 18217

One options can be to define:

fun1{T<:Number}(yvec::DataArray{T}) = foreach(println,yvec)

Then,

using DataArrays
v = DataArray(rand(10))
w = DataArray(rand(1:10,10))
fun1(v)
#
# elements of v printed as Flaot64s
#
fun1(w)
#
# elements of w printed as Ints
#

A delicate but recurring point to note is the invariance of Julia parametric types which necessitate defining a parametric function. A look at the documentation regarding types should clarify this concept (http://docs.julialang.org/en/release-0.4/manual/types/#types).

Upvotes: 1

Related Questions