source: project/wiki/eggref/4/slice @ 31124

Last change on this file since 31124 was 31124, checked in by Mario Domenech Goulart, 6 years ago

slice (wiki): update documentation for version 1.1

  • Property svnwiki:title set to slice
File size: 3.6 KB
Line 
1== Slice
2
3[[toc:]]
4
5=== Description
6
7This 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.
8
9Since version 1.1, UTF-8 is supported (use the {{slice-utf8}} module).
10
11=== Author
12
13[[/users/mario-domenech-goulart|Mario Domenech Goulart]]
14
15
16=== Procedures
17
18<procedure>(slice object #!optional from to)</procedure>
19
20Slice the given {{object}} returning the slice from {{from}} to {{to}}. 
21
22If {{to}} is ommited and {{from}} is positive, return the slice from {{from}} to the last element of the given {{object}}.
23
24If {{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}}.
25
26Examples:
27
28<enscript highlight=scheme>
29(define v '#(1 2 3 4 5 6 7))
30
31(slice v 0 0)     => #()
32(slice v 1 0))    => #()
33(slice v 0 1))    => #(1)
34(slice v 1 3))    => #(2 3)
35(slice v 10 10))  => #()
36(slice v 0 10))   => #(1 2 3 4 5 6 7)
37(slice v 10 0))   => #()
38(slice v 0))      => #(1 2 3 4 5 6 7)
39(slice v -1))     => #(7)
40(slice v 10))     => #()
41(slice v -10))    => #(1 2 3 4 5 6 7)
42(slice v -4))     => #(4 5 6 7)
43(slice v -4 -4))  => #()
44(slice v -4 -2))  => #(4 5)
45(slice v -4 -10)) => #()
46(slice v -10 -4)) => #(1 2 3)
47</enscript>
48
49
50If {{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.
51
52Example:
53
54<enscript highlight=scheme>
55(define s (make-custom-string "custom string"))
56
57(slice (lambda (obj)
58         (and (custom-string? obj)
59              (lambda (obj from to)
60                (handle-exceptions
61                 exn
62                 ""
63                 (substring (custom-string-text obj) from to))))))
64
65(slice s 0 1) => "c"
66</enscript>
67
68=== Requirements
69
70* [[/egg/utf8|utf8]]
71
72=== License
73
74 Copyright (c) 2010-2014, Mario Domenech Goulart
75 All rights reserved.
76 
77 Redistribution and use in source and binary forms, with or without
78 modification, are permitted provided that the following conditions
79 are met:
80 1. Redistributions of source code must retain the above copyright
81    notice, this list of conditions and the following disclaimer.
82 2. Redistributions in binary form must reproduce the above copyright
83    notice, this list of conditions and the following disclaimer in the
84    documentation and/or other materials provided with the distribution.
85 3. The name of the authors may not be used to endorse or promote products
86    derived from this software without specific prior written permission.
87 
88 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
89 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
90 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
92 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
94 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
95 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
96 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
97 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
98 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99
100
101=== Version history
102
103==== Version 1.1
104* UTF-8 support ({{slice-utf8}} module)
105* Compiled with -O3
106
107==== Version 1.0
108* Initial release
Note: See TracBrowser for help on using the repository browser.