split-sequence

2021-05-31

Splits a sequence into a list of subsequences delimited by objects satisfying a test.

Upstream URL

github.com/sharplispers/split-sequence

Author

Arthur Lemmens <alemmens@xs4all.nl>

Maintainer

Sharp Lispers <sharplispers@googlegroups.com>

License

MIT
README

SPLIT-SEQUENCE

SPLIT-SEQUENCE is a member of the Common Lisp Utilities family of programs, designed by community consensus.

Function SPLIT-SEQUENCE, SPLIT-SEQUENCE-IF, SPLIT-SEQUENCE-IF-NOT

Syntax:

split-sequence delimiter sequence &key count remove-empty-subseqs from-end start end test test-not key ⇒ list, index

split-sequence-if predicate sequence &key count remove-empty-subseqs from-end start end key ⇒ list, index

split-sequence-if-not predicate sequence &key count remove-empty-subseqs from-end start end key ⇒ list, index

Arguments and Values:

delimiter—an object.

predicate—a designator for a function of one argument that returns a generalized boolean.

sequence—a proper sequence.

count—an integer or nil. The default is nil.

remove-empty-subseqs—a generalized boolean. The default is false.

from-end—a generalized boolean. The default is false.

start, endbounding index designators of sequence. The defaults for start and end are 0 and nil, respectively.

test—a designator for a function of two arguments that returns a generalized boolean.

test-not—a designator for a function of two arguments that returns a generalized boolean.

key—a designator for a function of one argument, or nil.

list—a proper sequence.

index—an integer greater than or equal to zero, and less than or equal to the length of the sequence.

Description:

Splits sequence into a list of subsequences delimited by objects satisfying the test.

List is a list of sequences of the same kind as sequence that has elements consisting of subsequences of sequence that were delimited in the argument by elements satisfying the test. Index is an index into sequence indicating the unprocessed region, suitable as an argument to subseq to continue processing in the same manner if desired.

The count argument, if supplied, limits the number of subsequences in the first return value; if more than count delimited subsequences exist in sequence, the count leftmost delimited subsequences will be in order in the first return value, and the second return value will be the index into sequence at which processing stopped.

If from-end is non-null, sequence is conceptually processed from right to left, accumulating the subsequences in reverse order; from-end only makes a difference in the case of a non-null count argument. In the presence of from-end, the count rightmost delimited subsequences will be in the order that they are in sequence in the first return value, and the second is the index indicating the end of the unprocessed region.

The start and end keyword arguments permit a certain subsequence of the sequence to be processed without the need for a copying stage; their use is conceptually equivalent to partitioning the subsequence delimited by start and end, only without the need for copying.

If remove-empty-subseqs is null (the default), then empty subsequences will be included in the result.

In all cases, the subsequences in the first return value will be in the order that they appeared in sequence.

Examples:

SPLIT-SEQUENCE> (split-sequence #\Space "A stitch in time saves nine.")
⇒ ("A" "stitch" "in" "time" "saves" "nine.")
⇒ 28

SPLIT-SEQUENCE> (split-sequence #\, "foo,bar ,baz, foobar , barbaz,")
⇒ ("foo" "bar " "baz" " foobar " " barbaz" "")
⇒ 30

Dependencies (1)

  • fiveam
  • GitHub
  • Quicklisp