Skip to content

Instantly share code, notes, and snippets.

@Altai-man
Last active July 13, 2019 10:59
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 Altai-man/971f31ef5bf55ea24405bc0096fc7580 to your computer and use it in GitHub Desktop.
Save Altai-man/971f31ef5bf55ea24405bc0096fc7580 to your computer and use it in GitHub Desktop.
func gen(nums ...int) <-chan int {
out := make(chan int)
go func() {
for _, n := range nums {
out <- n
}
close(out)
}()
return out
}
func sq(in <-chan int) <-chan int {
out := make(chan int)
go func() {
for n := range in {
out <- n * n
}
close(out)
}()
return out
}
func main() {
// Set up the pipeline.
c := gen(2, 3)
out := sq(c)
// Consume the output.
fmt.Println(<-out) // 4
fmt.Println(<-out) // 9
}
sub gen(*@nums --> Supply) {
my $source = Supplier::Preserving.new;
start { $source.emit: $_ for @nums; $source.done }
$source.Supply;
}
sub sq(Supply $in --> Supply) {
supply whenever $in -> $n { emit $n * $n }
}
sub MAIN {
my $c = gen(2, 3);
my $out = sq($s);
react whenever $out { .say }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment