} ;
HELP: each-permutation
-{ $values { "seq" sequence } { "quot" { $quotation "( seq -- )" } } }
+{ $values { "seq" sequence } { "quot" { $quotation "( ... elt -- ... )" } } }
{ $description "Applies the quotation to each permutation of " { $snippet "seq" } " in order." } ;
HELP: inverse-permutation
}""" } } ;
HELP: each-combination
-{ $values { "seq" sequence } { "k" "a non-negative integer" } { "quot" { $quotation "( seq -- )" } } }
+{ $values { "seq" sequence } { "k" "a non-negative integer" } { "quot" { $quotation "( ... elt -- ... )" } } }
{ $description "Applies the quotation to each combination of " { $snippet "seq" } " choosing " { $snippet "k" } " elements, in order." } ;
PRIVATE>
-: each-permutation ( seq quot -- )
+: each-permutation ( ... seq quot: ( ... elt -- ... ) -- ... )
permutations-quot each ; inline
-: map-permutations ( seq quot -- seq' )
+: map-permutations ( ... seq quot: ( ... elt -- ... newelt ) -- ... newseq )
permutations-quot map ; inline
-: filter-permutations ( seq quot -- seq' )
+: filter-permutations ( ... seq quot: ( ... elt -- ... ? ) -- ... newseq )
selector [ each-permutation ] dip ; inline
: all-permutations ( seq -- seq' )
[ ] map-permutations ;
-: find-permutation ( seq quot -- elt )
+: find-permutation ( ... seq quot: ( ... elt -- ... ? ) -- ... elt/f )
[ permutations-quot find drop ]
[ drop over [ permutation ] [ 2drop f ] if ] 2bi ; inline
-: reduce-permutations ( seq identity quot -- result )
+: reduce-permutations ( ... seq identity quot: ( ... prev elt -- ... next ) -- ... result )
swapd each-permutation ; inline
: inverse-permutation ( seq -- permutation )
PRIVATE>
-: each-combination ( seq k quot -- )
+: each-combination ( ... seq k quot: ( ... elt -- ... ) -- ... )
combinations-quot each ; inline
-: map-combinations ( seq k quot -- seq' )
+: map-combinations ( ... seq k quot: ( ... elt -- ... newelt ) -- ... newseq )
combinations-quot map ; inline
-: filter-combinations ( seq k quot -- seq' )
+: filter-combinations ( ... seq k quot: ( ... elt -- ... ? ) -- ... newseq )
selector [ each-combination ] dip ; inline
-:: map>assoc-combinations ( seq k quot exemplar -- )
+:: map>assoc-combinations ( ... seq k quot: ( ... elt -- ... key value ) exemplar -- ... assoc )
[ combinations-quot ] dip map>assoc ; inline
: all-combinations ( seq k -- seq' )
[ ] map-combinations ;
-: find-combination ( seq k quot -- elt/f )
+: find-combination ( ... seq k quot: ( ... elt -- ... ? ) -- ... elt/f )
[ combinations-quot find drop ]
[ drop pick [ combination ] [ 3drop f ] if ] 3bi ; inline
-: reduce-combinations ( seq k identity quot -- result )
+: reduce-combinations ( ... seq k identity quot: ( ... prev elt -- next ) -- ... result )
[ -rot ] dip each-combination ; inline
: all-subsets ( seq -- subsets )