assocs: add effect declarations to assoc combinators
parent
3544568fe8
commit
b4867f4a1c
|
@ -202,7 +202,7 @@ HELP: new-assoc
|
|||
{ $contract "Creates a new assoc of the same size as " { $snippet "exemplar" } " which can hold " { $snippet "capacity" } " entries before growing." } ;
|
||||
|
||||
HELP: assoc-find
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" "a boolean" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "key" "the successful key, or f" } { "value" "the successful value, or f" } { "?" "a boolean" } }
|
||||
{ $description "Applies a predicate quotation to each entry in the assoc. Returns the key and value that the quotation succeeds on, or " { $link f } " for both if the quotation fails. It also returns a boolean describing whether there was anything found; this can be used to distinguish between a key and a value equal to " { $link f } ", or nothing being found." } ;
|
||||
|
||||
HELP: clear-assoc
|
||||
|
@ -242,7 +242,7 @@ HELP: ?at
|
|||
{ $description "Looks up the value associated with a key. If the key was not present, an error can be thrown without extra stack shuffling. This word handles assocs that store " { $link f } "." } ;
|
||||
|
||||
HELP: assoc-each
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- )" } } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... )" } } }
|
||||
{ $description "Applies a quotation to each entry in the assoc." }
|
||||
{ $examples
|
||||
{ $example
|
||||
|
@ -254,7 +254,7 @@ HELP: assoc-each
|
|||
} ;
|
||||
|
||||
HELP: assoc-map
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- newkey newvalue )" } } { "newassoc" "a new assoc" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... newkey newvalue )" } } { "newassoc" "a new assoc" } }
|
||||
{ $description "Applies the quotation to each entry in the input assoc and collects the results in a new assoc of the same type as the input." }
|
||||
{ $examples
|
||||
{ $unchecked-example
|
||||
|
@ -269,15 +269,15 @@ HELP: assoc-map
|
|||
{ assoc-map assoc-map-as } related-words
|
||||
|
||||
HELP: assoc-filter
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "subassoc" "a new assoc" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "subassoc" "a new assoc" } }
|
||||
{ $description "Outputs an assoc of the same type as " { $snippet "assoc" } " consisting of all entries for which the predicate quotation yields true." } ;
|
||||
|
||||
HELP: assoc-filter-as
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "exemplar" assoc } { "subassoc" "a new assoc" } }
|
||||
{ $description "Outputs an assoc of the same type as " { $snippet "exemplar" } " consisting of all entries for which the predicate quotation yields true." } ;
|
||||
|
||||
HELP: assoc-filter!
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } }
|
||||
{ $description "Removes all entries for which the predicate quotation yields true." }
|
||||
{ $side-effects "assoc" } ;
|
||||
|
||||
|
@ -291,11 +291,11 @@ HELP: assoc-partition
|
|||
{ $description "Calls a predicate quotation on each key of the input assoc. If the test yields true, the key/value pair is added to " { $snippet "true-assoc" } "; if false, it's added to " { $snippet "false-assoc" } "." } ;
|
||||
|
||||
HELP: assoc-any?
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "?" "a boolean" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "?" "a boolean" } }
|
||||
{ $description "Tests if the assoc contains an entry satisfying a predicate by applying the quotation to each entry in turn. Iteration stops if an entry is found for which the quotation outputs a true value." } ;
|
||||
|
||||
HELP: assoc-all?
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- ? )" } } { "?" "a boolean" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... ? )" } } { "?" "a boolean" } }
|
||||
{ $description "Tests if all entries in the assoc satisfy a predicate by applying the quotation to each entry in turn. a predicate quotation to entry in the assoc. Iteration stops if an entry is found for which the quotation outputs " { $link f } ". If the assoc is empty, always outputs " { $link t } "." } ;
|
||||
|
||||
HELP: assoc-subset?
|
||||
|
@ -378,25 +378,25 @@ HELP: substitute
|
|||
{ $description "Creates a new sequence where elements of " { $snippet "seq" } " which appear as keys in " { $snippet "assoc" } " are replaced by the corresponding values, and all other elements are unchanged." } ;
|
||||
|
||||
HELP: cache
|
||||
{ $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation "( key -- value )" } } { "value" "a previously-retained or freshly-computed value" } }
|
||||
{ $values { "key" "a key" } { "assoc" assoc } { "quot" { $quotation "( ... key -- ... value )" } } { "value" "a previously-retained or freshly-computed value" } }
|
||||
{ $description "If the key is present in the assoc, outputs the associated value, otherwise calls the quotation to produce a value and stores the key/value pair into the assoc. Returns a value either looked up or newly stored in the assoc." }
|
||||
{ $side-effects "assoc" } ;
|
||||
|
||||
HELP: 2cache
|
||||
{ $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation "( key1 key2 -- value )" } } { "value" "a previously-retained or freshly-computed value" } }
|
||||
{ $values { "key1" "a key" } { "key2" "a key" } { "assoc" assoc } { "quot" { $quotation "( ... key1 key2 -- ... value )" } } { "value" "a previously-retained or freshly-computed value" } }
|
||||
{ $description "If a single key composed of the input keys is present in the assoc, outputs the associated value, otherwise calls the quotation to produce a value and stores the keys/value pair into the assoc. Returns the value stored in the assoc. Returns a value either looked up or newly stored in the assoc." }
|
||||
{ $side-effects "assoc" } ;
|
||||
|
||||
HELP: map>assoc
|
||||
{ $values { "seq" "a sequence" } { "quot" { $quotation "( elt -- key value )" } } { "exemplar" assoc } { "assoc" "a new assoc" } }
|
||||
{ $values { "seq" "a sequence" } { "quot" { $quotation "( ... elt -- ... key value )" } } { "exemplar" assoc } { "assoc" "a new assoc" } }
|
||||
{ $description "Applies the quotation to each element of the sequence, and collects the keys and values into a new assoc having the same type as " { $snippet "exemplar" } "." } ;
|
||||
|
||||
HELP: assoc>map
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( key value -- elt )" } } { "exemplar" "a sequence" } { "seq" "a new sequence" } }
|
||||
{ $values { "assoc" assoc } { "quot" { $quotation "( ... key value -- ... elt )" } } { "exemplar" "a sequence" } { "seq" "a new sequence" } }
|
||||
{ $description "Applies the quotation to each entry of the assoc and collects the results into a new sequence of the same type as the exemplar." } ;
|
||||
|
||||
HELP: change-at
|
||||
{ $values { "key" object } { "assoc" assoc } { "quot" { $quotation "( value -- newvalue )" } } }
|
||||
{ $values { "key" object } { "assoc" assoc } { "quot" { $quotation "( ..a value -- ..b newvalue )" } } }
|
||||
{ $description "Applies the quotation to the value associated with " { $snippet "key" } ", storing the new value back in the assoc." }
|
||||
{ $side-effects "assoc" } ;
|
||||
|
||||
|
@ -432,7 +432,7 @@ HELP: assoc-combine
|
|||
|
||||
HELP: assoc-map-as
|
||||
{ $values
|
||||
{ "assoc" assoc } { "quot" { $quotation "( key value -- newkey newvalue )" } } { "exemplar" assoc }
|
||||
{ "assoc" assoc } { "quot" { $quotation "( ... key value -- ... newkey newvalue )" } } { "exemplar" assoc }
|
||||
{ "newassoc" assoc } }
|
||||
{ $description "Applies the quotation to each entry in the input assoc and collects the results in a new assoc of the stame type as the exemplar." }
|
||||
{ $examples { $example "USING: prettyprint assocs hashtables math ;" " H{ { 1 2 } { 3 4 } } [ sq ] { } assoc-map-as ." "{ { 1 4 } { 3 16 } }" } } ;
|
||||
|
|
|
@ -49,43 +49,43 @@ M: assoc assoc-like drop ; inline
|
|||
|
||||
PRIVATE>
|
||||
|
||||
: assoc-find ( assoc quot -- key value ? )
|
||||
: assoc-find ( ... assoc quot: ( ... key value -- ... ? ) -- ... key value ? )
|
||||
(assoc-each) find swap [ first2 t ] [ drop f f f ] if ; inline
|
||||
|
||||
: key? ( key assoc -- ? ) at* nip ; inline
|
||||
|
||||
: assoc-each ( assoc quot -- )
|
||||
: assoc-each ( ... assoc quot: ( ... key value -- ... ) -- ... )
|
||||
(assoc-each) each ; inline
|
||||
|
||||
: assoc>map ( assoc quot exemplar -- seq )
|
||||
: assoc>map ( ... assoc quot: ( ... key value -- ... elt ) exemplar -- ... seq )
|
||||
[ collector-for [ assoc-each ] dip ] [ like ] bi ; inline
|
||||
|
||||
: assoc-map-as ( assoc quot exemplar -- newassoc )
|
||||
: assoc-map-as ( ... assoc quot: ( ... key value -- ... newkey newvalue ) exemplar -- ... newassoc )
|
||||
[ [ 2array ] compose V{ } assoc>map ] dip assoc-like ; inline
|
||||
|
||||
: assoc-map ( assoc quot -- newassoc )
|
||||
: assoc-map ( ... assoc quot: ( ... key value -- ... newkey newvalue ) -- ... newassoc )
|
||||
over assoc-map-as ; inline
|
||||
|
||||
: assoc-filter-as ( assoc quot exemplar -- subassoc )
|
||||
: assoc-filter-as ( ... assoc quot: ( ... key value -- ... ? ) exemplar -- ... subassoc )
|
||||
[ (assoc-each) filter ] dip assoc-like ; inline
|
||||
|
||||
: assoc-filter ( assoc quot -- subassoc )
|
||||
: assoc-filter ( ... assoc quot: ( ... key value -- ... ? ) -- ... subassoc )
|
||||
over assoc-filter-as ; inline
|
||||
|
||||
: assoc-filter! ( assoc quot -- assoc )
|
||||
: assoc-filter! ( ... assoc quot: ( ... key value -- ... ? ) -- ... assoc )
|
||||
[
|
||||
over [ [ [ drop ] 2bi ] dip [ delete-at ] 2curry unless ] 2curry
|
||||
assoc-each
|
||||
] [ drop ] 2bi ; inline
|
||||
|
||||
: assoc-partition ( assoc quot -- true-assoc false-assoc )
|
||||
: assoc-partition ( ... assoc quot: ( ... key value -- ... ? ) -- ... true-assoc false-assoc )
|
||||
[ (assoc-each) partition ] [ drop ] 2bi
|
||||
[ assoc-like ] curry bi@ ; inline
|
||||
|
||||
: assoc-any? ( assoc quot -- ? )
|
||||
: assoc-any? ( ... assoc quot: ( ... key value -- ... ? ) -- ... ? )
|
||||
assoc-find 2nip ; inline
|
||||
|
||||
: assoc-all? ( assoc quot -- ? )
|
||||
: assoc-all? ( ... assoc quot: ( ... key value -- ... ? ) -- ... ? )
|
||||
[ not ] compose assoc-any? not ; inline
|
||||
|
||||
: at ( key assoc -- value/f )
|
||||
|
@ -150,23 +150,23 @@ M: assoc assoc-clone-like ( assoc exemplar -- newassoc )
|
|||
: substitute ( seq assoc -- newseq )
|
||||
substituter map ;
|
||||
|
||||
: cache ( key assoc quot -- value )
|
||||
: cache ( ... key assoc quot: ( ... key -- ... value ) -- ... value )
|
||||
[ [ at* ] 2keep ] dip
|
||||
[ [ nip call dup ] [ drop ] 3bi set-at ] 3curry
|
||||
[ drop ] prepose
|
||||
unless ; inline
|
||||
|
||||
: 2cache ( key1 key2 assoc quot -- value )
|
||||
: 2cache ( ... key1 key2 assoc quot: ( ... key1 key2 -- ... value ) -- ... value )
|
||||
[ 2array ] 2dip [ first2 ] prepose cache ; inline
|
||||
|
||||
: change-at ( key assoc quot -- )
|
||||
: change-at ( ..a key assoc quot: ( ..a value -- ..b newvalue ) -- ..b )
|
||||
[ [ at ] dip call ] [ drop ] 3bi set-at ; inline
|
||||
|
||||
: at+ ( n key assoc -- ) [ 0 or + ] change-at ; inline
|
||||
|
||||
: inc-at ( key assoc -- ) [ 1 ] 2dip at+ ; inline
|
||||
|
||||
: map>assoc ( seq quot exemplar -- assoc )
|
||||
: map>assoc ( ... seq quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
|
||||
[ [ 2array ] compose { } map-as ] dip assoc-like ; inline
|
||||
|
||||
: extract-keys ( seq assoc -- subassoc )
|
||||
|
|
Loading…
Reference in New Issue