9
$\begingroup$

Given a list of pairs:

data = {{a,b},{c,d},{e,f},{g,h},{i,j}}

I need the moving map:

MapPair[F,data]

{ F[{a,b},{c,d}], F[{c,d},{e,f}], F[{e,f},{g,h}], F[{g,h},{i,j}] }

The built-in function MovingMap does not work:

MovingMap[F,data,2]

Which built-in function does maps a function F pairwise over the data?

$\endgroup$
4
  • $\begingroup$ I need a built-in function that essentially does this: myMapPair[func_, data_] := Array[func[data[[#1]], data[[#1 + 1]]] &, Length[data] - 1] $\endgroup$
    – QuantumDot
    Commented Jun 8, 2022 at 20:50
  • 12
    $\begingroup$ BlockMap[F, data, 2, 1] ought to work. $\endgroup$
    – Carl Woll
    Commented Jun 8, 2022 at 20:51
  • 2
    $\begingroup$ I like the BlockMap idea. You can also try mapPair[f_, v_] := f @@@ Partition[ v, 2, 1] $\endgroup$
    – irchans
    Commented Jun 8, 2022 at 21:00
  • $\begingroup$ @CarlWoll Thanks! I knew it had to exist! $\endgroup$
    – QuantumDot
    Commented Jun 8, 2022 at 21:06

5 Answers 5

14
$\begingroup$
BlockMap[Apply[F], data, 2, 1]
$\endgroup$
1
  • $\begingroup$ i usually use mapthread when i have multiple variables but i knew there had to be a shorter more robust MMA "blackbelt" configuration. i havent tried it but assuming this works this should be at the very top and i definitely be using next time i need to break out with sewing needles. $\endgroup$ Commented Jun 10, 2022 at 16:55
6
$\begingroup$

Using MapAt:

MapAt[Apply[F], Partition[data, {2}, 1], All]
(*{F[{a, b}, {c, d}], F[{c, d}, {e, f}], F[{e, f}, {g, h}], F[{g, h}, {i, j}]}*)
$\endgroup$
0
3
$\begingroup$
MapThread[F, {Most@#, Rest@#}]&[data]

(* {F[{a, b}, {c, d}], F[{c, d}, {e, f}], F[{e, f}, {g, h}], F[{g, h}, {i, j}]} *)

In addition, as kglr has pointed out here, Partition can take an undocumented sixth argument.

Partition[data,2,1,{1,-1},{},F]

(* {F[{a, b}, {c, d}], F[{c, d}, {e, f}], F[{e, f}, {g, h}], F[{g, h}, {i, j}]} *)
$\endgroup$
2
$\begingroup$

2 more possibilities:

data = {{a, b}, {c, d}, {e, f}, {g, h}, {i, j}};

Most @ MapThread[F, {data, RotateLeft @ data}]

{F[{a, b}, {c, d}], F[{c, d}, {e, f}], F[{e, f}, {g, h}], F[{g, h}, {i, j}]}

F @@@ Transpose[{data[[;; -2]], data[[2 ;;]]}]

{F[{a, b}, {c, d}], F[{c, d}, {e, f}], F[{e, f}, {g, h}], F[{g, h}, {i, j}]}

$\endgroup$
2
$\begingroup$

The first argument of Partition can have any Head. So we can use

List @@ Partition[F @@ data, 2, 1]
{F[{a, b}, {c, d}], 
 F[{c, d}, {e, f}],   
 F[{e, f}, {g, h}],   
 F[{g, h}, {i, j}]}

Generalizing:

partitionMap = List @@ Partition[# @@ #2, ##3] &;

partitionMap[F, data, 2, 1]
{F[{a, b}, {c, d}], 
 F[{c, d}, {e, f}],   
 F[{e, f}, {g, h}],   
 F[{g, h}, {i, j}]}
partitionMap[F, data, 3, 1]
 {F[{a, b}, {c, d}, {e, f}],   
  F[{c, d}, {e, f}, {g, h}],   
  F[{e, f}, {g, h}, {i, j}]}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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