Created
March 9, 2013 16:21
-
-
Save masak/5124688 to your computer and use it in GitHub Desktop.
From a starting seed and a bunch of operations, generate the closure
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
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