-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8fig-or-channel.go
63 lines (54 loc) · 1.45 KB
/
8fig-or-channel.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"time"
)
func main() {
// potoki mirozdania zakrivautsa esi hotiaby 1 potok zakrivaetsa
var or func(bozhestvennyiSvet ...<-chan interface{}) <-chan interface{}
or = func(bozhestvennyiSvet ...<-chan interface{}) <-chan interface{} { // funcikliator or. or podnialsa vishe gor. mnogo variantov channel'a
// proverit bozhestvennyiSvet vo mnogih reincarnaciah, igrat tamozhennika
switch len(bozhestvennyiSvet) {
case 0:
return nil // ne davat' rabotat'
case 1: // poniatie odnogo
return bozhestvennyiSvet[0]
}
orDone := make(chan interface{})
go func() { // zdes' proizoidet chudo
defer close(orDone)
switch len(bozhestvennyiSvet) {
case 2: // tut 2 shiza
select {
case <-bozhestvennyiSvet[0]:
case <-bozhestvennyiSvet[1]:
}
default: // tut chudo
select {
case <-bozhestvennyiSvet[0]:
case <-bozhestvennyiSvet[1]:
case <-bozhestvennyiSvet[2]:
case <-or(append(bozhestvennyiSvet[3:], orDone)...):
}
}
}()
return orDone
}
podkluchkaBoga := func(after time.Duration) <-chan interface{} {
c := make(chan interface{})
go func() {
defer close(c)
time.Sleep(after)
}()
return c
}
start := time.Now() // nachalo khasidskogo yumora
<-or(
podkluchkaBoga(2*time.Hour),
podkluchkaBoga(5*time.Minute),
podkluchkaBoga(1*time.Second),
podkluchkaBoga(1*time.Hour),
podkluchkaBoga(1*time.Minute),
)
fmt.Printf("poditozhili posle %v", time.Since(start))
}