Skip to content

Instantly share code, notes, and snippets.

@raiph
Forked from Xliff/BQNListOp.md
Created June 13, 2023 02:49
Show Gist options
  • Save raiph/1c59c17e1da316669a288b929e7cb0b5 to your computer and use it in GitHub Desktop.
Save raiph/1c59c17e1da316669a288b929e7cb0b5 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.

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