Generator/Exponential: Difference between revisions

Go solution
(Go solution)
Line 53:
}
println()</lang>
=={{header|Go}}==
Go channels and goroutines implement generators directly.
<lang go>package main
 
import "fmt"
 
func genSquares(ch chan<- int) {
for i := 0; ; i++ {
ch <- i * i
}
}
 
func genCubes(ch chan<- int) {
for i := 0; ; i++ {
ch <- i * i * i
}
}
 
// this long function name to indicate that the filtering
// logic works only because squares and cubes are both
// monotonically increasing over the (presumed) domain
// of non-negative integers.
func genMonotonicIncA_NotMonotonicIncB(outCh chan<- int, aCh, bCh <-chan int) {
for a, b := <-aCh, <-bCh; ; {
if a > b {
b = <-bCh
continue
} else if a < b {
outCh <- a
}
a = <-aCh
}
}
 
func main() {
chSq := make(chan int)
go genSquares(chSq)
chCu := make(chan int)
go genCubes(chCu)
chF := make(chan int)
go genMonotonicIncA_NotMonotonicIncB(chF, chSq, chCu)
for i := 0; i < 20; i++ {
<-chF
}
for i := 0; i < 10; i++ {
fmt.Print(<-chF, " ")
}
fmt.Println("")
}</lang>
Output:
<pre>
529 576 625 676 784 841 900 961 1024 1089
</pre>
 
=={{header|Haskell}}==
1,707

edits