Option 5 – Arrays of type S and type T where S and T are subtypes of Number

Since Julia arrays support broadcasting, we can generalize the function arguments from a Vector{T} to an Array{T,N} signature in order to support multidimensional arrays. Let's now define the function as follows:

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

We have pretty good confidence that this would work. Let's test it now:

Fabulous! We have finally satisfied all the requirements as listed in the test scenarios. Are we done? Maybe not. For the sake of argument, we may want to support other types of containers that are not necessarily a dense array. What if the input is sparse matrices? Let's improve the function once again.

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

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