-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamb-1.go
49 lines (45 loc) · 1.08 KB
/
amb-1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"fmt"
"sync"
)
func ambStrings(ss []string) chan []string {
c := make(chan []string)
go func() {
for _, s := range ss {
c <- []string{s}
}
close(c)
}()
return c
}
func ambChain(ss []string, cIn chan []string) chan []string {
cOut := make(chan []string)
go func() {
var w sync.WaitGroup
for chain := range cIn {
w.Add(1)
go func(chain []string) {
for s1 := range ambStrings(ss) {
if s1[0][len(s1[0])-1] == chain[0][0] {
cOut <- append(s1, chain...)
}
}
w.Done()
}(chain)
}
w.Wait()
close(cOut)
}()
return cOut
}
func main() {
s1 := []string{"the", "that", "a"}
s2 := []string{"frog", "elephant", "thing"}
s3 := []string{"walked", "treaded", "grows"}
s4 := []string{"slowly", "quickly"}
c := ambChain(s1, ambChain(s2, ambChain(s3, ambStrings(s4))))
for s := range c {
fmt.Println(s)
}
}