routine to generate a 2d array from two 1d arrays and a function

Posted by intuited on Stack Overflow See other posts from Stack Overflow or by intuited
Published on 2010-04-05T22:41:35Z Indexed on 2010/04/05 22:43 UTC
Read the original article Hit count: 211

I'm guessing that there's a word for this concept, and that it's available in at least some popular languages, but my perfunctory search was fruitless.

A pseudocode example of what I'd like to do:

function foo(a, b) {
  return a * b  // EG
}

a = [ 1, 2, 3 ]
b = [ 4, 5, 6 ]
matrix = the_function_for_which_I_search(foo, [a, b] )
print matrix
=> [ [ 4, 8, 12], [5, 10, 15], [6, 12, 18] ]

// or
function concatenate(a,b)
  return a.b
}
print the_function_for_which_I_search( concatenate, [ a, b ])
=> [ [ '14', '24', '34'], ['15', '25', '35'], [16', '26', '36'] ]

In other words, function_for_which_I_search will apply the function given as its first argument to each combination of the elements of the two arrays passed as its second argument, and return the results as a two-dimensional array.

I would like to know if such a routine has a common name, and if it's available in a python module, cpan package, ruby gem, pear package, etc. I'm also wondering if this is a core function in other languages, maybe haskell or R?

© Stack Overflow or respective owner

Related posts about multidimensional-array

Related posts about arrays