| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> |
| 2 |
<HTML> |
| 3 |
<HEAD> |
| 4 |
<TITLE>Function SPLIT-SEQUENCE, SPLIT-SEQUENCE-IF, SPLIT-SEQUENCE-IF-NOT</TITLE> |
| 5 |
<LINK REL="stylesheet" HREF="style.css" type="text/css"> |
| 6 |
</HEAD> |
| 7 |
<BODY> |
| 8 |
|
| 9 |
<p><p><i>Function</i> <b>SPLIT-SEQUENCE, SPLIT-SEQUENCE-IF, SPLIT-SEQUENCE-IF-NOT</b></p> |
| 10 |
|
| 11 |
<p><p><b>Syntax:</b></p> |
| 12 |
|
| 13 |
<p><p><b>split-sequence</b> <i>delimiter sequence <tt>&key</tt> count remove-empty-subseqs from-end start end test test-not key</i> => <i>list, index</i></p> |
| 14 |
<p><p><b>split-sequence-if</b> <i>predicate sequence <tt>&key</tt> count remove-empty-subseqs from-end start end key</i> => <i>list, index</i></p> |
| 15 |
|
| 16 |
<p><p><b>split-sequence-if-not</b> <i>predicate sequence <tt>&key</tt> count remove-empty-subseqs from-end start end key</i> => <i>list, index</i></p> |
| 17 |
|
| 18 |
<p><p><b>Arguments and Values:</b></p> |
| 19 |
|
| 20 |
<p><p><i>delimiter</i>---an <i>object</i>.</p> |
| 21 |
|
| 22 |
<p><i>predicate</i>---a <i>designator</i> for a <i>function</i> of one <i>argument</i> that returns a <i>generalized boolean</i>.</p> |
| 23 |
<p><i>sequence</i>---a <i>proper sequence</i>.</p> |
| 24 |
|
| 25 |
<p><i>count</i>---an <i>integer</i> or <b>nil</b>. The default is <b>nil</b>.</p> |
| 26 |
<p><i>remove-empty-subseqs</i>---a <i>generalized boolean</i>. The default is <i>false</i>.</p> |
| 27 |
|
| 28 |
<p><i>from-end</i>---a <i>generalized boolean</i>. The default is <i>false</i>.</p> |
| 29 |
<p><i>start, end</i>---<i>bounding index designators</i> of <i>sequence</i>. The defaults for </i>start</i> and <i>end</i> are <tt>0</tt> and <b>nil</b>, respectively.</p> |
| 30 |
|
| 31 |
<p><i>test</i>---a <i>designator</i> for a <i>function</i> of two <i>arguments</i> that returns a <i>generalized boolean</i>.</p> |
| 32 |
<p><i>test-not</i>---a <i>designator</i> for a <i>function</i> of two <i>arguments</i> that returns a <i>generalized boolean</i>.</p> |
| 33 |
|
| 34 |
<p><i>key</i>---a <i>designator</i> for a <i>function</i> of one <i>argument</i>, or <b>nil</b>.</p> |
| 35 |
<p><i>list</i>---a <i>proper sequence</i>.</p> |
| 36 |
|
| 37 |
<p><i>index</i>---an <i>integer</i> greater than or equal to zero, and less than or equal to the <i>length</i> of the <i>sequence</i>.</p> |
| 38 |
|
| 39 |
<p><p><b>Description:</b></p> |
| 40 |
|
| 41 |
<p><p>Splits <i>sequence</i> into a list of subsequences delimited by objects <i>satisfying the test</i>. |
| 42 |
|
| 43 |
|
| 44 |
<p><i>List</i> is a list of sequences of the same kind as <i>sequence</i> that has elements consisting of subsequences of <i>sequence</i> that were delimited in the argument by elements <i>satisfying the test</i>. <i>Index</i> is an index into <i>sequence</i> indicating the unprocessed region, suitable as an argument to <a class="hyperspec" href =" http://www.lispworks.com/documentation/HyperSpec/Body/acc_subseq.html"><b>subseq</b></a> to continue processing in the same manner if desired. |
| 45 |
|
| 46 |
|
| 47 |
<p>The <i>count</i> argument, if supplied, limits the number of subsequences in the first return value; if more than <i>count</i> delimited subsequences exist in <i>sequence</i>, the <i>count</i> leftmost delimited subsequences will be in order in the first return value, and the second return value will be the index into <i>sequence</i> at which processing stopped. |
| 48 |
|
| 49 |
<p>If <i>from-end</i> is non-null, <i>sequence</i> is conceptually processed from right to left, accumulating the subsequences in reverse order; <i>from-end</i> only makes a difference in the case of a non-null <i>count</i> argument. In the presence of <i>from-end</i>, the <i>count</i> rightmost delimited subsequences will be in the order that they are in <i>sequence</i> in the first return value, and the second is the index indicating the end of the unprocessed region. |
| 50 |
|
| 51 |
|
| 52 |
<p>The <i>start</i> and <i>end</i> keyword arguments permit a certain subsequence of the <i>sequence</i> to be processed without the need for a copying stage; their use is conceptually equivalent to partitioning the subsequence delimited by <i>start</i> and <i>end</i>, only without the need for copying. |
| 53 |
|
| 54 |
<p>If <i>remove-empty-subseqs</i> is null (the default), then empty subsequences will be included in the result. |
| 55 |
|
| 56 |
|
| 57 |
<p>In all cases, the subsequences in the first return value will be in the order that they appeared in <i>sequence</i>. |
| 58 |
|
| 59 |
<p><p><b>Examples:</b></p> |
| 60 |
|
| 61 |
<p><pre> |
| 62 |
(split-sequence:SPLIT-SEQUENCE #\Space "A stitch in time saves nine.") |
| 63 |
=> ("A" "stitch" "in" "time" "saves" "nine.") |
| 64 |
28 |
| 65 |
(split-sequence:SPLIT-SEQUENCE #\, "foo,bar ,baz, foobar , barbaz,") |
| 66 |
=> ("foo" "bar " "baz" " foobar " " barbaz" "") |
| 67 |
30 |
| 68 |
</pre> |
| 69 |
|
| 70 |
<p><p><b>Implementation notes:</b></p> |
| 71 |
|
| 72 |
<p>This code was written various people, and the license is |
| 73 |
unknown. Since multiple people worked on it collaboratively and none |
| 74 |
of them seem interested in keeping their intellectual property rights |
| 75 |
to it, I'll assume that it is in the public domain (since the process |
| 76 |
that produced it seems like the very essence of public domain). If |
| 77 |
this is incorrect, please <a href="mailto:[email protected]">contact |
| 78 |
me</a> so we can get it straightened out.</p> |
| 79 |
|
| 80 |
<p>The implementation itself is mature and well tested, and it is |
| 81 |
widely used. The code should be fast enough for most people, but be |
| 82 |
warned: it was written with vectors in mind, with list manipulation as |
| 83 |
an afterthought. It does a lot of things that are quick on vectors but |
| 84 |
slow on lists, and this can result in many orders of magnitude |
| 85 |
slowdown in list benchmarks versus code written for lists. If this is |
| 86 |
a problem for you, it should be straightforward to write your own, |
| 87 |
such as the (more limited, not API compatible) example function given |
| 88 |
by Szymon in <a |
| 89 |
href="http://common-lisp.net/pipermail/cl-utilities-devel/2006-May/000011.html">this |
| 90 |
mailing list post</a>:</p> |
| 91 |
|
| 92 |
<p><pre> |
| 93 |
(defun split-list-if (test list &aux (start list) (end list)) |
| 94 |
(loop while (and end (setq start (member-if-not test end))) |
| 95 |
collect (ldiff start (setq end (member-if test start))))) |
| 96 |
</pre></p> |
| 97 |
|
| 98 |
<p>If this is an issue for enough people, I could optimize the code |
| 99 |
and fix this problem. I'm reluctant to do that, however, since the |
| 100 |
code works and is tested. It's usually more important to be correct |
| 101 |
and non-buggy than to be fast, and I have been known to introduce |
| 102 |
bugs.</p> |
| 103 |
|
| 104 |
<p class="footer"><hr><a href="index.html">Manual Index</a></p> |
| 105 |
|
| 106 |
</body></html> |