Skip to content

Instantly share code, notes, and snippets.

@Xliff
Created May 22, 2025 04:35
Show Gist options
  • Save Xliff/c12d6a0442668a555fa49fc5346dc574 to your computer and use it in GitHub Desktop.
Save Xliff/c12d6a0442668a555fa49fc5346dc574 to your computer and use it in GitHub Desktop.
Raku - An annoying thing about <Z>

An annoying thing about Z, zip, and roundrobin is that they don't allow you to use lists of differing sizes. I whipped up this custom infix to solve the problem:

multi sub infix:<ZZ> ($a is copy, $b is copy) {
  my ($larger, $smaller) = ($a, $b);

  if $a.elems != $b.elems {
    ($larger, $smaller) = ($a, $b) if $a.elems > $b.elems;

    my $sc = $smaller.clone;
    while $smaller.elems < $larger.elems {
      $smaller.push: $sc.head;
      $sc.rotate(-1)
    }
  }

  (
    do {
      $a.elems > $b.elems
        ?? ( $smaller[] Z $larger[]  )
        !! ( $larger[]  Z $smaller[] )
    }
  ).map( *.flat )
}

I suspect there are smaller alternatives...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment