-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnok.go
111 lines (98 loc) · 2.53 KB
/
nok.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2016 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package do
// ===========================================================================
// Nok represents some condition
// which is false by default.
//
// The null value is useful: its Do() never returns true.
type Nok func() bool
// Do applies Ok iff Ok is not nil
// and returns its value - usually false,
func (nok *Nok) Do() bool {
if *nok != nil {
return (*nok)()
}
return false
}
// Not returns the negation - usually true.
func (nok *Nok) Not() bool {
if *nok != nil {
return !(*nok)()
}
return true
}
// ===========================================================================
// NokJoin returns a closure around given fs.
//
// Iff there are no fs, nil is returned, and
// iff there is only one fs, this single fs is returned.
//
// Evaluate the returned function
// by invoking its Do() or
// by invoking it directly, iff not nil.
//
// Note: Order matters - evaluation terminates on first exceptional (non-default) result.
func NokJoin(fs ...Nok) Nok {
switch len(fs) {
case 0:
return nil
case 1:
return fs[0]
default:
return func() bool {
for _, f := range fs {
if (&f).Do() {
return true
}
}
return false
}
}
}
// ===========================================================================
// Set sets all noks as the new nok
// when the returned Option is applied.
func (nok *Nok) Set(noks ...Nok) Option {
return func(any interface{}) Opt {
prev := *nok
*nok = NokJoin(noks...)
return func() Opt {
return (*nok).Set(prev)(any)
}
}
}
// Add appends all noks to the existing nok
// when the returned Option is applied.
func (nok *Nok) Add(noks ...Nok) Option {
if nok == nil || *nok == nil {
return (*nok).Set(noks...)
}
return func(any interface{}) Opt {
prev := *nok
*nok = NokJoin(append([]Nok{prev}, noks...)...)
return func() Opt {
return (*nok).Set(prev)(any)
}
}
}
// ===========================================================================
// NokIt returns a Nok function
// which Do()es the Join of the given its
// and returns the default, namely: `false`,
// upon evaluation.
//
// Evaluate the returned function
// by invoking it's Do() or
// by invoking it directly, iff not nil.
//
// NokIt is a convenient wrapper.
func NokIt(its ...It) Nok {
return func() bool {
it := ItJoin(its...)
(&it).Do()
return false
}
}
// ===========================================================================