1
$\begingroup$

(Edited to provide an example)

Recently I have found myself writing silly functions like the following, which is just a new arity of Subtract in which the arguments are inside a list.

subtract[{a_, b_}] := Subtract[a, b]

If I were adding that variant to one of my own functions, I would use the same name, and would feel more comfortable about it.

I feel sure I should not need to do this for system functions, yet these variants are very convenient when I am working with lists.

There is probably some elementary principle of Mathematica coding that I have failed to absorb. What could it be?

Example:

pairsOfPositions = {{{a, b, c}, {d, e, f}}, {{g, h, i}, {j, k, l}}, {{m, n, o}, {p, q, r}}}; differences = Map[subtract, pairsOfPositions, {1}];

where the desired result for differences is { {a - d, b - e, c - f}, {g - j, h - k, i - l}, {m - p, n - q, o - r} }

$\endgroup$
8
  • 5
    $\begingroup$ Apply? $\endgroup$
    – Michael E2
    Aug 3, 2015 at 1:22
  • 1
    $\begingroup$ Proposed duplicates: (10833), (46238) $\endgroup$
    – Mr.Wizard
    Aug 3, 2015 at 2:10
  • $\begingroup$ My question is supposed to be about items embedded in a list. Maybe Apply would help, but I don't yet understand how. $\endgroup$ Aug 3, 2015 at 2:28
  • $\begingroup$ @RalphDratman does not Subtract @@@ pairsOfPositions give the output you want? If so you just need to take the time to understand Apply. If this is not the result you want or you chose a narrow example please edit the question to explain. $\endgroup$
    – Mr.Wizard
    Aug 3, 2015 at 2:30
  • $\begingroup$ Yes, thank you Sir Wizard, Subtract @@@ pairsOfPositions does work. Thank you so much. I will as you suggested look more deeply into Apply. $\endgroup$ Aug 3, 2015 at 2:55

1 Answer 1

2
$\begingroup$
Subtract @@ {a, b}
(* a - b *)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.