apply binary operation to all adjacent pairs

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?)

• something like this? f @@ list[[# ;; # + 1]] & /@ Range[Length[list] - 1] Nov 6, 2015 at 20:30
• l = {a, b, c}; Inner[f, Most@l, Rest@l, List] Nov 6, 2015 at 20:30
• @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.
– Alan
Nov 6, 2015 at 21:08
• You can verify how Mathematica is calculating (more or less) by Map[f @@ # &, Partition[{a, b, c}, 2, 1]] // Trace Nov 6, 2015 at 21:22
• 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. Nov 6, 2015 at 21:23

1 Answer

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

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

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

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

res1 == res2
(* True *)

• While comparable in terms of memory compared to ListConvolve, DeveloperPartitionMap takes about twice as long as Map on my machine. Nov 6, 2015 at 22:03
• 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.
– Alan
Nov 7, 2015 at 0:27