@@ -88,6 +88,38 @@ func getCompSign(e expreduceapi.Ex) int {
88
88
return - 2
89
89
}
90
90
91
+ func int64InSlice (target int64 , list []int64 ) bool {
92
+ for _ , v := range list {
93
+ if v == target {
94
+ return true
95
+ }
96
+ }
97
+ return false
98
+ }
99
+
100
+ func comparisonFn (expr expreduceapi.ExpressionInterface , es expreduceapi.EvalStateInterface , expectedOrders []int64 ) expreduceapi.Ex {
101
+ var lastN expreduceapi.Ex = nil
102
+ for i := 1 ; i < len (expr .GetParts ()); i ++ {
103
+ currN := es .Eval (
104
+ atoms .NewExpression (
105
+ []expreduceapi.Ex {
106
+ atoms .NewSymbol ("System`N" ),
107
+ expr .GetParts ()[i ],
108
+ },
109
+ ),
110
+ )
111
+ if ! atoms .NumberQ (currN ) {
112
+ return expr
113
+ }
114
+ if lastN != nil && ! int64InSlice (atoms .ExOrder (lastN , currN ), expectedOrders ) {
115
+ return atoms .NewSymbol ("System`False" )
116
+ }
117
+
118
+ lastN = currN
119
+ }
120
+ return atoms .NewSymbol ("System`True" )
121
+ }
122
+
91
123
func getComparisonDefinitions () (defs []Definition ) {
92
124
defs = append (defs , Definition {
93
125
Name : "Equal" ,
@@ -248,36 +280,7 @@ func getComparisonDefinitions() (defs []Definition) {
248
280
)
249
281
},
250
282
legacyEvalFn : func (this expreduceapi.ExpressionInterface , es expreduceapi.EvalStateInterface ) expreduceapi.Ex {
251
- if len (this .GetParts ()) != 3 {
252
- return this
253
- }
254
-
255
- a := es .Eval (
256
- atoms .NewExpression (
257
- []expreduceapi.Ex {
258
- atoms .NewSymbol ("System`N" ),
259
- this .GetParts ()[1 ],
260
- },
261
- ),
262
- )
263
- b := es .Eval (
264
- atoms .NewExpression (
265
- []expreduceapi.Ex {
266
- atoms .NewSymbol ("System`N" ),
267
- this .GetParts ()[2 ],
268
- },
269
- ),
270
- )
271
-
272
- if ! atoms .NumberQ (a ) || ! atoms .NumberQ (b ) {
273
- return this
274
- }
275
-
276
- // Less
277
- if atoms .ExOrder (a , b ) == 1 {
278
- return atoms .NewSymbol ("System`True" )
279
- }
280
- return atoms .NewSymbol ("System`False" )
283
+ return comparisonFn (this , es , []int64 {1 })
281
284
},
282
285
})
283
286
defs = append (defs , Definition {
@@ -294,35 +297,7 @@ func getComparisonDefinitions() (defs []Definition) {
294
297
)
295
298
},
296
299
legacyEvalFn : func (this expreduceapi.ExpressionInterface , es expreduceapi.EvalStateInterface ) expreduceapi.Ex {
297
- if len (this .GetParts ()) != 3 {
298
- return this
299
- }
300
-
301
- a := es .Eval (
302
- atoms .NewExpression (
303
- []expreduceapi.Ex {
304
- atoms .NewSymbol ("System`N" ),
305
- this .GetParts ()[1 ],
306
- },
307
- ),
308
- )
309
- b := es .Eval (
310
- atoms .NewExpression (
311
- []expreduceapi.Ex {
312
- atoms .NewSymbol ("System`N" ),
313
- this .GetParts ()[2 ],
314
- },
315
- ),
316
- )
317
-
318
- if ! atoms .NumberQ (a ) || ! atoms .NumberQ (b ) {
319
- return this
320
- }
321
- // Greater
322
- if atoms .ExOrder (a , b ) == - 1 {
323
- return atoms .NewSymbol ("System`True" )
324
- }
325
- return atoms .NewSymbol ("System`False" )
300
+ return comparisonFn (this , es , []int64 {- 1 })
326
301
},
327
302
})
328
303
defs = append (defs , Definition {
@@ -339,39 +314,7 @@ func getComparisonDefinitions() (defs []Definition) {
339
314
)
340
315
},
341
316
legacyEvalFn : func (this expreduceapi.ExpressionInterface , es expreduceapi.EvalStateInterface ) expreduceapi.Ex {
342
- if len (this .GetParts ()) != 3 {
343
- return this
344
- }
345
-
346
- a := es .Eval (
347
- atoms .NewExpression (
348
- []expreduceapi.Ex {
349
- atoms .NewSymbol ("System`N" ),
350
- this .GetParts ()[1 ],
351
- },
352
- ),
353
- )
354
- b := es .Eval (
355
- atoms .NewExpression (
356
- []expreduceapi.Ex {
357
- atoms .NewSymbol ("System`N" ),
358
- this .GetParts ()[2 ],
359
- },
360
- ),
361
- )
362
-
363
- if ! atoms .NumberQ (a ) || ! atoms .NumberQ (b ) {
364
- return this
365
- }
366
- // Less
367
- if atoms .ExOrder (a , b ) == 1 {
368
- return atoms .NewSymbol ("System`True" )
369
- }
370
- // Equal
371
- if atoms .ExOrder (a , b ) == 0 {
372
- return atoms .NewSymbol ("System`True" )
373
- }
374
- return atoms .NewSymbol ("System`False" )
317
+ return comparisonFn (this , es , []int64 {1 , 0 })
375
318
},
376
319
})
377
320
defs = append (defs , Definition {
@@ -388,39 +331,7 @@ func getComparisonDefinitions() (defs []Definition) {
388
331
)
389
332
},
390
333
legacyEvalFn : func (this expreduceapi.ExpressionInterface , es expreduceapi.EvalStateInterface ) expreduceapi.Ex {
391
- if len (this .GetParts ()) != 3 {
392
- return this
393
- }
394
-
395
- a := es .Eval (
396
- atoms .NewExpression (
397
- []expreduceapi.Ex {
398
- atoms .NewSymbol ("System`N" ),
399
- this .GetParts ()[1 ],
400
- },
401
- ),
402
- )
403
- b := es .Eval (
404
- atoms .NewExpression (
405
- []expreduceapi.Ex {
406
- atoms .NewSymbol ("System`N" ),
407
- this .GetParts ()[2 ],
408
- },
409
- ),
410
- )
411
-
412
- if ! atoms .NumberQ (a ) || ! atoms .NumberQ (b ) {
413
- return this
414
- }
415
- // Greater
416
- if atoms .ExOrder (a , b ) == - 1 {
417
- return atoms .NewSymbol ("System`True" )
418
- }
419
- // Equal
420
- if atoms .ExOrder (a , b ) == 0 {
421
- return atoms .NewSymbol ("System`True" )
422
- }
423
- return atoms .NewSymbol ("System`False" )
334
+ return comparisonFn (this , es , []int64 {- 1 , 0 })
424
335
},
425
336
})
426
337
defs = append (defs , Definition {
0 commit comments