-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds split functions for sequence types including lists, vectors, seqs and strings #1083
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the future it might be nice for this to work on any sequence type with any Eq element type
A few updates on the most recent commit:
|
I'm not 100% convinced this is a class we should include. My main reason is that it seems a little contrived: we wanted something generic so we forced it to be. I think the true generic aspect of something that splittable isn't that it's splittable, but rather that elements have a natural ordering (i.e., they can be indexed) and each element can be accessed in finite time. My recommendation for the direction of this PR is to create a function |
Now this merely defines a function split for string, seq, list, and vector. |
8242ff2
to
298d972
Compare
No urgency, but ready for review whenever the next PR/Issue cleanup happens. 5/10:
|
@@ -207,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)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation needs to be streaming. The current version won't work with infinite iterators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused about this-
I don't believeSeq
can be infinite, so I'm not sure how a seq
could or should generate an infinite/stream Iterator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you mean simply that I should also define split for Iterator
and ensure infinite compatibility?
Add iter split for all containers:
Add split functions for vector, slice (and lisparray if possible) that yield slices. |
@@ -764,5 +780,7 @@ This function is equivalent to all size-N elements of `(COMBS L)`." | |||
(define-instance (Default (List :a)) | |||
(define (default) Nil))) | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some humble requests
@@ -643,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)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we have split-by
which doesn't need Eq, and split
implemented in terms of it?
(declare split (UFix -> String -> (Tuple String String))) | ||
(define (split n str) | ||
"Splits a string into a head and tail at the nth index." | ||
(declare bisect (UFix -> String -> (Tuple String String))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think bisect
has too much meaning in compsci, indicates something is going to be searched. Can we rename this to cleave
?
@@ -134,6 +136,12 @@ does not have that suffix." | |||
(define (chars str) | |||
"Returns an iterator over the characters in `str`." | |||
(iter:into-iter str)) | |||
|
|||
(declare split (Char -> String -> (List String))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split-by
too
(declare split (Char -> String -> (List String))) | ||
(define (split ch str) | ||
"Splits a string with a specified single-character delimeter." | ||
(map (fn (x) (the String (into x))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for this PR, but I'd love the Lisp-equivalent of cl:string
:
(define (string x)
(the String (into x)))
@@ -247,6 +248,24 @@ | |||
iter) | |||
Unit) | |||
|
|||
(declare split ((Eq :a) => :a -> (Vector :a) -> (iter:Iterator (Vector :a)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split-by here too
I'm going to close this for now |
This adds splitting by single-character delimiters