Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial implementation of Junction type.
  • Loading branch information
pmichaud committed Jul 7, 2011
1 parent ecc173b commit 7d4d042
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/core/Junction.pm
@@ -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>); }

1 change: 0 additions & 1 deletion src/core/stubs.pm
Expand Up @@ -3,7 +3,6 @@
# Code/Block/Routine/Sub/Method and Str/Int/Num. They are built in BOOTSTRAP.pm
# in Perl6::Metamodel for now, though should be a BEGIN block in CORE.setting
# in the end.
my class Junction is Mu { }
my class Whatever is Cool { ... }
my class WhateverCode is Code { ... }
my class Bag is Iterable does Associative { }
Expand Down
1 change: 1 addition & 0 deletions tools/build/Makefile.in
Expand Up @@ -130,6 +130,7 @@ CORE_SOURCES = \
src/core/Code.pm \
src/core/WhateverCode.pm \
src/core/Attribute.pm \
src/core/Junction.pm \
src/core/Cool.pm \
src/core/Whatever.pm \
src/core/Bool.pm \
Expand Down

0 comments on commit 7d4d042

Please sign in to comment.