Skip to content

Commit

Permalink
Prefer non-polymorphic fixnum and flonum operators
Browse files Browse the repository at this point in the history
  • Loading branch information
anttih committed Feb 19, 2024
1 parent 3fa8779 commit 25c36ce
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 57 deletions.
36 changes: 18 additions & 18 deletions arrays/src/Data/Array.ss
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@

(define rangeImpl
(lambda (start end)
(let* ([step (if (> start end) -1 1)]
(let* ([step (if (fx>? start end) -1 1)]
[result (srfi:214:make-flexvector (+ (* step (- end start)) 1))])
(let recur ([i start]
[n 0])
(if (not (= i end))
(if (not (fx=? i end))
(begin
(srfi:214:flexvector-set! result n i)
(recur (+ i step) (+ n 1)))
(recur (fx+ i step) (fx1+ n)))
(begin
(srfi:214:flexvector-set! result n i)
result))))))

(define replicateImpl
(lambda (count value)
(if (< count 1)
(if (fx<? count 1)
(rt:make-array)
(let ([result (srfi:214:make-flexvector count)])
(srfi:214:flexvector-fill! result value)
Expand All @@ -64,25 +64,25 @@

(define unconsImpl
(lambda (empty next xs)
(if (= (rt:array-length xs) 0)
(if (fx=? (rt:array-length xs) 0)
(empty 'unit)
((next (rt:array-ref xs 0)) (srfi:214:flexvector-copy xs 1)))))

(define indexImpl
(lambda (just nothing xs i)
(if (or (< i 0) (>= i (rt:array-length xs)))
(if (or (fx<? i 0) (fx>=? i (rt:array-length xs)))
nothing
(just (rt:array-ref xs i)))))

(define findMapImpl
(lambda (nothing isJust f xs)
(let ([len (rt:array-length xs)])
(let recur ([i 0])
(if (< i len)
(if (fx<? i len)
(let ([result (f (rt:array-ref xs i))])
(if (isJust result)
result
(recur (+ i 1))))
(recur (fx1+ i))))
nothing)))))

(define findIndexImpl
Expand All @@ -101,23 +101,23 @@

(define _insertAt
(lambda (just nothing i a l)
(if (or (< i 0) (> i (rt:array-length l)))
(if (or (fx<? i 0) (fx>? i (rt:array-length l)))
nothing
(let ([l1 (srfi:214:flexvector-copy l)])
(srfi:214:flexvector-add! l1 i a)
(just l1)))))

(define _deleteAt
(lambda (just nothing i l)
(if (or (< i 0) (>= i (rt:array-length l)))
(if (or (fx<? i 0) (fx>=? i (rt:array-length l)))
nothing
(let ([l1 (srfi:214:flexvector-copy l)])
(srfi:214:flexvector-remove! l1 i)
(just l1)))))

(define _updateAt
(lambda (just nothing i a l)
(if (or (< i 0) (>= i (rt:array-length l)))
(if (or (fx<? i 0) (fx>=? i (rt:array-length l)))
nothing
(let ([l1 (srfi:214:flexvector-copy l)])
(srfi:214:flexvector-set! l1 i a)
Expand Down Expand Up @@ -147,22 +147,22 @@
[out (srfi:214:make-flexvector len)])
(let recur ([i 0]
[acc b])
(if (< i len)
(if (fx<? i len)
(let ([next ((f acc) (rt:array-ref xs i))])
(srfi:214:flexvector-set! out i next)
(recur (+ i 1) next))
(recur (fx1+ i) next))
out)))))

(define scanrImpl
(lambda (f b xs)
(let* ([len (rt:array-length xs)]
[out (srfi:214:make-flexvector len)])
(let recur ([i (- len 1)]
(let recur ([i (fx1- len)]
[acc b])
(if (>= i 0)
(if (fx>=? i 0)
(let ([next ((f (rt:array-ref xs i)) acc)])
(srfi:214:flexvector-set! out i next)
(recur (- i 1) next))
(recur (fx1- i) next))
out)))))

;;------------------------------------------------------------------------------
Expand All @@ -173,7 +173,7 @@
(lambda (compare fromOrdering xs)
(let ([tmp (srfi:214:flexvector->vector xs)])
(vector-sort!
(lambda (x y) (> (fromOrdering ((compare y) x)) 0))
(lambda (x y) (fx>? (fromOrdering ((compare y) x)) 0))
tmp)
(srfi:214:vector->flexvector tmp))))

Expand All @@ -184,7 +184,7 @@

(define sliceImpl
(lambda (s e l)
(if (> s e)
(if (fx>? s e)
(rt:make-array)
(srfi:214:flexvector-copy l s e))))

Expand Down
16 changes: 8 additions & 8 deletions arrays/src/Data/Array/ST.ss
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@

(define pokeImpl
(lambda (i a xs)
(let ([ret (and (>= i 0) (< i (rt:array-length xs)))])
(let ([ret (and (fx>=? i 0) (fx<? i (rt:array-length xs)))])
(if ret (srfi:214:flexvector-set! xs i a))
ret)))

(define peekImpl
(lambda (just nothing i xs)
(if (and (>= i 0) (< i (rt:array-length xs)))
(if (and (fx>=? i 0) (fx<? i (rt:array-length xs)))
(just (rt:array-ref xs i))
nothing)))

(define popImpl
(lambda (just nothing xs)
(if (> (rt:array-length xs) 0)
(if (fx>? (rt:array-length xs) 0)
(just (srfi:214:flexvector-remove-back! xs))
nothing)))

Expand All @@ -50,16 +50,16 @@

(define shiftImpl
(lambda (just nothing xs)
(if (> (rt:array-length xs) 0)
(if (fx>? (rt:array-length xs) 0)
(just (srfi:214:flexvector-remove-front! xs))
nothing)))

(define spliceImpl
(lambda (i howMany bs xs)
(if (> howMany 0)
(if (fx>? howMany 0)
(let ([removed (srfi:214:make-flexvector howMany)])
(srfi:214:flexvector-copy! removed 0 xs i (+ i howMany))
(srfi:214:flexvector-remove-range! xs i (+ i howMany))
(srfi:214:flexvector-copy! removed 0 xs i (fx+ i howMany))
(srfi:214:flexvector-remove-range! xs i (fx+ i howMany))
(srfi:214:flexvector-add-all! xs i (srfi:214:flexvector->list bs))
removed)
(begin
Expand Down Expand Up @@ -89,7 +89,7 @@
(lambda (compare fromOrdering xs)
(let ([tmp (srfi:214:flexvector->vector xs)])
(vector-sort!
(lambda (x y) (> (fromOrdering ((compare y) x)) 0))
(lambda (x y) (fx>? (fromOrdering ((compare y) x)) 0))
tmp)
(srfi:214:flexvector-copy! xs 0 (srfi:214:vector->flexvector tmp)))))

Expand Down
10 changes: 5 additions & 5 deletions foldable-traversable/test/Main.ss
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
arrayReplicate
mkNEArray
foldMap1NEArray)
(import (only (rnrs base) define lambda let if + > <)
(import (chezscheme)
(prefix (purs runtime) rt:)
(prefix (purs runtime srfi :214) srfi:214:))

(define arrayFrom1UpTo
(lambda (n)
(srfi:214:flexvector-map/index
(lambda (i x) (+ i 1))
(lambda (i x) (fx1+ i))
(srfi:214:make-flexvector n))))

(define arrayReplicate
Expand All @@ -23,7 +23,7 @@
(lambda (nothing)
(lambda (just)
(lambda (arr)
(if (> (srfi:214:flexvector-length arr) 0)
(if (fx>? (srfi:214:flexvector-length arr) 0)
(just arr)
nothing)))))

Expand All @@ -34,8 +34,8 @@
(let ([len (srfi:214:flexvector-length arr)])
(let recur ([acc (f (rt:array-ref arr 0))]
[i 1])
(if (< i len)
(recur ((append acc) (f (rt:array-ref arr i))) (+ i 1))
(if (fx<? i len)
(recur ((append acc) (f (rt:array-ref arr i))) (fx1+ i))
acc)))))))

)
6 changes: 3 additions & 3 deletions prelude/src/Control/Bind.ss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(library (Control.Bind foreign)
(export arrayBind)
(import (only (rnrs base) define lambda if = + begin let)
(import (chezscheme)
(prefix (purs runtime) rt:)
(prefix (purs runtime srfi :214) srfi:214:))

Expand All @@ -12,10 +12,10 @@
(let ([len (rt:array-length arr)]
[result (srfi:214:flexvector)])
(let loop ([i 0])
(if (= i len)
(if (fx=? i len)
result
(begin
(srfi:214:flexvector-append! result (f (rt:array-ref arr i)))
(loop (+ i 1)))))))))
(loop (fx1+ i)))))))))

)
4 changes: 2 additions & 2 deletions prelude/src/Data/Eq.ss
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
(define eqIntImpl
(lambda (r1)
(lambda (r2)
(= r1 r2))))
(fx=? r1 r2))))

(define eqNumberImpl
(lambda (r1)
(lambda (r2)
(= r1 r2))))
(fl=? r1 r2))))

(define eqCharImpl
(lambda (r1)
Expand Down
21 changes: 10 additions & 11 deletions prelude/src/Data/Ord.ss
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
ordStringImpl
ordCharImpl
ordArrayImpl)
(import (chezscheme))
(import (only (rnrs base) define lambda)
(import (chezscheme)
(only (purs runtime pstring) pstring<? pstring=?)
(prefix (purs runtime srfi :214) srfi:214:))

Expand All @@ -28,19 +27,19 @@
(lambda (gt)
(lambda (x)
(lambda (y)
(if (< x y)
(if (fx<? x y)
lt
(if (= x y) eq gt))))))))
(if (fx=? x y) eq gt))))))))

(define ordNumberImpl
(lambda (lt)
(lambda (eq)
(lambda (gt)
(lambda (x)
(lambda (y)
(if (< x y)
(if (fl<? x y)
lt
(if (= x y) eq gt))))))))
(if (fl=? x y) eq gt))))))))

(define ordStringImpl
(lambda (lt)
Expand Down Expand Up @@ -70,15 +69,15 @@
[ylen (srfi:214:flexvector-length ys)])
(let loop ([xsi 0]
[ysi 0])
(if (or (= xsi xlen) (= ysi ylen))
(if (or (fx=? xsi xlen) (fx=? ysi ylen))
(cond
[(= xlen ylen) 0]
[(> xlen ylen) -1]
[(fx=? xlen ylen) 0]
[(fx>? xlen ylen) -1]
(else 1))
(let ([o ((f (srfi:214:flexvector-ref xs xsi)) (srfi:214:flexvector-ref ys ysi))])
(if (not (fx=? o 0))
o
(loop
(+ xsi 1)
(+ ysi 1)))))))))))
(fx1+ xsi)
(fx1+ ysi)))))))))))
)
8 changes: 4 additions & 4 deletions prelude/src/Data/Show.ss
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
(define (string-join xs separator)
(let ([len (rt:array-length xs)])
(cond
[(= len 0) (string->pstring "")]
[(= len 1) (rt:array-ref xs 0)]
[(fx=? len 0) (string->pstring "")]
[(fx=? len 1) (rt:array-ref xs 0)]
(else
(let recur ([i 1]
[buffer (rt:array-ref xs 0)])
(if (= len i)
(if (fx=? len i)
buffer
(recur (+ i 1) (pstring-concat buffer separator (rt:array-ref xs i)))))))))
(recur (fx1+ i) (pstring-concat buffer separator (rt:array-ref xs i)))))))))

(define showArrayImpl
(lambda (f)
Expand Down
10 changes: 5 additions & 5 deletions prelude/src/Data/Show/Generic.ss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(library (Data.Show.Generic foreign)
(export intercalate)
(import (only (rnrs base) define lambda if let cond else = +)
(import (chezscheme)
(only (purs runtime pstring) pstring pstring-concat)
(prefix (purs runtime) rt:))

Expand All @@ -11,14 +11,14 @@
(lambda (xs)
(let ([len (rt:array-length xs)])
(cond
[(= len 0) (pstring)]
[(= len 1) (rt:array-ref xs 0)]
[(fx=? len 0) (pstring)]
[(fx=? len 1) (rt:array-ref xs 0)]
(else
(let recur ([i 1]
[buffer (rt:array-ref xs 0)])
(if (= len i)
(if (fx=? len i)
buffer
(recur (+ i 1)
(recur (fx1+ i)
(pstring-concat buffer separator (rt:array-ref xs i)))))))))))

)
2 changes: 1 addition & 1 deletion st/src/Control/Monad/ST/Internal.ss
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
(lambda (hi)
(lambda (f)
(lambda ()
(do ([i lo (+ i 1)]) ((= i hi) 'unit)
(do ([i lo (fx1+ i)]) ((fx=? i hi) 'unit)
((f i))))))))

(define foreach
Expand Down

0 comments on commit 25c36ce

Please sign in to comment.