chickadee » simple-sequences

Rationale

This module allows to acces some sequence types, in particular lists, pairs, vectors and strings with a common interface. The number of sequence types handled can be enhanced. The egg is not as complete and sophisticated as Felix' sequences egg, but quite usable. In particular, it is sufficient for use in Paul Graham's dbind implementation, cf. On Lisp, p. 232, which is the workhorse of my bindings egg.

API

sequence-db


sequence-dbprocedure
sequence-db seqprocedure
sequence-db seq? seq-length seq-ref seq-tail seq-maker #!rest pos?procedure

database processing: the first resets the database to the standard with lists, pairs, vectors and strings, the second returns the vector of handlers as well as the discriminator, the third adds a new database record either at the end or before the pos? discriminator. A record cosists of a discriminator, seq?, and a vector with items seq-lenth, seq-ref, seq-tail and seq-maker patterned after vectors. Note, that the last record can handle atoms, albeit it is not a sequence.

sequence?


sequence? xprocedure

type predicate.

seq-length


seq-length seqprocedure

returns the length of a sequences

seq-ref


seq-ref seq kprocedure

returns the item of seq at position k

seq-tail


seq-tail seq kprocedure

reuturns the subsequence of seq starting at position k. Note that k might be equal to (seq-length seq), returning the sentinel of empty sequence

seq-maker


seq-maker seqprocedure

returns the maker, like list or vector, to create new sequences of the same type as seq.

seq-nil


seq-nil seqprocedure

returns the sentinel of pairs or empty sequences

seq-pseudo?


seq-pseudo? xprocedure

checks, if the sequence is pseudo, i.e. a pair. This is needed to handle pairs like other sequences.

sequence->list


sequence->list seqprocedure

type transformer

seq-reverse


seq-reverse seqprocedure

returns a new sequence with items reversed

subseq


subseq seq iprocedure
subseq seq i jprocedure

creates a new sequence of the same type as seq consisting of seq's items from i included and j excluded

seq-filter


seq-filter ok? seqprocedure

returns two subsequences of the same type as seq of items passed or not passed by the ok? test

seq-memp


seq-memp ok? seqprocedure

returns a new sequence of seq's type of items from seq starting at the first item which passes the ok? test, or #f

seq-append


seq-append seq #!rest seqsprocedure

appends the items of all argument sequences, which must be of the same type as seq and in case of pairs, the same sentinel

seq-map


seq-map fn seq #!rest seqsprocedure

maps the argument sequences up to the shortest length. All sequences must be of the same type, and in case of pairs, must have the same sentinels

seq-for-each


seq-for-each proc seq #!rest seqsprocedure

applies proc to each item of seq and seqs up to the shortest length. The sequences might be of different type

seq-ref*


seq-ref* seq indprocedure

references the value of a nested sequence at appropriate index list: with index '(0) it returns (seq-ref seq 0), with index '(1 0) it returns (seq-ref (seq-ref seq 1) 0)

seq-flat?


seq-flat? seqprocedure

is the sequence seq flat?

seq-length*


seq-length* seqprocedure

counts the number of items in a nested sequence seq

seq-map*


seq-map* fn seqprocedure

deep map: maps all items of a nested sequence seq with function fn

simple-sequences


simple-sequencesprocedure
simple-sequences symprocedure

with sym: documentation of exported symbol without sym: list of exported symbols

Examples

(import simple-sequences)

(sequence-db '())
;-> (vector length list-ref list-tail list)

(sequence-db #())
;-> (vector vector-length vector-ref subvector vector)

(sequence-db "")
;-> (vector string-length string-ref substring string)

(seq-length '())
;-> 0

(seq-length #())
;-> 0

(seq-length "")
;-> 0

(seq-length '(1 . 2))
;-> 1

(seq-length 1)
;-> 0

(seq-ref '(0 1 2 3) 1)
;-> 1

(seq-ref #(0 1 2 3) 1)
;-> 1

(seq-ref "0123" 1)
;-> 1

(seq-ref '(0 1 2 . 3) 1)
;-> 1

(seq-tail '(0 1 2 3 4) 1)
;-> (quote (1 2 3 4))

(seq-tail #(0 1 2 3 4) 1)
;-> #(1 2 3 4)

(seq-tail "01234" 1)
;-> 1234

(seq-tail '(0 1 2 3 . 4) 1)
;-> (quote (1 2 3 . 4))

(seq-tail '(0 1 2 3 . 4) 4)
;-> 4

(seq-tail 1 0)
;-> 1

(sequence? 1)
;-> #f

(length (sequence-db))
;-> 5

(list-ref
  (sequence-db
    number?
    (lambda (x) 0)
    (lambda (x i) (error))
    (lambda (x i) x)
    (lambda (x) x))
  4)
;-> number?

(car (sequence-db
       integer?
       (lambda (x) 0)
       (lambda (x i) (error))
       (lambda (x i) x)
       (lambda (x) x)
       list?))
;-> integer?

(length (sequence-db))
;-> 5

(condition-case (seq-ref 1 0) ((exn) "out of range"))
;-> out of range

(seq-tail 1 0)
;-> 1

(condition-case (seq-tail 1 1) ((exn) "out of range"))
;-> out of range

(sequence->list '(0 1 2 . 3))
;-> (quote (0 1 2))

(seq-maker str)
;-> string

(subseq str 1)
;-> 123

(subseq str 1 3)
;-> 12

(subseq pls 1 3)
;-> (quote (1 2 . 3))

(seq-nil str)
;-> 

(seq-nil pls)
;-> 3

(seq-pseudo? pls)
;-> #t

(seq-pseudo? lst)
;-> #f

(seq-reverse "abc")
;-> cba

(seq-reverse '(1 2 3 . 0))
;-> (quote (3 2 1 . 0))

(receive (odd even) (seq-filter odd? vec) (list odd even))
;-> (quote (#(1 3) #(0 2)))

(receive (odd even) (seq-filter odd? pls) (list odd even))
;-> (quote ((1 . 3) (0 2 . 3)))

(seq-memp odd? vec)
;-> #(1 2 3)

(seq-memp odd? pls)
;-> (quote (1 2 . 3))

(seq-memp (lambda (s) (char=? s #\2)) str)
;-> 23

(seq-append #(1 2 3) #(10 20 30))
;-> #(1 2 3 10 20 30)

(seq-append '(1 2 3 . 0) '(10 20 30 . 0))
;-> (quote (1 2 3 10 20 30 . 0))

(seq-append "a" "b" "c" "d" "e")
;-> abcde

(seq-map add1 #(0 1 2))
;-> #(1 2 3)

(seq-map add1 '(0 1 2 . 0))
;-> (quote (1 2 3 . 0))

(seq-map + #(1 2 3) #(10 20 30 40))
;-> #(11 22 33)

(seq-map + '(1 2 . 0) '(10 20 . 0))
;-> (quote (11 22 . 0))

(condition-case (seq-map + '(1 2 . 3) '(10 20 . 30)) ((exn) "different nils"))
;-> different nils

(seq-ref* '(1 #(2 3)) '(1 0))
;-> 2

(seq-ref* '(0 #(1 "23")) '(1 1 1))
;-> 3

(seq-ref* '(0 #(1 "23")) '(1 0))
;-> 1

(seq-ref* '(0 #(1 "23")) '(1 1))
;-> 23

(seq-flat? #(1 2 3))
;-> #t

(seq-flat? '(1 #(2 3)))
;-> #f

(seq-length* '((2 (3 #(4) 5) 1) 0))
;-> 6

(seq-length* '(1 #(2 "3")))
;-> 3

(seq-length* '(((((0))))))
;-> 1

(seq-map* add1 '(((2) 1) 0))
;-> (quote (((3) 2) 1))

(seq-map* add1 '(0 #(1 2)))
;-> (quote (1 #(2 3)))

(seq-map* add1 '(0 (1 2 . 3)))
;-> (quote (1 (2 3 . 3)))

(seq-map* add1 '(0 (1 (2 . 3))))
;-> (quote (1 (2 (3 . 3))))

(seq-map* add1 '(0 (1 (2 . 3) . 0)))
;-> (quote (1 (2 (3 . 3) . 0)))

Requirements

None

Last update

Nov 22, 2020

Author

Juergen Lorenz

License

Copyright (c) 2020 , Juergen Lorenz, ju (at) jugilo (dot) de All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version history

1.0
Initial check in

Contents »