This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
values.go
369 lines (296 loc) · 8.12 KB
/
values.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package parens
import (
"fmt"
"math"
"strconv"
)
var (
_ Any = Nil{}
_ Any = Int64(0)
_ Any = Float64(1.123123)
_ Any = Bool(true)
_ Any = Char('∂')
_ Any = String("specimen")
_ Any = Symbol("specimen")
_ Any = Keyword("specimen")
_ Any = (*LinkedList)(nil)
_ Seq = (*LinkedList)(nil)
)
// Comparable values define a partial ordering.
type Comparable interface {
// Comp(pare) the value to another value. Returns:
//
// * 0 if v == other
// * 1 if v > other
// * -1 if v < other
//
// If the values are not comparable, ErrIncomparableTypes is returned.
Comp(other Any) (int, error)
}
// EqualityProvider asserts equality between two values.
type EqualityProvider interface {
Equals(other Any) (bool, error)
}
// Eq returns true if two values are equal.
func Eq(a, b Any) (bool, error) {
if eq, ok := a.(EqualityProvider); ok {
return eq.Equals(b)
} else if cmp, ok := a.(Comparable); ok {
val, err := cmp.Comp(b)
return val == 0, err
} else if cmp, ok := b.(Comparable); ok {
val, err := cmp.Comp(a)
return val == 0, err
}
return false, nil
}
// Lt returns true if a < b
func Lt(a, b Any) (bool, error) {
if acmp, ok := a.(Comparable); ok {
i, err := acmp.Comp(b)
return i == -1, err
}
return false, ErrIncomparableTypes
}
// Gt returns true if a > b
func Gt(a, b Any) (bool, error) {
if acmp, ok := a.(Comparable); ok {
i, err := acmp.Comp(b)
return i == 1, err
}
return false, ErrIncomparableTypes
}
// Le returns true if a <= b
func Le(a, b Any) (bool, error) {
if acmp, ok := a.(Comparable); ok {
i, err := acmp.Comp(b)
return i <= 0, err
}
return false, ErrIncomparableTypes
}
// Ge returns true if a >= b
func Ge(a, b Any) (bool, error) {
if acmp, ok := a.(Comparable); ok {
i, err := acmp.Comp(b)
return i >= 0, err
}
return false, ErrIncomparableTypes
}
// Cons returns a new seq with `v` added as the first and `seq` as the rest.
// seq can be nil as well.
func Cons(v Any, seq Seq) (Seq, error) {
newSeq := &LinkedList{
first: v,
rest: seq,
count: 1,
}
if seq != nil {
cnt, err := seq.Count()
if err != nil {
return nil, err
}
newSeq.count = cnt + 1
}
return newSeq, nil
}
// NewList returns a new linked-list containing given values.
func NewList(items ...Any) Seq {
if len(items) == 0 {
return Seq((*LinkedList)(nil))
}
var err error
lst := Seq(&LinkedList{})
for i := len(items) - 1; i >= 0; i-- {
if lst, err = Cons(items[i], lst); err != nil {
panic(err)
}
}
return lst
}
// Nil represents the Value 'nil'.
type Nil struct{}
// SExpr returns a valid s-expression representing Nil.
func (Nil) SExpr() (string, error) { return "nil", nil }
// Equals returns true IFF other is nil.
func (Nil) Equals(other Any) (bool, error) { return IsNil(other), nil }
func (Nil) String() string { return "nil" }
// Int64 represents a 64-bit integer Value.
type Int64 int64
// SExpr returns a valid s-expression representing Int64.
func (i64 Int64) SExpr() (string, error) { return i64.String(), nil }
// Equals returns true if 'other' is also an integer and has same Value.
func (i64 Int64) Equals(other Any) (bool, error) {
val, ok := other.(Int64)
return ok && (val == i64), nil
}
// Comp performs comparison against another Int64.
func (i64 Int64) Comp(other Any) (int, error) {
if n, ok := other.(Int64); ok {
switch {
case i64 > n:
return 1, nil
case i64 < n:
return -1, nil
default:
return 0, nil
}
}
return 0, ErrIncomparableTypes
}
func (i64 Int64) String() string { return strconv.Itoa(int(i64)) }
// Float64 represents a 64-bit double precision floating point Value.
type Float64 float64
// SExpr returns a valid s-expression representing Float64.
func (f64 Float64) SExpr() (string, error) { return f64.String(), nil }
// Equals returns true if 'other' is also a float and has same Value.
func (f64 Float64) Equals(other Any) (bool, error) {
val, ok := other.(Float64)
return ok && (val == f64), nil
}
// Comp performs comparison against another Float64.
func (f64 Float64) Comp(other Any) (int, error) {
if n, ok := other.(Float64); ok {
switch {
case f64 > n:
return 1, nil
case f64 < n:
return -1, nil
default:
return 0, nil
}
}
return 0, ErrIncomparableTypes
}
func (f64 Float64) String() string {
if math.Abs(float64(f64)) >= 1e16 {
return fmt.Sprintf("%e", f64)
}
return fmt.Sprintf("%f", f64)
}
// Bool represents a boolean Value.
type Bool bool
// SExpr returns a valid s-expression representing Bool.
func (b Bool) SExpr() (string, error) { return b.String(), nil }
// Equals returns true if 'other' is a boolean and has same logical Value.
func (b Bool) Equals(other Any) (bool, error) {
val, ok := other.(Bool)
return ok && (val == b), nil
}
func (b Bool) String() string {
if b {
return "true"
}
return "false"
}
// Char represents a Unicode character.
type Char rune
// SExpr returns a valid s-expression representing Char.
func (char Char) SExpr() (string, error) {
return fmt.Sprintf("\\%c", char), nil
}
// Equals returns true if the other Value is also a character and has same Value.
func (char Char) Equals(other Any) (bool, error) {
val, isChar := other.(Char)
return isChar && (val == char), nil
}
func (char Char) String() string { return fmt.Sprintf("\\%c", char) }
// String represents a string of characters.
type String string
// SExpr returns a valid s-expression representing String.
func (str String) SExpr() (string, error) { return str.String(), nil }
// Equals returns true if 'other' is string and has same Value.
func (str String) Equals(other Any) (bool, error) {
otherStr, isStr := other.(String)
return isStr && (otherStr == str), nil
}
func (str String) String() string { return fmt.Sprintf("\"%s\"", string(str)) }
// Symbol represents a lisp symbol Value.
type Symbol string
// SExpr returns a valid s-expression representing Symbol.
func (sym Symbol) SExpr() (string, error) { return string(sym), nil }
// Equals returns true if the other Value is also a symbol and has same Value.
func (sym Symbol) Equals(other Any) (bool, error) {
otherSym, isSym := other.(Symbol)
return isSym && (sym == otherSym), nil
}
func (sym Symbol) String() string { return string(sym) }
// Keyword represents a keyword Value.
type Keyword string
// SExpr returns a valid s-expression representing Keyword.
func (kw Keyword) SExpr() (string, error) { return kw.String(), nil }
// Equals returns true if the other Value is keyword and has same Value.
func (kw Keyword) Equals(other Any) (bool, error) {
otherKW, isKeyword := other.(Keyword)
return isKeyword && (otherKW == kw), nil
}
func (kw Keyword) String() string { return fmt.Sprintf(":%s", string(kw)) }
// LinkedList implements an immutable Seq using linked-list data structure.
type LinkedList struct {
count int
first Any
rest Seq
}
// SExpr returns a valid s-expression for LinkedList.
func (ll *LinkedList) SExpr() (string, error) {
if ll == nil {
return "()", nil
}
return SeqString(ll, "(", ")", " ")
}
// Equals returns true if the other value is a LinkedList and contains the same
// values.
func (ll *LinkedList) Equals(other Any) (eq bool, err error) {
o, ok := other.(*LinkedList)
if !ok || o.count != ll.count {
return
}
var s Seq = ll
err = ForEach(o, func(any Any) (bool, error) {
v, _ := s.First()
veq, ok := v.(EqualityProvider)
if !ok {
return false, nil
}
if eq, err = veq.Equals(any); err != nil || !eq {
return true, err
}
s, _ = s.Next()
return false, nil
})
return
}
// Conj returns a new list with all the items added at the head of the list.
func (ll *LinkedList) Conj(items ...Any) (res Seq, err error) {
if ll == nil {
res = &LinkedList{}
} else {
res = ll
}
for _, item := range items {
if res, err = Cons(item, res); err != nil {
break
}
}
return
}
// First returns the head or first item of the list.
func (ll *LinkedList) First() (Any, error) {
if ll == nil {
return nil, nil
}
return ll.first, nil
}
// Next returns the tail of the list.
func (ll *LinkedList) Next() (Seq, error) {
if ll == nil {
return nil, nil
}
return ll.rest, nil
}
// Count returns the number of the list.
func (ll *LinkedList) Count() (int, error) {
if ll == nil {
return 0, nil
}
return ll.count, nil
}