-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcollection-utils.dylan
63 lines (52 loc) · 1.96 KB
/
collection-utils.dylan
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module: Collection-Utilities
author: Matthias Hölzl ([email protected])
copyright: see below
version: 0.1 10 Apr 2004
synopsis: This Module implements some useful methods on collections.
// Copyright.
// =========
// Copyright (C) 1998-2004 Dr. Matthias Hölzl.
// Use and copying of this software and preparation of derivative
// works based on this software are permitted, including commercial
// use, provided that the following conditions are observed:
//
// 1. This copyright notice must be retained in full on any copies
// and on appropriate parts of any derivative works. (Other names
// and years may be added, so long as no existing ones are removed.)
//
// This software is made available "as is". Neither the authors nor
// Carnegie Mellon University make any warranty about the software,
// its performance, or its conformity to any specification.
//
// Bug reports, questions, comments, and suggestions should be sent by
// E-mail to the Internet address "[email protected]".
// If you need to receive this library under another license contact
// the author ([email protected]).
// SINGLETON? -- check whether ARG is a collection with a single
// element.
//
define open generic singleton?
(collection :: <collection>) => singleton? :: <boolean>;
define method singleton?
(collection :: <collection>) => singleton? :: <boolean>;
collection.size = 1;
end method singleton?;
define sealed method singleton?
(list :: <pair>) => singleton? :: <boolean>;
empty?(tail(list));
end method singleton?;
define sealed method singleton?
(list :: <empty-list>) => false :: <boolean>;
#f;
end method singleton?;
define constant $not-found = pair(#f, #f);
define method key-exists?
(collection :: <collection>, key)
=> (key-exists? :: <boolean>, value :: <object>);
let result = element(collection, key, default: $not-found);
if (result == $not-found)
values(#f, #f);
else
values(#t, result);
end if;
end method key-exists?;