-
Notifications
You must be signed in to change notification settings - Fork 5
/
dry.go
executable file
·144 lines (126 loc) · 4.22 KB
/
dry.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2022, nwillc@gmail.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package genfuncs
import (
"fmt"
"golang.org/x/exp/constraints"
)
var (
// Orderings
LessThan = -1
EqualTo = 0
GreaterThan = 1
// Predicates
IsBlank = OrderedEqualTo("")
IsNotBlank = Not(IsBlank)
F32IsZero = OrderedEqualTo(float32(0.0))
F64IsZero = OrderedEqualTo(0.0)
IIsZero = OrderedEqualTo(0)
)
// Empty return an empty value of type T.
func Empty[T any]() (empty T) {
return empty
}
// OrderedEqual returns true jf a is ordered equal to b.
func OrderedEqual[O constraints.Ordered](a, b O) (orderedEqualTo bool) {
orderedEqualTo = Ordered(a, b) == EqualTo
return orderedEqualTo
}
// OrderedEqualTo return a function that returns true if its argument is ordered equal to a.
func OrderedEqualTo[O constraints.Ordered](a O) (fn Function[O, bool]) {
fn = Curried[O, bool](OrderedEqual[O], a)
return fn
}
// OrderedGreater returns true if a is ordered greater than b.
func OrderedGreater[O constraints.Ordered](a, b O) (orderedGreaterThan bool) {
orderedGreaterThan = Ordered(a, b) == GreaterThan
return orderedGreaterThan
}
// OrderedGreaterThan returns a function that returns true if its argument is ordered greater than a.
func OrderedGreaterThan[O constraints.Ordered](a O) (fn Function[O, bool]) {
fn = Curried[O, bool](OrderedGreater[O], a)
return fn
}
// OrderedLess returns true if a is ordered less than b.
func OrderedLess[O constraints.Ordered](a, b O) (orderedLess bool) {
orderedLess = Ordered(a, b) == LessThan
return orderedLess
}
// OrderedLessThan returns a function that returns true if its argument is ordered less than a.
func OrderedLessThan[O constraints.Ordered](a O) (fn Function[O, bool]) {
fn = Curried[O, bool](OrderedLess[O], a)
return fn
}
// Max returns max value one or more constraints.Ordered values,
func Max[T constraints.Ordered](v ...T) (max T) {
if len(v) == 0 {
panic(fmt.Errorf("%w: at leat one value required", IllegalArguments))
}
max = v[0]
for _, val := range v {
if val > max {
max = val
}
}
return max
}
// Min returns min value of one or more constraints.Ordered values,
func Min[T constraints.Ordered](v ...T) (min T) {
if len(v) == 0 {
panic(fmt.Errorf("%w: at leat one value required", IllegalArguments))
}
min = v[0]
for _, val := range v {
if val < min {
min = val
}
}
return min
}
// StringerToString creates a ToString for any type that implements fmt.Stringer.
func StringerToString[T fmt.Stringer]() (fn ToString[T]) {
fn = func(t T) string { return t.String() }
return fn
}
// TransformArgs uses the function to transform the arguments to be passed to the operation.
func TransformArgs[T1, T2, R any](transform Function[T1, T2], operation BiFunction[T2, T2, R]) (fn BiFunction[T1, T1, R]) {
fn = func(a, b T1) R {
return operation(transform(a), transform(b))
}
return fn
}
// Curried takes a BiFunction and one argument, and Curries the function to return a single argument Function.
func Curried[A, R any](operation BiFunction[A, A, R], a A) (fn Function[A, R]) {
fn = func(b A) R { return operation(b, a) }
return fn
}
// Not takes a predicate returning and inverts the result.
func Not[T any](predicate Function[T, bool]) (fn Function[T, bool]) {
fn = func(a T) bool { return !predicate(a) }
return fn
}
// Ordered performs old school -1/0/1 comparison of constraints.Ordered arguments.
func Ordered[T constraints.Ordered](a, b T) (order int) {
switch {
case a < b:
order = LessThan
case a > b:
order = GreaterThan
default:
order = EqualTo
}
return order
}