diff --git a/library/collections/classes.lisp b/library/collections/classes.lisp index 2fbdbe8b..4fe62b98 100644 --- a/library/collections/classes.lisp +++ b/library/collections/classes.lisp @@ -38,6 +38,9 @@ #:head# #:last #:last# + #:tail + #:take + #:drop #:index-elt #:index-elt# #:index-where @@ -178,6 +181,8 @@ the front or back, depending on which is natural for the underlying data structu ;; TODO: Make it so that these must all be the proper KeyedCollection types as well (coalton-toplevel + ;; NOTE: In all cases, LinearCollection methods should return collections that don't + ;; share mutable state with the original. (define-class (LinearCollection :m) ;; Get elements from the collection (head @@ -192,6 +197,15 @@ the front or back, depending on which is natural for the underlying data structu (last# "Return the last element of the collection, erroring if it does not exist." (:m :a -> :a)) + (tail + "Return all except the first element of the collection." + (:m :a -> :m :a)) + (take + "Return the first `n` elements of the collection." + (UFix -> :m :a -> :m :a)) + (drop + "Return all except the first `n` elements of the collection." + (UFix -> :m :a -> :m :a)) ;; Query the collection (index-elt "Return the index of the first occurence of `elt`, if it can be found." @@ -209,7 +223,6 @@ the front or back, depending on which is natural for the underlying data structu "Return the first element matching a predicate function." ((:a -> Boolean) -> :m :a -> Optional :a)) ;; Retrieve subsets of the collection. - ;; NOTE: In all cases, these should return collections that don't share mutable state with the original. (subseq "Extract the collection from `start` (inclusive) to `end` (exclusive)." (UFix -> UFix -> :m :a -> :m :a)) @@ -330,6 +343,9 @@ with that element. The second collection is empty if no element satisfied `pred` (define last l:last) (define (last# lst) (o:from-some "Attempted to retrieve last element of empty list." (l:last lst))) + (define tail l:tail) + (define take l:take) + (define drop l:drop) (define index-elt l:elemIndex) (define (index-elt# elt lst) (o:from-some "Cannot find element in list." (l:elemIndex elt lst))) diff --git a/library/collections/immutable/list.lisp b/library/collections/immutable/list.lisp index 2366e127..bdff1018 100644 --- a/library/collections/immutable/list.lisp +++ b/library/collections/immutable/list.lisp @@ -104,12 +104,12 @@ ((Cons x _) (Some x)) ((Nil) None))) - (declare tail (List :a -> Optional (List :a))) + (declare tail (List :a -> List :a)) (define (tail l) "Returns every element except the first in a list." (match l - ((Cons _ xs) (Some xs)) - ((Nil) None))) + ((Cons _ xs) xs) + ((Nil) Nil))) (declare car (List :a -> :a)) (define (car x) diff --git a/library/collections/mutable/vector.lisp b/library/collections/mutable/vector.lisp index 76f17857..82731682 100644 --- a/library/collections/mutable/vector.lisp +++ b/library/collections/mutable/vector.lisp @@ -364,8 +364,8 @@ (let ((end-point (min end (length vec))) (new-length (- end-point start)) (new-vec (with-capacity new-length))) - (for i in (iter:up-to new-length) - (set! i (index-unsafe (+ start i) vec) new-vec)) + (for i in (iter:range-increasing 1 start end-point) + (push! (index-unsafe i vec) new-vec)) new-vec))) (declare split-at-vec (UFix -> Vector :a -> Tuple (Vector :a) (Vector :a))) @@ -569,6 +569,12 @@ (define cln:head# head-unsafe) (define cln:last last) (define cln:last# last-unsafe) + (define (cln:tail vec) + (subseq-vec 1 (length vec) vec)) + (define (cln:drop n vec) + (subseq-vec n (length vec) vec)) + (define (cln:take n vec) + (subseq-vec 0 n vec)) (define cln:index-elt find-elem) (define (cln:index-elt# elt vec) (opt:from-some "Cannot find element in vector." (find-elem elt vec))) diff --git a/library/prelude.lisp b/library/prelude.lisp index 3a3b6041..0a8ca95d 100644 --- a/library/prelude.lisp +++ b/library/prelude.lisp @@ -167,6 +167,9 @@ #:head# #:last #:last# + #:tail + #:take + #:drop #:index-elt #:index-elt# #:index-where @@ -225,6 +228,9 @@ #:head# #:last #:last# + #:tail + #:take + #:drop #:index-elt #:index-elt# #:index-where @@ -251,11 +257,8 @@ (:import-from #:coalton-library/collections/immutable/list - #:tail #:singleton #:repeat - #:drop - #:take #:find #:fold #:foldr @@ -269,11 +272,8 @@ #:any #:split) (:export - #:tail #:singleton #:repeat - #:drop - #:take #:find #:fold #:foldr