Is there a built-in or more efficient approach than using Partition to apply a binary operation to adjacent pairs of elements? That is, is it reasonable to be using the following for a binary operation f and a possibly long list (here, just {a,b,c}) the following approach?

Map[f @@ # &, Partition[{a, b, c}, 2, 1]]

Related: will the result of this use of Partition ever actually be constructed in full, or are the pairs just generated on demand? (And how can I anticipate the answer to this question?)

  • $\begingroup$ something like this? f @@ list[[# ;; # + 1]] & /@ Range[Length[list] - 1] $\endgroup$ – george2079 Nov 6 '15 at 20:30
  • 1
    $\begingroup$ l = {a, b, c}; Inner[f, Most@l, Rest@l, List] $\endgroup$ – Dr. belisarius Nov 6 '15 at 20:30
  • $\begingroup$ @belisariushassettled If I'm willing to risk the construciton of Most and Rest, I can just use Thread. But that gets me back to my question of how I can know when such things will be constructed. $\endgroup$ – Alan Nov 6 '15 at 21:08
  • $\begingroup$ You can verify how Mathematica is calculating (more or less) by Map[f @@ # &, Partition[{a, b, c}, 2, 1]] // Trace $\endgroup$ – Dr. belisarius Nov 6 '15 at 21:22
  • 1
    $\begingroup$ Regarding the second question, Map has no Hold* attributes so the full Partition will be evaluated before Map sees it. The pairs are not generated on demand. $\endgroup$ – Simon Woods Nov 6 '15 at 21:23

For a packed numerical list it is both faster and less memory intensive to use ListConvolve:

list = RandomReal[{0, 1}, 10^7];
f = Plus;

  res1 = Map[f @@ # &, Partition[list, 2, 1]]]
(* {2.37724, 320005544} *)

  res2 = ListConvolve[{1, 1}, list, {-1, 1}, {}, Times, f]]
(* {0.290708, 80006000} *)

res1 == res2
(* True *)
  • $\begingroup$ While comparable in terms of memory compared to ListConvolve, Developer`PartitionMap takes about twice as long as Map on my machine. $\endgroup$ – rcollyer Nov 6 '15 at 22:03
  • $\begingroup$ Wonderful! PartitionMap is exactly what I was looking for. Presumably it will get faster ... But I'm also very happy to learn about this use of ListConvolve. I confess I had hopefully skimmed the documentation for it without discovering how to do this. $\endgroup$ – Alan Nov 7 '15 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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