Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial implementation of Junction type.
- Loading branch information
Showing
3 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| my class Junction is Mu { | ||
| has $!storage; # elements of Junction | ||
| has $!type; # type of Junction | ||
|
|
||
| method new(*@values, :$type) { | ||
| self.bless(*, :storage(@values.eager), :$type); | ||
| } | ||
|
|
||
| multi method Bool(Junction:D:) { | ||
| ($!storage.map({return True if $_}).gimme(*); return False) | ||
| if $!type eq 'any'; | ||
| ($!storage.map({return False unless $_}).gimme(*); return True) | ||
| if $!type eq 'all'; | ||
| ($!storage.map({return False if $_}).gimme(*); return True) | ||
| if $!type eq 'none'; | ||
| # 'one' junction | ||
| my $count = 0; | ||
| $!storage.map({ $count++ if $_; return False if $count > 1 }).gimme(*); | ||
| $count == 1; | ||
| } | ||
|
|
||
| submethod BUILD(:$!storage, :$!type) { } | ||
|
|
||
| multi method perl(Junction:D:) { | ||
| $!type ~ '(' ~ $!storage.map({$_.perl}).join(', ') ~ ')' | ||
| } | ||
| } | ||
|
|
||
| sub any(*@values) { Junction.new(@values, :type<any>); } | ||
| sub all(*@values) { Junction.new(@values, :type<all>); } | ||
| sub one(*@values) { Junction.new(@values, :type<one>); } | ||
| sub none(*@values) { Junction.new(@values, :type<none>); } | ||
|
|
||
| sub infix:<|>(*@values) { Junction.new(@values, :type<any>); } | ||
| sub infix:<&>(*@values) { Junction.new(@values, :type<all>); } | ||
| sub infix:<^>(*@values) { Junction.new(@values, :type<one>); } | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters