Skip to content

Commit

Permalink
Fix range implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
anttih committed Oct 27, 2023
1 parent 9610b4c commit 16055f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/Data/Array.ss
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
allImpl
unsafeIndexImpl
)
(import (only (rnrs base) define lambda let error + -)
(import (only (rnrs base) define lambda begin let let* cond if not * + - = > error)
(prefix (purs runtime lib) rt:)
(prefix (purs runtime srfi :214) srfi:214:))

Expand All @@ -34,9 +34,17 @@

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

(define replicateImpl
(lambda (count value)
Expand Down
4 changes: 2 additions & 2 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Effect (Effect)
import Effect.Console (log)
import Partial.Unsafe (unsafePartial)
import Test.Assert (assert)
import Test.Data.UndefinedOr (defined, undefined)
-- import Test.Data.UndefinedOr (defined, undefined)

testArray :: Effect Unit
testArray = do
Expand Down Expand Up @@ -297,7 +297,7 @@ testArray = do

log "sort should reorder a list into ascending order based on the result of compare"
assert $ A.sort [1, 3, 2, 5, 6, 4] == [1, 2, 3, 4, 5, 6]
assert $ A.sort [defined 1, undefined, defined 2] == [undefined, defined 1, defined 2]
-- assert $ A.sort [defined 1, undefined, defined 2] == [undefined, defined 1, defined 2]

log "sortBy should reorder a list into ascending order based on the result of a comparison function"
assert $ A.sortBy (flip compare) [1, 3, 2, 5, 6, 4] == [6, 5, 4, 3, 2, 1]
Expand Down
4 changes: 2 additions & 2 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module Test.Main where
import Prelude

import Effect (Effect)
-- import Test.Data.Array (testArray)
import Test.Data.Array (testArray)
import Test.Data.Array.Partial (testArrayPartial)
import Test.Data.Array.ST (testArrayST)
import Test.Data.Array.ST.Partial (testArraySTPartial)
-- import Test.Data.Array.NonEmpty (testNonEmptyArray)

main :: Effect Unit
main = do
-- testArray
testArray
testArrayST
testArrayPartial
testArraySTPartial
Expand Down

0 comments on commit 16055f2

Please sign in to comment.