Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active June 13, 2023 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Xliff/9ed2279ca10ee3d52809832ce19fe81d to your computer and use it in GitHub Desktop.
Save Xliff/9ed2279ca10ee3d52809832ce19fe81d to your computer and use it in GitHub Desktop.
To Raku - The BQN List Operator

The BQN list operator can readily transfer into Raku. It's basically the same as Raku's list operator: ,. There is a small devil in the detail and that can be illustrated in the following example:

multi sub infix:<> ($a, $b) { 
  infix:<,>($a, $b) 
}

say 1‿2‿3; # ((1 2) 3)

An easy thought as to a solution would be to flatten the result of the infix operation, but that would be incorrect. The trick is to flatten only when the LHS is a List. So a better version of the above operation is as follows:

# Can't use SmartMatch because Array ~~ List is True!
multi sub infix:<> ($a, $b) { 
  infix:<,>($a.WHAT === List ?? |$a !! $a, $b) 
}

say 1‿2‿3   # (1 2 3)
say [1‿2]‿3 # ([1 2] 3)

Would be interested in hearing from some of the BQN coders out there if this works for them.

@raiph
Copy link

raiph commented Jun 12, 2023

say [1‿2]‿3 # ((1 2) 3 should be say [1‿2]‿3 # ([1 2] 3).

@Xliff
Copy link
Author

Xliff commented Jun 12, 2023

Ah... good point. Thanks.

@0racle
Copy link

0racle commented Jun 13, 2023

This seems to do the trick.

sub infix:<>(|c) is assoc('list') {
    infix:<,>(|c)
}

say 1‿2‿3‿4‿5;   # (1 2 3 4 5)
say ((1‿2)‿3)‿4; # (((1 2) 3) 4)

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