2

Problem: How to map a function onto a list. My (example input:

mylist = {{1, 2}, {3, 4}}
gg[x_, y_] := {N[x + y], N[x y]}
Map[gg, mylist]

Expected output:

{{3., 2.}, {7., 12.}}

Actual output:

{gg[{1, 2}], gg[{3, 4}]}

How do I get gg[.] to give a numerical (function evaluated) output of mylist?

mark r
  • 61
  • 1

2 Answers2

3

As mentioned in the comments by Kuba, you should use

gg @@@ mylist

Indeed, this is precisely raison d'être of @@@. If you want to use Map, then Kuba suggests to use the operator form of @@, to wit,

Apply[gg] /@ mylist

Finally, let me mention that if you insist on using Map, then you should redefine the function gg into (hat tip to kglr)

gg[{x__}] := {N[+x], N[1 x]}

If you do so, then Map[gg, mylist] yields your expected output.

1
mylist = {{1, 2}, {3, 4}}
gg[x_, y_] := {N[x + y], N[x y]}
Map[gg[#[[1]],#[[2]]]&, mylist]
Vink
  • 151
  • 1
  • 6