How can I apply a function to a list with non-overlapping window. E.g. $data = \{x_1, x_2, ...., x_n\}$ and we apply f with window size 2 to get $\{f(x_1,x_2), f(x_3, x_4), ...., f(x_{n-1}. x_n)\}$.

I understand that I can partition and use map on the partitioned list. But are there more efficient ways to handle this operation?

  • 1
    $\begingroup$ BlockMap? $\endgroup$ – Rohit Namjoshi Jul 16 '19 at 15:27
  • 1
    $\begingroup$ Pre-version 10.2, you can do f /@ Partition[Range[10], 2]. $\endgroup$ – march Jul 16 '19 at 17:16
  • $\begingroup$ Yes, that might be as efficient as BlockMap. I was just thinking that there should be a direct implementation. As I see it was only from v 10.2 up. $\endgroup$ – Al Guy Jul 16 '19 at 20:07
data = Range[10];
BlockMap[f, data, 2]

(* {f[{1, 2}], f[{3, 4}], f[{5, 6}], f[{7, 8}], f[{9, 10}]} *)

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.