Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
47
Task/Generator-Exponential/Go/generator-exponential-1.go
Normal file
47
Task/Generator-Exponential/Go/generator-exponential-1.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// note: exponent not limited to ints
|
||||
func newPowGen(e float64) func() float64 {
|
||||
var i float64
|
||||
return func() (r float64) {
|
||||
r = math.Pow(i, e)
|
||||
i++
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// given two functions af, bf, both monotonically increasing, return a
|
||||
// new function that returns values of af not returned by bf.
|
||||
func newMonoIncA_NotMonoIncB_Gen(af, bf func() float64) func() float64 {
|
||||
a, b := af(), bf()
|
||||
return func() (r float64) {
|
||||
for {
|
||||
if a < b {
|
||||
r = a
|
||||
a = af()
|
||||
break
|
||||
}
|
||||
if b == a {
|
||||
a = af()
|
||||
}
|
||||
b = bf()
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fGen := newMonoIncA_NotMonoIncB_Gen(newPowGen(2), newPowGen(3))
|
||||
for i := 0; i < 20; i++ {
|
||||
fGen()
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Print(fGen(), " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
48
Task/Generator-Exponential/Go/generator-exponential-2.go
Normal file
48
Task/Generator-Exponential/Go/generator-exponential-2.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func newPowGen(e float64) chan float64 {
|
||||
ch := make(chan float64)
|
||||
go func() {
|
||||
for i := 0.; ; i++ {
|
||||
ch <- math.Pow(i, e)
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
// given two input channels, a and b, both known to return monotonically
|
||||
// increasing values, supply on channel c values of a not returned by b.
|
||||
func newMonoIncA_NotMonoIncB_Gen(a, b chan float64) chan float64 {
|
||||
ch := make(chan float64)
|
||||
go func() {
|
||||
for va, vb := <-a, <-b; ; {
|
||||
switch {
|
||||
case va < vb:
|
||||
ch <- va
|
||||
fallthrough
|
||||
case va == vb:
|
||||
va = <-a
|
||||
default:
|
||||
vb = <-b
|
||||
|
||||
}
|
||||
}
|
||||
}()
|
||||
return ch
|
||||
}
|
||||
|
||||
func main() {
|
||||
ch := newMonoIncA_NotMonoIncB_Gen(newPowGen(2), newPowGen(3))
|
||||
for i := 0; i < 20; i++ {
|
||||
<-ch
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
fmt.Print(<-ch, " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue