math.combinatorics: adding find-combination and find-permutation.

db4
John Benediktsson 2012-04-20 23:31:40 -07:00
parent 0650495414
commit bada993f78
2 changed files with 12 additions and 1 deletions

View File

@ -106,3 +106,7 @@ IN: math.combinatorics.tests
{ { 6 6 6 6 6 6 } }
[ { 1 2 3 } [ sum ] map-permutations ] unit-test
{ { 2 3 } } [ { 1 2 3 } 2 [ first 2 = ] find-combination ] unit-test
{ { 2 1 3 } } [ { 1 2 3 } [ first 2 = ] find-permutation ] unit-test

View File

@ -65,6 +65,10 @@ PRIVATE>
: filter-permutations ( seq quot -- seq' )
selector [ each-permutation ] dip ; inline
: find-permutation ( seq quot -- elt )
[ dup [ permutation-iota ] keep ] dip
'[ _ permutation @ ] find drop swap permutation ; inline
: reduce-permutations ( seq identity quot -- result )
swapd each-permutation ; inline
@ -145,7 +149,7 @@ PRIVATE>
: each-combination ( seq k quot -- )
combinations-quot each ; inline
: map-combinations ( seq k quot -- )
: map-combinations ( seq k quot -- seq' )
combinations-quot map ; inline
: filter-combinations ( seq k quot -- seq' )
@ -160,6 +164,9 @@ PRIVATE>
: all-combinations ( seq k -- seq' )
[ ] map-combinations ;
: find-combination ( seq k quot -- i elt )
[ combinations-quot find drop ] [ drop combination ] 3bi ; inline
: reduce-combinations ( seq k identity quot -- result )
[ -rot ] dip each-combination ; inline