Option 6 – Abstract arrays

The AbstractArray is the abstract type for all Julia array containers. Many Julia packages implement the array interface and are made to be subtypes of AbstractArray. It would be a shame if we go so far to make the sumprod function versatile enough, and yet we cannot support sparse matrices or other types of array-type containers. To make it more general, let's turn our function definition from Array to AbstractArray as follows:

sumprod_6(A::AbstractArray{S,N}, B::AbstractArray{T,N}) where 
{N, S <: Number, T <: Number} = sum(A .* B)

The signature is the same as the previous option, except that the function can be dispatched with any AbstractArray container types. Let's make sure that the function works as expected:

The function is working properly for our existing cases. Let's just try it once again using the sparse matrix type:

Bravo! It is working great now, even with a non-dense array type. We are almost done. Let's look at our last option—duck typing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.93.136