);
my @decls;
- if (@layers) {
- push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers);
- }
- for (@other_mods) {
- push @decls, $_->declare($tikz);
- }
+ push @decls, LaTeX::TikZ::Mod::Layer->declare(@layers) if @layers;
+ push @decls, $_->declare($tikz) for @other_mods;
my @content = (
"\\begin{tikzpicture}",
- $seq->draw($tikz)->instantiate,
+ @{ $seq->draw($tikz) },
"\\end{tikzpicture}",
);
}
use overload (
- '.' => \&concat,
+ '@{}' => 'dereference',
);
sub flatten {
return @body;
}
-sub concat {
- my ($left, $right, $rev) = @_;
-
- $_body_tc->assert_valid($right);
+sub dereference { [ $_[0]->instantiate ] }
- $left = $left->flatten;
+sub fold {
+ my ($left, $right, $rev) = @_;
my (@left, @right);
- if ($my_tc->check($right)) {
- $right = $right->flatten;
-
- my ($only_left, $common, $only_right) = $inter->(
- $left->_mods_cache,
- $right->_mods_cache,
- );
-
- if (@$common) {
- my $x = $left->new
- ->mod(@$only_left)
- ->body($left->_body);
- my $y = $left->new
- ->mod(@$only_right)
- ->body($right->_body);
- ($x, $y) = ($y, $x) if $rev;
- return $left->new
- ->mod(@$common)
- ->body($x . $y);
+ if ($my_tc->check($left)) {
+ $left = $left->flatten;
+
+ if ($my_tc->check($right)) {
+ $right = $right->flatten;
+
+ my ($only_left, $common, $only_right) = $inter->(
+ $left->_mods_cache,
+ $right->_mods_cache,
+ );
+
+ if (@$common) {
+ my $x = $left->new
+ ->mod(@$only_left)
+ ->body($left->_body);
+ my $y = $left->new
+ ->mod(@$only_right)
+ ->body($right->_body);
+ return $left->new
+ ->mod(@$common)
+ ->body(fold($x, $y, $rev));
+ } else {
+ @right = $right->instantiate;
+ }
} else {
- @right = $right->instantiate;
+ $_body_tc->assert_valid($right);
+ @right = @$right;
}
+
+ @left = $left->instantiate;
} else {
- @right = @$right;
+ if ($my_tc->check($right)) {
+ return fold($right, $left, 1);
+ } else {
+ $_body_tc->assert_valid($_) for $left, $right;
+ @left = @$left;
+ @right = @$right;
+ }
}
- @left = $left->instantiate;
-
- $left->new
- ->body($rev ? [ @right, @left ] : [ @left, @right ]);
+ $rev ? [ @right, @left ] : [ @left, @right ];
}
__PACKAGE__->meta->make_immutable;
my @mods = $set->mods_unique;
- LaTeX::TikZ::Scope->new
- ->mod(map $_->apply($tikz), @mods)
- ->body($set->_set->draw($tikz))
+ my $body = $set->_set->draw($tikz);
+
+ if (@mods) {
+ $body = LaTeX::TikZ::Scope->new
+ ->mod(map $_->apply($tikz), @mods)
+ ->body($body);
+ }
+
+ $body;
}
}
use List::Util ();
+use LaTeX::TikZ::Scope;
+
use Any::Moose;
use Any::Moose 'Util::TypeConstraints'
=> [ qw/subtype as where find_type_constraint/ ];
sub draw {
my $set = shift;
- List::Util::reduce { $a . $b } map $_->draw(@_), $set->kids;
+ List::Util::reduce { LaTeX::TikZ::Scope::fold($a, $b) }
+ map $_->draw(@_),
+ $set->kids;
}
use LaTeX::TikZ::API seq => sub {
shift;
- die 'wut' if $_[0]->isa('LaTeX::TikZ::Set::Op');
-
__PACKAGE__->new(kids => \@_);
};