1
$\begingroup$

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?

$\endgroup$
3
  • 1
    $\begingroup$ BlockMap? $\endgroup$ Jul 16, 2019 at 15:27
  • 1
    $\begingroup$ Pre-version 10.2, you can do f /@ Partition[Range[10], 2]. $\endgroup$
    – march
    Jul 16, 2019 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, 2019 at 20:07

1 Answer 1

3
$\begingroup$
data = Range[10];
BlockMap[f, data, 2]

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

Your Answer

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

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