Skip to content

Commit

Permalink
Added tail, take, and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Walker committed Jan 3, 2025
1 parent b003000 commit c4ce79a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
18 changes: 17 additions & 1 deletion library/collections/classes.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#:head#
#:last
#:last#
#:tail
#:take
#:drop
#:index-elt
#:index-elt#
#:index-where
Expand Down Expand Up @@ -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
Expand All @@ -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."
Expand All @@ -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))
Expand Down Expand Up @@ -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)))
Expand Down
6 changes: 3 additions & 3 deletions library/collections/immutable/list.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions library/collections/mutable/vector.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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)))
Expand Down
12 changes: 6 additions & 6 deletions library/prelude.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
#:head#
#:last
#:last#
#:tail
#:take
#:drop
#:index-elt
#:index-elt#
#:index-where
Expand Down Expand Up @@ -225,6 +228,9 @@
#:head#
#:last
#:last#
#:tail
#:take
#:drop
#:index-elt
#:index-elt#
#:index-where
Expand All @@ -251,11 +257,8 @@

(:import-from
#:coalton-library/collections/immutable/list
#:tail
#:singleton
#:repeat
#:drop
#:take
#:find
#:fold
#:foldr
Expand All @@ -269,11 +272,8 @@
#:any
#:split)
(:export
#:tail
#:singleton
#:repeat
#:drop
#:take
#:find
#:fold
#:foldr
Expand Down

0 comments on commit c4ce79a

Please sign in to comment.