Last active
July 13, 2019 10:59
-
-
Save Altai-man/971f31ef5bf55ea24405bc0096fc7580 to your computer and use it in GitHub Desktop.
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
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 | |
} |
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 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