{ { 6 6 6 6 6 6 } }
[ { 1 2 3 } [ sum ] map-permutations ] unit-test
+{ f } [ { 1 2 3 } 2 [ last 4 = ] find-combination ] unit-test
{ { 2 3 } } [ { 1 2 3 } 2 [ first 2 = ] find-combination ] unit-test
+{ f } [ { 1 2 3 } [ last 4 = ] find-permutation ] unit-test
{ { 2 1 3 } } [ { 1 2 3 } [ first 2 = ] find-permutation ] unit-test
: find-permutation ( seq quot -- elt )
[ dup [ permutation-iota ] keep ] dip
- '[ _ permutation @ ] find drop swap permutation ; inline
+ '[ _ permutation @ ] find drop
+ [ swap permutation ] [ drop f ] if* ; inline
: reduce-permutations ( seq identity quot -- result )
swapd each-permutation ; inline
PRIVATE>
+: combination ( m seq k -- seq' )
+ <combo> apply-combination ;
+
: each-combination ( seq k quot -- )
combinations-quot each ; inline
: map>assoc-combinations ( seq k quot exemplar -- )
[ combinations-quot ] dip map>assoc ; inline
-: combination ( m seq k -- seq' )
- <combo> apply-combination ;
-
: all-combinations ( seq k -- seq' )
[ ] map-combinations ;
: find-combination ( seq k quot -- i elt )
- [ combinations-quot find drop ] [ drop combination ] 3bi ; inline
+ [ combinations-quot find drop ]
+ [ drop pick [ combination ] [ 3drop f ] if ] 3bi ; inline
: reduce-combinations ( seq k identity quot -- result )
[ -rot ] dip each-combination ; inline