-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfun.go
More file actions
73 lines (61 loc) · 1.16 KB
/
fun.go
File metadata and controls
73 lines (61 loc) · 1.16 KB
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
package fun
import (
"fmt"
"log"
)
// Pair is a data structure that has two values.
type Pair[K, V any] struct {
K K
V V
}
func Zero[T any]() T {
var zero T
return zero
}
// ToString converts the value to string.
func ToString[A any](a A) string {
return fmt.Sprintf("%v", a)
}
// DebugP returns function that prints prefix with element and returns it.
// Useful for debug printing.
func DebugP[V any](prefix string) func(V) V {
return func(v V) V {
// TODO: print place
log.Println(prefix, v)
return v
}
}
// Debug returns function that prints element and returns it.
// Useful for debug printing.
func Debug[V any](v V) V {
// TODO: print place
log.Println(v)
return v
}
func Has[K comparable, V any](dict map[K]V, key K) bool {
_, ok := dict[key]
return ok
}
func Cond[R any](defaultValue R, cases ...func() (R, bool)) R {
for _, case_ := range cases {
if res, ok := case_(); ok {
return res
}
}
return defaultValue
}
func Ptr[T any](t T) *T {
return &t
}
func Deref[T any](ptr *T) T {
if ptr == nil {
return Zero[T]()
}
return *ptr
}
func Pipe[T any](t T, endos ...func(T) T) T {
for _, endo := range endos {
t = endo(t)
}
return t
}