-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpipeline.go
45 lines (37 loc) · 930 Bytes
/
pipeline.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
package pipeline
type PipeValue interface{}
type PipeNext func(PipeValue) PipeValue
type PipeInterface interface {
Handle(PipeValue, PipeNext) PipeValue
}
type Pipeline struct {
value PipeValue
pipes []PipeInterface
}
func Send(value interface{}) *Pipeline {
return &Pipeline{value: value}
}
func (p *Pipeline) Through(pipes []PipeInterface) *Pipeline {
p.pipes = pipes
return p
}
func (p *Pipeline) Then(destination PipeNext) PipeValue {
carry := p.carry()
stack := destination
for i := len(p.pipes) - 1; i >= 0; i-- {
stack = carry(stack, p.pipes[i])
}
return stack(p.value)
}
func (p *Pipeline) ThenReturn() PipeValue {
return p.Then(func(value PipeValue) PipeValue {
return value
})
}
func (p *Pipeline) carry() func(PipeNext, PipeInterface) PipeNext {
return func(stack PipeNext, pipe PipeInterface) PipeNext {
return func(value PipeValue) PipeValue {
return pipe.Handle(value, stack)
}
}
}