Slice

  1. Slice
    1. Description
    2. Author
    3. Procedures
    4. Requirements
    5. License
    6. Version history
      1. Version 1.2
      2. Version 1.1
      3. Version 1.0

Description

This extension provides a procedure (slice) for slicing lists, vectors and strings. Slicing is quite flexible: negative indexes may be used for either the from index or for the to index.

Since version 1.1, UTF-8 is supported (use the slice-utf8 module).

Author

Mario Domenech Goulart

Procedures

[procedure] (slice object #!optional from to)

Slice the given object returning the slice from from to to.

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

If to is ommited 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 s (make-custom-string "custom string"))

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

(slice s 0 1) => "c"

Requirements

License

Copyright (c) 2010-2014, Mario Domenech Goulart
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. 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.
3. The name of the authors may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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

Version 1.2

Version 1.1

Version 1.0