-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpredef.go
109 lines (82 loc) · 1.95 KB
/
predef.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
package monadgo
import (
"fmt"
"reflect"
)
// Any respensts root type of monadgo.
type Any interface {
// Get returns original go value.
Get() interface{}
// rv returns reflect.Value wrapping original value.
rv() reflect.Value
// force monadgo type must implement String()
fmt.Stringer
}
// ----------------------------------------------------------------------------
// Unit represents scala-like Unit.
type Unit interface {
Any
}
type _unit struct{}
func (u _unit) Get() interface{} {
return unit
}
func (u _unit) rv() reflect.Value {
return unitValue
}
func (u _unit) String() string {
return "Void"
}
// ----------------------------------------------------------------------------
// Null represents scala-like Null.
type Null interface {
Any
}
type _null struct{}
func (n *_null) Get() interface{} {
return nil
}
func (n *_null) rv() reflect.Value {
return nullValue
}
func (n *_null) String() string {
return "Null"
}
// ----------------------------------------------------------------------------
// Nothing represents scala-like Nothing.
type Nothing interface {
Null
}
type _nothing struct {
Null
}
func (n *_nothing) rv() reflect.Value {
return nothingValue
}
func (n *_nothing) String() string {
return "Nothing"
}
// ----------------------------------------------------------------------------
// PartialFunc represents scala-like PartailFunction.
type PartialFunc struct {
condition funcTR
action funcTR
}
// PartialFuncOf returns a partail function for monandgo.
func PartialFuncOf(c, a interface{}) PartialFunc {
return PartialFunc{
condition: funcOf(c),
action: funcOf(a),
}
}
// DefinedAt returns x is defined at p or not.
func (p PartialFunc) DefinedAt(v reflect.Value) bool {
return p.condition.call(v).Bool()
}
// Call invokes action on x, returns Nothing if x is not defined in p.
func (p PartialFunc) Call(v reflect.Value) reflect.Value {
if p.DefinedAt(v) {
return p.action.call(v)
}
return nothingValue
}