/R6RS-slice

A slicer procedure for Lists, Vectors, Strings and Bytevectors

Primary LanguageSchemeOtherNOASSERTION

# About

This is the R6RS port of Mario Goulart's 'slice' egg for Chicken
Scheme. It provides a procedure (slice) for slicing lists, vectors,
strings and bytevectors. Negative indices may be used for either the
'from' index or the 'to' index.

# Installation

It requires SRFI 1 and SRFI 64 for the tests.

Tests have been ran on
Ikarus 0.0.4-rc1+
Mosh 0.2.5
Larceny 0.97
Racket 5.0

I also hand-tested Ypsilon 0.9.6-trunk/r503 because Ypsilon doesn't
support SRFI 64.


# Documentation

From http://wiki.call-cc.org/eggref/4/slice

[procedure] slice object [from] [to] -> object
Slice the given 'object' returning the slice from 'from' to 'to'.

If 'to' is omitted and 'from' is positive, return the slice from
'from' to the last element of the given 'object'.

If 'to' is omitted and 'from' is negative, return the slice from
'from', assuming 'from' is the counted from the end of 'object', to
the last element of the given 'object'.

Examples:
    (define v '#(1 2 3 4 5 6 7))

    (slice v 0 0)     => #()
    (slice v 1 0))    => #()
    (slice v 0 1))    => #(1)
    (slice v 1 3))    => #(2 3)
    (slice v 10 10))  => #()
    (slice v 0 10))   => #(1 2 3 4 5 6 7)
    (slice v 10 0))   => #()
    (slice v 0))      => #(1 2 3 4 5 6 7)
    (slice v -1))     => #(7)
    (slice v 10))     => #()
    (slice v -10))    => #(1 2 3 4 5 6 7)
    (slice v -4))     => #(4 5 6 7)
    (slice v -4 -4))  => #()
    (slice v -4 -2))  => #(4 5)
    (slice v -4 -10)) => #()
    (slice v -10 -4)) => #(1 2 3)


If 'from' and 'to' are ommited, the 'object' argument is expected to be a procedure to be added to the set of slicers known by slice. The given procedure is an one-argument one which should check for the type of the object it is given and return a slicer procedure, which should accept an 'object', the 'from' and 'to' indexes.

Example:

    (define-record-type custom-string (fields text))
    (define s (make-custom-string "custom string"))

    (slice (lambda (obj)
             (and (custom-string? obj)
                  (lambda (obj from to)
                    (guard (exn
                             (else ""))
                      (substring (custom-string-text obj) from to))))))

    (slice s 0 1) => "c"