Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junction ~~ Junction behavior

Tags:

raku

I want to check if all elements of an Array have given type.

$ raku -e 'my @t = 1,2,3; say all(@t) ~~ Int'
True

$ raku -e 'my @t = 1,2,3,"a"; say all(@t) ~~ Int'
False

Works as expected so far. Now I want to allow two types:

$ raku -e 'my @t = 1,2,3,"a"; say all(@t) ~~ Int|Str'
False

Why is so? If 1 ~~ Int|Str is True for single element why does it fail for all() elements junction?

BTW: This question is about understanding Junction ~~ Junction behavior (which is also a bit undocumented), not about alternative way of performing check from example (which I know is possible).

like image 264
Pawel Pabian bbkr Avatar asked Aug 11 '21 14:08

Pawel Pabian bbkr


1 Answers

A few additional lines may help clarify what's going on:

say all(1,    2,   3)  ~~ Int|Str; # OUTPUT: «True»
say all('a', 'b', 'c') ~~ Int|Str; # OUTPUT: «True»
say all(1,    2,  'c') ~~ Int|Str; # OUTPUT: «False»

That is, all(1, 2, 'c') ~~ Int|Str is asking "is it the case that all of 1, 2, 'c' are Ints or, alternatively, is it the case that all of 1, 2, 'c' are Strs?" Since neither of those is the case, it returns False.

like image 53
codesections Avatar answered Nov 01 '22 12:11

codesections