4 ms·
I'm not sure this is how you're supposed to use junctions. I vaguely recall that the semantics is not obvious when you put a junction on both sides of a relati
by grondilu 11y ago
I'm not sure this is how you're supposed to use junctions. I vaguely recall that the semantics is not obvious when you put a junction on both sides of a relational operator. It's much clearer when there is a junction on only one side.
More typical uses are:
die "unauthorized value" if $_ == 1|2;
grep none(1, 2), @values;
say "$a and $b are even" if ($a & $b) %% 2;
Basically : junctions are not sets. Don't try to use them for set operations. Perl 6 has a Set structure. Use that instead.
- masklinn 11y agoThe first two seem to be set operations ("value in set" and "filter values not in set"), I've no idea what the third one is supposed to do, something like all ((==0).(`rem` 2))?
- bhaak 11y agoThe third seems to be shorthand for map and reduce (or only reduce). In Ruby, you could express the same with `[2,4,6].reduce(true) {|even,num| even & num.even? }` or more clearer IMO `[2,4,6].map(&:even?).reduce(&:&)`.
- grondilu 11y agoBy set operations I was more specifically thinking of intersection and union. Testing if an element belongs to a set can indeed be done with junctions but that's pretty much it. The third example shows the so-called auto-threading behavior. I think it's significantly different than what you can do with sets. A Junction in Perl 6 is "a superposition of data values pretending to be a single value". So for instance any algebraic expression can be used with a junction as if it was a scalar. The result will be a junction. In this example the expression is $_ %% 2, which is Perl 6's way of writing the boolean statement "the variable $_ is divisible by 2". We use this expression with ($a & $b) instead of $_.