Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@masak
Created March 9, 2013 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/5124688 to your computer and use it in GitHub Desktop.
Save masak/5124688 to your computer and use it in GitHub Desktop.
From a starting seed and a bunch of operations, generate the closure
sub closure($elem, *@ops) {
my $set = set $elem;
my @queue = $elem;
while @queue {
my $e = pop @queue;
for @ops.map(-> &op { &op($e)}) -> $d {
next if $d (elem) $set;
$set = $set (|) $d;
push @queue, $d;
}
}
$set;
}
sub swap($p1, $p2) {
-> $s {
my @s = $s.comb;
@s[$p1, $p2].=reverse;
@s.join
}
}
say closure "abcd", swap(0, 1), swap(1, 2), swap(2, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment