diff options
| author | 2021-06-26 16:09:01 -0400 | |
|---|---|---|
| committer | 2021-06-26 16:09:01 -0400 | |
| commit | 4123e2ab37ab300c9c5158fd78f89363c167f46c (patch) | |
| tree | 147f6fa832db2b1b1ee7eafb211f0701a2c96f58 /srfi-225.html | |
| parent | adding srfi-225.html (diff) | |
formatting; register-dictionary-type
Diffstat (limited to 'srfi-225.html')
| -rw-r--r-- | srfi-225.html | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/srfi-225.html b/srfi-225.html index 1865d8d..c6abf0a 100644 --- a/srfi-225.html +++ b/srfi-225.html @@ -45,58 +45,58 @@ None at present. <h3 id="predicates">Predicates</h3> <p><code>(dictionary?</code> <em>obj</em><code>)</code></p> <p>Returns <code>#t</code> if <em>obj</em> answers <code>#t</code> to some registered predicate, and <code>#f</code> otherwise.</p> -<pre><code>(define dict '((1 . 2) (3 . 4) (5 . 6))) -(dictionary? dict) => #t</code></pre> +<blockquote><pre>(define dict '((1 . 2) (3 . 4) (5 . 6))) +(dictionary? dict) => #t</code></blockquote> <p><code>(dict-empty?</code> <em>dictionary</em><code>)</code></p> <p>Returns <code>#t</code> if <em>dictionary</em> contains no associations and <code>#f</code> if it does contain associations.</p> -<pre><code>(dict-empty? '()) => #t -(dict-empty? dict) => #f</code></pre> +<blockquote><pre>(dict-empty? '()) => #t +(dict-empty? dict) => #f</code></blockquote> <p><code>(dict-contains?</code> <em>dictionary key</em><code>)</code></p> -<pre><code>(dict-contains? dict 1) => #t -(dict-contains? dict 2) => #f</code></pre> +<blockquote><pre>(dict-contains? dict 1) => #t +(dict-contains? dict 2) => #f</code></blockquote> <p>Returns <code>#t</code>if one of the keys of <em>dictionary</em> is the same as <em>key</em> and <code>#f</code> otherwise.</p> <h3 id="lookup">Lookup</h3> <p><code>(dict-ref</code> <em>dictionary key</em> [<em>failure</em> [<em>success</em>] ]<code>)</code></p> <p>If <em>key</em> is the same as some key of <em>dictionary</em>, then invokes <em>success</em> on the corresponding value and returns its result. If <em>key</em> is not a key of <em>dictionary</em>, then invokes the thunk <em>failure</em> and returns its result. The default value of <em>failure</em> signals an error; the default value of <em>success</em> is the identity procedure.</p> -<pre><code>(dict-ref dict 1 (lambda () '() list) => (1) ; success wraps value in a list -(dict-ref dict 2 (lambda () '() list)) => () ; failure returns empty list</code></pre> +<blockquote><pre>(dict-ref dict 1 (lambda () '() list) => (1) ; success wraps value in a list +(dict-ref dict 2 (lambda () '() list)) => () ; failure returns empty list</code></blockquote> <p><code>(dict-ref/default</code> <em>dictionary key default</em><code>)</code></p> <p>If <em>key</em> is the same as some key of <em>dictionary</em> then returns the corresponding value. If not, then returns <em>default</em>.</p> -<pre><code>(dict-ref/default dict 1 #f) => 1 -(dict-ref/default dict 1 #f) => #f</code></pre> +<blockquote><pre>(dict-ref/default dict 1 #f) => 1 +(dict-ref/default dict 1 #f) => #f</code></blockquote> <h3 id="mutation">Mutation</h3> <p>All these procedures are linear-update: that is, they may return a new dictionary object (which may or may not share storage with the <em>dictionary</em> argument), or the same dictionary object, mutated. In either case, it is an error to access the dictionary later through any other reference to it, as that reference may have been invalidated.</p> <p><code>(dict-set!</code> <em>dictionary obj</em> …<code>)</code></p> <p>Returns a dictionary that contains all the associations of <em>dictionary</em> plus those specified by <em>objs</em>, which alternate between keys and values. If a key to be added already exists in <em>dictionary</em>, the new value prevails.</p> -<pre><code>; alists are changed non-destructively +<blockquote><pre>; alists are changed non-destructively (dict-set! dict 7 8) => ((7 . 8) (1 . 2) (3 . 4) (5 . 6)) -(dict-set! dict 3 5) => ((1 . 2) (3 . 5) (5 . 6) ; may share last alist entry</code></pre> +(dict-set! dict 3 5) => ((1 . 2) (3 . 5) (5 . 6) ; may share last alist entry</code></blockquote> <p><code>(dict-adjoin!</code>dictionary obj …<code>)</code></p> <p>Returns a dictionary that contains all the associations of <em>dictionary</em> plus those specified by <em>objs</em>, which alternate between keys and values. If a key to be added already exists in <em>dictionary</em>, the old value prevails.</p> -<pre><code>; alists are changed non-destructively +<blockquote><pre>; alists are changed non-destructively (dict-adjoin! dict 7 8) => ((7 . 8) (1 . 2) (3 . 4) (5 . 6)) -(dict-adjoin! dict 3 5) => ((1 . 2) (3 . 5) (5 . 6) ; may share last alist entry</code></pre> +(dict-adjoin! dict 3 5) => ((1 . 2) (3 . 5) (5 . 6) ; may share last alist entry</code></blockquote> <p><code>(dict-delete!</code> <em>dictionary key</em> …<code>)</code></p> <p>Returns a dictionary that contains all the associations of <em>dictionary</em> except those whose keys are the same as one of the <em>keys</em>.</p> -<pre><code>; alists are changed non-destructively +<blockquote><pre>; alists are changed non-destructively (dict-delete! dict 1 3) => ((7 . 8) (1 . 2) (3 . 4) (5 . 6)) -(dict-delete! dict 3 5) => ((1 . 2) (3 . 4) (5 . 6) ; may share whole alist</code></pre> +(dict-delete! dict 3 5) => ((1 . 2) (3 . 4) (5 . 6) ; may share whole alist</code></blockquote> <p><code>(dict-delete-all!</code> <em>dictionary keylist</em><code>)</code></p> <p>Returns a dictionary with all the associations of <em>dictionary</em> except those whose keys are the same as some member of <em>keylist</em>.</p> -<pre><code>(dict-delete-all! dict '(1 3)) => ((5 . 6))</code></pre> +<blockquote><pre>(dict-delete-all! dict '(1 3)) => ((5 . 6))</code></blockquote> <p><code>(dict-replace!</code> <em>dictionary key value</em><code>)</code></p> <p>Returns a dictionary that contains all the associations of <em>dictionary</em> except as follows: If <em>key</em> is the same as a key of <em>dictionary</em>, then the association for that key is omitted and replaced by the association defined by the pair <em>key</em> and <em>value</em>. If there is no such key in <em>dictionary</em>, then dictionary is returned unchanged.</p> -<pre><code>(dict-replace! dict 1 3) => ((1 . 3) (3 . 4) (5 . 6)) -(dict-replace! dict 2 3) => ((1 . 2) (3 . 4) (5 . 6))</code></pre> +<blockquote><pre>(dict-replace! dict 1 3) => ((1 . 3) (3 . 4) (5 . 6)) +(dict-replace! dict 2 3) => ((1 . 2) (3 . 4) (5 . 6))</code></blockquote> <p><code>(dict-intern!</code>dictionary key failure<code>)</code></p> <p>Extracts the value associated with the key in <em>dictionary</em> that is the same as <em>key</em>, and returns two values, <em>dictionary</em> and the value. If <em>key</em> is not the same as any key in <em>dictionary</em>, <em>failure</em> is invoked on no arguments.</p> <p>The procedure then returns two values, a dictionary that contains all the associations of <em>dictionary</em> and in addition a new association that maps <em>key</em> to the result of invoking <em>failure</em>, and the result of invoking <em>failure</em>.</p> -<pre><code>(dict-intern! dict 1 (lambda () #f)) => ; 2 values +<blockquote><pre>(dict-intern! dict 1 (lambda () #f)) => ; 2 values ((1 . 2) (3 . 4) (5 . 6)) 3 (dict-intern! dict 2 (lambda () #f)) => ; 2 values ((2 . #f) (1 . 2) (3 . 4) (5 . 6)) - #f</code></pre> + #f</code></blockquote> <p><code>(dict-update!</code> <em>dictionary key updater</em> [<em>failure</em> [<em>success</em>] ]<code>)</code></p> <p>Retrieves the value of <em>key</em> as if by <code>dict-ref</code>, invokes <em>updater</em> on it, and sets the value of <em>key</em> to be the result of calling <em>updater</em> as if by <code>dict-set</code>, but may do so more efficiently. Returns the updated dictionary. The default value of <em>failure</em> signals an error; the default value of <em>success</em> is the identity procedure.</p> <p><code>(dict-update/default!</code> <em>dictionary key updater default</em><code>)</code></p> @@ -104,19 +104,19 @@ None at present. <p><code>(dict-pop!</code> <em>dictionary</em><code>)</code></p> <p>Chooses an association from <em>dictionary</em> and returns three values: a dictionary that contains all associations of <em>dictionary</em> except the chosen one, and the key and the value of the chosen association. If the dictionary is ordered, the first association is chosen; otherwise the chosen association is arbitrary.</p> <p>If dictionary contains no associations and <em>failure</em> is supplied, it is an error.</p> -<pre><code>(dict-pop! dict) => # 3 values +<blockquote><pre>(dict-pop! dict) => # 3 values ((3 . 4) (5 . 6)) 1 - 2</code></pre> + 2</code></blockquote> <p><code>(dict-map!</code> <em>proc dictionary</em><code>)</code></p> <p>Returns a dictionary similar to <em>dictionary</em> that maps each key of <em>dictionary</em> to the value that results from invoking <em>proc</em> on the corresponding key and value of <em>dictionary</em>.</p> -<pre><code>(dict-map! (lambda (k v) (cons v k)) dict) => ((2 . 1) (4 . 3) (6 . 5))</code></pre> +<blockquote><pre>(dict-map! (lambda (k v) (cons v k)) dict) => ((2 . 1) (4 . 3) (6 . 5))</code></blockquote> <p><code>(dict-filter!</code> <em>pred dictionary</em><code>)</code></p> <p>Returns a dictionary similar to <em>dictionary</em> that contains just the associations of <em>dictionary</em> that satisfy <em>pred</em> when it is invoked on the key and value of the association.</p> -<pre><code>(dict-filter (lambda (x) (= x 1)) dict) => ((1 . 2))</code></pre> +<blockquote><pre>(dict-filter (lambda (x) (= x 1)) dict) => ((1 . 2))</code></blockquote> <p><code>(dict-remove!</code> <em>pred dictionary</em><code>)</code></p> <p>Returns a dictionary that contains all the associations of <em>dictionary</em> except those that satisfy <em>pred</em> when called on the key and value.</p> -<pre><code>(dict-remove (lambda (x) (= x 1)) dict) => ((3 . 4) (5 . 6))</code></pre> +<blockquote><pre>(dict-remove (lambda (x) (= x 1)) dict) => ((3 . 4) (5 . 6))</code></blockquote> <p><code>(dict-search!</code> <em>dictionary key failure success</em><code>)</code></p> <p>This procedure is a workhorse for dictionary lookup, insert, and delete. The dictionary <em>dictionary</em> is searched for an association whose key is the same as <em>key</em> in the sense of <em>dictionary</em>’s comparator. If one is not found, then the <em>failure</em> procedure is tail-called with two continuation arguments, <em>insert</em> and <em>ignore</em>, and is expected to tail-call one of them.</p> <p>However, if such an association is found, then the <em>success</em> procedure is tail-called with the matching key of <em>dictionary</em>, the associated value, and two continuation arguments, <em>update</em> and <em>remove</em>, and is expected to tail-call one of them.</p> @@ -132,52 +132,65 @@ None at present. <h3 id="the-whole-dictionary">The whole dictionary</h3> <p><code>(dict-size</code> <em>dictionary</em><code>)</code></p> <p>Returns an exact integer representing the number of associations in <em>dictionary</em>.</p> -<pre><code>(dict-size dict) => 0</code></pre> +<blockquote><pre>(dict-size dict) => 0</code></blockquote> <p><code>(dict-for-each</code> <em>proc dictionary</em><code>)</code></p> <p>Invokes <em>proc</em> on each key of <em>dictionary</em> and its corresponding value in that order. This procedure is used for doing operations on the whole dictionary. If the dictionary type is inherently ordered, associations are processed in the inherent order; otherwise in an arbitrary order. Returns an unspecified value.</p> -<pre><code>(dict-for-each write dict) => unspecified - ; writes "135" to current output</code></pre> +<blockquote><pre>(dict-for-each write dict) => unspecified + ; writes "135" to current output</code></blockquote> <p><code>(dict-count</code> <em>pred dictionary</em><code>)</code></p> <p>Passes each association of dictionary as two arguments to <em>pred</em> and returns the number of times that <em>pred</em> returned true as an an exact integer.</p> -<pre><code>(dict-count dict (lambda (k v) (even? k) => 0</code></pre> +<blockquote><pre>(dict-count dict (lambda (k v) (even? k) => 0</code></blockquote> <p><code>(dict-any</code> <em>pred dictionary</em><code>)</code></p> <p>Passes each association of <em>dictionary</em> as two arguments to <em>pred</em> and returns the value of the first call to <em>pred</em> that returns true, after which no further calls are made. If the dictionary type is inherently ordered, associations are processed in the inherent order; otherwise in an arbitrary order. If all calls return false, <code>dict-any</code> returns false.</p> -<pre><code>(define (both-even? k v) (and (even? k) (even? v)) +<blockquote><pre>(define (both-even? k v) (and (even? k) (even? v)) (dict-any both-even? '((2 . 4) (3 . 5))) => #t -(dict-any both-even? '((1 . 2) (3 . 4))) => #f</code></pre> +(dict-any both-even? '((1 . 2) (3 . 4))) => #f</code></blockquote> <p><code>(dict-every</code> <em>pred dictionary</em><code>)</code></p> <p>Passes each association of <em>dictionary</em> as two arguments to <em>pred</em> and returns <code>#f</code> after the first call to <em>pred</em> that returns false after which no further calls are made. If the dictionary type is inherently ordered, associations are processed in the inherent order; otherwise in an arbitrary order. If all calls return true, <code>dict-any</code> returns the value of the last call, or <code>#t</code> if no calls are made.</p> -<pre><code>(define (some-even? k v) (or (even? k) (even? v)) +<blockquote><pre>(define (some-even? k v) (or (even? k) (even? v)) (dict-every some-even? '((2 . 3) (3 . 4))) => #t -(dict-every some-even? '((1 . 3) (3 . 4))) => #f</code></pre> +(dict-every some-even? '((1 . 3) (3 . 4))) => #f</code></blockquote> <p><code>(dict-keys</code> <em>dictionary</em><code>)</code></p> <p>Returns a list of the keys of <em>dictionary</em>. If the dictionary type is inherently ordered, associations are processed in the inherent order; otherwise in an arbitrary order. The order may change when new elements are added to <em>dictionary</em>.</p> -<pre><code>(dict-keys dict) => (1 3 5)</code></pre> +<blockquote><pre>(dict-keys dict) => (1 3 5)</code></blockquote> <p><code>(dict-values</code> <em>dictionary</em><code>)</code></p> <p>Returns a list of the values of <em>dictionary</em>. The results returned by <code>dict-keys</code> and <code>dict-values</code> are ordered consistently.</p> -<pre><code>(dict-values dict) => (2 4 6)</code></pre> +<blockquote><pre>(dict-values dict) => (2 4 6)</code></blockquote> <p><code>(dict-entries</code> <em>dictionary</em><code>)</code></p> <p>Returns two values, the result of calling <code>dict-keys</code> and the result of calling <code>dict-values</code> on <em>dictionary</em>.</p> -<pre><code>(dict-entries dict) => ; 2 values +<blockquote><pre>(dict-entries dict) => ; 2 values (1 3 5) - (2 4 6)</code></pre> + (2 4 6)</code></blockquote> <p><code>(dict-fold</code> <em>proc knil dictionary</em><code>)</code></p> <p>Invokes <em>proc</em> on each association of <em>dictionary</em> with three arguments: the key of the association, the value of the association, and an accumulated result of the previous invocation. For the first invocation, <em>knil</em> is used as the third argument. Returns the result of the last invocation, or <em>knil</em> if there was no invocation.</p> -<pre><code>(dict-fold + 0 '((1 . 2) (3 . 4))) => 10</code></pre> +<blockquote><pre>(dict-fold + 0 '((1 . 2) (3 . 4))) => 10</code></blockquote> <p><code>(dict-map->list</code> <em>proc dictionary</em><code>)</code></p> <p>Returns a list of values that result from invoking <em>proc</em> on the keys and corresponding values of <em>dictionary</em>.</p> -<pre><code>(dict-map->list - dict) => (-1 -1 -1)</code></pre> +<blockquote><pre>(dict-map->list - dict) => (-1 -1 -1)</code></blockquote> <p><code>(dict->alist</code> <em>dictionary</em><code>)</code></p> <p>Returns an alist whose keys and values are the keys and values of <em>dictionary</em>.</p> -<pre><code>; plist to alist -(dict->alist '(1 2 3 4 5 6)) => ((1 . 2) (3 . 4) (5 . 6))</code></pre> +<blockquote><pre>; plist to alist +(dict->alist '(1 2 3 4 5 6)) => ((1 . 2) (3 . 4) (5 . 6))</code></blockquote> <h3 id="registering-dictionary-types">Registering dictionary types</h3> <p>The following procedure registers new dictionary types. It is an error to register a dictionary type whose instances return <code>#t</code> to any predicate already registered.</p> -<p><code>(register-dictionary!</code> <em>arg</em> …<code>)</code></p> +<p><code>(register-dictionary-type!</code> <em>arg</em> …<code>)</code></p> <p>Registers a new dictionary type, providing procedures that allow manipulation of dictionaries of that type. The <em>args</em> are alternately <em>procnames</em> and corresponding <em>procs</em>.</p> -<p>A <em>procname</em> argument is a symbol which is the same as one of the procedures defined in this SRFI (other than <code>register-dictionary!</code> itself), and a <em>proc</em> argument is the specific procedure implementing it for this type. These procedures only need to handle the full argument list when defining <code>dict-ref</code> and <code>dict-update!</code>, as the defaults have already been supplied by the framework.</p> +<p>A <em>procname</em> argument is a symbol which is the same as one of the procedures defined in this SRFI (other than <code>register-dictionary-type!</code> itself), and a <em>proc</em> argument is the specific procedure implementing it for this type. These procedures only need to handle the full argument list when defining <code>dict-ref</code> and <code>dict-update!</code>, as the other defaults have already been supplied by the framework.</p> <p>Arguments for the six procedures <code>dictionary?</code>, <code>dict-size</code>, <code>dict-search!</code>, <code>dict-map!</code>, <code>dict-filter!</code>, and <code>dict-for-each</code> are required. The others are optional, but if provided can be more efficient than the versions automatically provided by the implementation of this SRFI.</p> +<p>The following example is from the file <code>alist-impl.scm</code> +in the sample implementation; the procedures referred to are also in +that file.<p> +<blockquote><pre> + (register-dictionary-type! + 'dictionary? alist? + 'dict-map! alist-map! + 'dict-filter! alist-filter! + 'dict-search! alist-search! + 'dict-size alist-size + 'dict-for-each alist-foreach + 'dict->alist alist->alist)) +</code></blockquote> <h2 id="implementation">Implementation</h2> <p>The sample implementation is found in the GitHub repository.</p> |
