Skip to content

Commit

Permalink
Defines a split function for strings, lists, vecs, seqs
Browse files Browse the repository at this point in the history
  • Loading branch information
Izaakwltn committed May 10, 2024
1 parent fe97f1d commit 5f71597
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 119 deletions.
1 change: 0 additions & 1 deletion coalton.asd
Expand Up @@ -165,7 +165,6 @@
(:file "randomaccess")
(:file "cell")
(:file "iterator")
(:file "split")
(:file "optional")
(:file "result")
(:file "tuple")
Expand Down
51 changes: 25 additions & 26 deletions library/list.lisp
Expand Up @@ -10,8 +10,7 @@
(:local-nicknames
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator)
(#:arith #:coalton-library/math/arith)
(#:split #:coalton-library/split))
(#:arith #:coalton-library/math/arith))
(:export
#:head
#:tail
Expand Down Expand Up @@ -637,6 +636,29 @@ This function is equivalent to all size-N elements of `(COMBS L)`."
(map (Cons x) (combsOf (- n 1) xs)) ; combs with X
(combsOf n xs))))))) ; and without x

(declare split ((Eq :a) => :a -> (List :a) -> (iter:Iterator (List :a))))
(define (split delim xs)
(let ((blocks (cell:new Nil))
(current-block (cell:new Nil))
(iter (iter:into-iter xs)))

(iter:for-each! (fn (x)
(cond
((== x delim)
(cell:push! blocks (reverse (cell:read current-block)))
(cell:write! current-block nil)
Unit)
(True
(cell:push! current-block x)
Unit)))
iter)

(unless (null? (cell:read current-block))
(cell:push! blocks (reverse (cell:read current-block)))
Unit)

(iter:into-iter (reverse (cell:read blocks)))))

;;
;; Instances
;;
Expand Down Expand Up @@ -756,30 +778,7 @@ This function is equivalent to all size-N elements of `(COMBS L)`."
((Some a) (Cons a Nil)))))

(define-instance (Default (List :a))
(define (default) Nil))

(define-instance (split:Splittable List)
(define (split:split delim xs)
(let ((blocks (cell:new Nil))
(current-block (cell:new Nil))
(iter (iter:into-iter xs)))

(iter:for-each! (fn (x)
(cond
((== x delim)
(cell:push! blocks (reverse (cell:read current-block)))
(cell:write! current-block nil)
Unit)
(True
(cell:push! current-block x)
Unit)))
iter)

(unless (null? (cell:read current-block))
(cell:push! blocks (reverse (cell:read current-block)))
Unit)

(iter:into-iter (reverse (cell:read blocks)))))))
(define (default) Nil)))



Expand Down
1 change: 0 additions & 1 deletion library/prelude.lisp
Expand Up @@ -256,6 +256,5 @@
(#:hashtable #:coalton-library/hashtable)
(#:st #:coalton-library/monad/state)
(#:iter #:coalton-library/iterator)
(#:split #:coalton-library/split)
(#:sys #:coalton-library/system)))

63 changes: 33 additions & 30 deletions library/seq.lisp
Expand Up @@ -11,8 +11,7 @@
(#:cell #:coalton-library/cell)
(#:vector #:coalton-library/vector)
(#:iter #:coalton-library/iterator)
(#:list #:coalton-library/list)
(#:split #:coalton-library/split))
(#:list #:coalton-library/list))
(:export
#:Seq
#:new
Expand All @@ -23,6 +22,7 @@
#:put
#:empty?
#:conc
#:split
#:make))

(in-package #:coalton-library/seq)
Expand Down Expand Up @@ -62,7 +62,7 @@
UFix ; height
UFix ; cached full subtree size
(vector:Vector UFix) ; cumulative size table
(vector:Vector (Seq :a))) ; subtrees
(vector:Vector (Seq :a))) ; subtrees
(LeafArray (vector:Vector :a)))

(declare new (types:RuntimeRepr :a => Unit -> Seq :a))
Expand Down Expand Up @@ -141,14 +141,14 @@ a new `Seq` instance."
(do
((Tuple leaf newsub) <- (pop (vector:last-unsafe sts)))
(let newsts = (vector:copy sts))
(let newcst = (vector:copy cst))
(let last-idx = (- (vector:length cst) 1))
(let seq-size = (size seq))
(let newcst = (vector:copy cst))
(let last-idx = (- (vector:length cst) 1))
(let seq-size = (size seq))
(pure
(cond
;; this was the only thing left in seq
((== 1 seq-size)
(Tuple leaf newsub)) ; newsub is empty
(Tuple leaf newsub)) ; newsub is empty

;; the seq was exactly one larger than the subtree size
;; for the current height, this means we can reduce the tree height
Expand All @@ -166,7 +166,7 @@ a new `Seq` instance."
(vector:set! last-idx newsub newsts)
(Tuple leaf (RelaxedNode h fss newcst newsts)))))))))

(define (conc left right)
(define (conc left right)
"Concatenate two `Seq`s"
(cond
((empty? left) right)
Expand Down Expand Up @@ -209,6 +209,30 @@ a new `Seq` instance."
(rebalance-branches
(fold <> (butlast lsubts) (make-list nsubts (butfirst rsubts))))))))))))

(declare split ((Eq :a) (types:RuntimeRepr :a) => :a -> (Seq :a) -> (iter:Iterator (Seq :a))))
(define (split delim xs)
(let ((blocks (cell:new Nil))
(current-block (cell:new (new)))
(iter (iter:into-iter xs)))

(iter:for-each!
(fn (x)
(cond
((== x delim)
(cell:push! blocks (cell:read current-block))
(cell:write! current-block (new))
Unit)
(True
(cell:write! current-block (push (cell:read current-block) x))
Unit)))
iter)

(unless (empty? (cell:read current-block))
(cell:push! blocks (cell:read current-block))
Unit)

(iter:into-iter (list:reverse (cell:read blocks)))))

;;
;; Instances
;;
Expand Down Expand Up @@ -255,28 +279,7 @@ a new `Seq` instance."
(iter:zip! (iter:into-iter a)
(iter:into-iter b)))))))

(define-instance (split:Splittable Seq)
(define (split:split delim xs)
(let ((blocks (cell:new Nil))
(current-block (cell:new (new)))
(iter (iter:into-iter xs)))

(iter:for-each! (fn (x)
(cond
((== x delim)
(cell:push! blocks (cell:read current-block))
(cell:write! current-block (new))
Unit)
(True
(cell:write! current-block (push (cell:read current-block) x))
Unit)))
iter)

(unless (empty? (cell:read current-block))
(cell:push! blocks (cell:read current-block))
Unit)

(iter:into-iter (list:reverse (cell:read blocks))))))


;;
;; Helpers
Expand Down
32 changes: 0 additions & 32 deletions library/split.lisp

This file was deleted.

8 changes: 3 additions & 5 deletions library/string.lisp
Expand Up @@ -12,15 +12,13 @@
(:local-nicknames
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator)
(#:list #:coalton-library/list)
(#:split #:coalton-library/split))
(#:list #:coalton-library/list))
(:export
#:concat
#:reverse
#:length
#:substring
#:bisect
#:split
#:strip-prefix
#:strip-suffix
#:parse-int
Expand All @@ -29,7 +27,7 @@
#:substring-index
#:substring?
#:chars
#:delim-split))
#:split))


(in-package #:coalton-library/string)
Expand Down Expand Up @@ -143,7 +141,7 @@ does not have that suffix."
(define (split ch str)
"Splits a string with a specified single-character delimeter."
(map (fn (x) (the String (into x)))
(iter:collect! (split:split ch (the (List Char) (into str))))))
(iter:collect! (list:split ch (the (List Char) (into str))))))

;;
;; Instances
Expand Down
30 changes: 15 additions & 15 deletions library/vector.lisp
Expand Up @@ -9,8 +9,7 @@
(#:list #:coalton-library/list)
(#:cell #:coalton-library/cell)
(#:iter #:coalton-library/iterator)
(#:ram #:coalton-library/randomaccess)
(#:split #:coalton-library/split))
(#:ram #:coalton-library/randomaccess))
(:export
#:Vector
#:new
Expand All @@ -33,6 +32,7 @@
#:last
#:last-unsafe
#:extend!
#:split
#:find-elem
#:append
#:swap-remove!
Expand Down Expand Up @@ -248,6 +248,18 @@
iter)
Unit)

(declare list->vec (List :a -> Vector :a))
(define (list->vec x)
(into x))

(declare vec->list (Vector :a -> List :a))
(define (vec->list x)
(into x))

(declare split ((Eq :a) => :a -> (Vector :a) -> (iter:Iterator (Vector :a))))
(define (split delim v)
(map list->vec (list:split delim (vec->list v))))

;;
;; Instances
;;
Expand Down Expand Up @@ -349,19 +361,7 @@
vec))

(define-instance (Default (Vector :a))
(define default new))

(declare list->vec (List :a -> Vector :a))
(define (list->vec x)
(into x))

(declare vec->list (Vector :a -> List :a))
(define (vec->list x)
(into x))

(define-instance (split:Splittable Vector)
(define (split:split delim v)
(map list->vec (split:split delim (vec->list v))))))
(define default new)))

(cl:defmacro make (cl:&rest elements)
"Construct a `Vector' containing the ELEMENTS, in the order listed."
Expand Down
3 changes: 1 addition & 2 deletions tests/package.lisp
Expand Up @@ -32,8 +32,7 @@
(#:red-black/tree #:coalton-library/ord-tree)
(#:red-black/map #:coalton-library/ord-map)
(#:result #:coalton-library/result)
(#:seq #:coalton-library/seq)
(#:split #:coalton-library/split)))
(#:seq #:coalton-library/seq)))

(in-package #:coalton-native-tests)

Expand Down
14 changes: 7 additions & 7 deletions tests/split-tests.lisp
Expand Up @@ -18,12 +18,12 @@
;;;

(define-test split-list ()
(is (== (iter:collect! (split:split #\. (iter:collect! (string:chars *split-path*)))) *split-path-undotted*))
(is (== (iter:collect! (split:split #\/ (iter:collect! (string:chars *split-path*)))) *split-path-unslashed*))
(is (== (iter:collect! (split:split 2 (make-list 1 2 3 4 2 5 2))) (make-list (make-list 1)
(is (== (iter:collect! (list:split #\. (iter:collect! (string:chars *split-path*)))) *split-path-undotted*))
(is (== (iter:collect! (list:split #\/ (iter:collect! (string:chars *split-path*)))) *split-path-unslashed*))
(is (== (iter:collect! (list:split 2 (make-list 1 2 3 4 2 5 2))) (make-list (make-list 1)
(make-list 3 4)
(make-list 5))))
(is (== (iter:collect! (split:split #\A (iter:collect! (string:chars "BANANA"))))
(is (== (iter:collect! (list:split #\A (iter:collect! (string:chars "BANANA"))))
(make-list (make-list #\B)
(make-list #\N)
(make-list #\N)))))
Expand All @@ -34,9 +34,9 @@


(define-test split-vector ()
(is (== (iter:collect! (split:split #\. (list->vec (iter:collect! (string:chars *split-path*)))))
(is (== (iter:collect! (vector:split #\. (list->vec (iter:collect! (string:chars *split-path*)))))
(map list->vec *split-path-undotted*)))
(is (== (iter:collect! (split:split #\/ (list->vec (iter:collect! (string:chars *split-path*)))))
(is (== (iter:collect! (vector:split #\/ (list->vec (iter:collect! (string:chars *split-path*)))))
(map list->vec *split-path-unslashed*))))

;;;
Expand All @@ -51,6 +51,6 @@

(define-test split-seq ()
(is (== (the (List (seq:Seq Integer))
(iter:collect! (split:split 5 (seq-num-list))))
(iter:collect! (seq:split 5 (seq-num-list))))
(make-list (the (seq:Seq Integer) (iter:collect! (iter:up-to 5)))
(the (seq:Seq Integer) (iter:collect! (map (fn (x) (+ x 6)) (iter:up-to 4))))))))

0 comments on commit 5f71597

Please sign in to comment.