syakutori

Macro syakutori 

Source
macro_rules! syakutori {
    (
        $n:expr,
        ($l:ident, $r:ident),
        |$i:ident| $addable:expr,
        |$j:ident| $add:expr,
        |$k:ident| $remove:expr,
        $show:expr $(,)?
    ) => { ... };
}
Expand description

arg:

  • n: length of array
  • (l, r): ident of left-bound and right-bound
  • addable: |index| expr: return is addable element
  • add: |index| expr: add element
  • remove: |index| expr: remove element
  • show: call n times for l in 0..n, with rightmost r
let (a, w) = ([1, 2, 3, 4], 6);
let (mut ans, mut acc) = (0, 0);
syakutori!(
    a.len(),
    (l, r),
    |i| acc + a[i] <= w,
    |i| acc += a[i],
    |i| acc -= a[i],
    ans += r - l
);
assert_eq!(ans, 7);