Course of Raku / Functional, concurrent, reactive, and web programming / Concurrent programming / Junctions

Building junctions

The simplest way to build a junction is with the junctive operators. The | operator makes an any junction:

my $j = 1 | 2 | 3;
say $j; # any(1, 2, 3)

The value $j stands for “1 or 2 or 3”. There are four kinds of junction, each with an operator and a matching function:

  • any( … ) or a | b — true if any value matches
  • all( … ) or a & b — true if all values match
  • one( … ) — true if exactly one value matches
  • none( … ) — true if no value matches

The function forms are handy with a list:

say all(3, 7, 2);  # all(3, 7, 2)
say none(1, 2, 3); # none(1, 2, 3)

A junction is most useful in a comparison. Asking whether a number equals an any junction tests it against every value at once:

say so 2 == any(1, 2, 3); # True
say so 5 == any(1, 2, 3); # False

The so turns the junctive result into a plain Boolean. The next topic looks at what happens behind the scenes when you do this.

Practice

Complete the quiz that covers the contents of this topic.

Course navigation

Junctions   |   Quiz — Junctions


💪 Or jump directly to the exercises in this section.