math.combinators: Add filter-permutations/combinations. Minor cleanup.
parent
229d0e0549
commit
0322b4d028
|
@ -94,3 +94,15 @@ IN: math.combinatorics.tests
|
||||||
{ 2 1 1 } { 2 1 2 } { 2 2 1 } { 2 2 2 } } ]
|
{ 2 1 1 } { 2 1 2 } { 2 2 1 } { 2 2 2 } } ]
|
||||||
[ { 1 2 } 3 selections ] unit-test
|
[ { 1 2 } 3 selections ] unit-test
|
||||||
|
|
||||||
|
|
||||||
|
{ V{ { 1 2 } { 1 3 } } }
|
||||||
|
[ { 1 2 3 } 2 [ { 1 } head? ] filter-combinations ] unit-test
|
||||||
|
|
||||||
|
{ { 3 4 5 } }
|
||||||
|
[ { 1 2 3 } 2 [ sum ] map-combinations ] unit-test
|
||||||
|
|
||||||
|
{ V{ { 1 2 3 } { 1 3 2 } } }
|
||||||
|
[ { 1 2 3 } [ { 1 } head? ] filter-permutations ] unit-test
|
||||||
|
|
||||||
|
{ { 6 6 6 6 6 6 } }
|
||||||
|
[ { 1 2 3 } [ sum ] map-permutations ] unit-test
|
||||||
|
|
|
@ -42,23 +42,29 @@ PRIVATE>
|
||||||
: permutation-indices ( n seq -- permutation )
|
: permutation-indices ( n seq -- permutation )
|
||||||
length [ factoradic ] dip 0 pad-head >permutation ;
|
length [ factoradic ] dip 0 pad-head >permutation ;
|
||||||
|
|
||||||
|
: permutation-iota ( seq -- iota )
|
||||||
|
length factorial iota ; inline
|
||||||
|
|
||||||
PRIVATE>
|
PRIVATE>
|
||||||
|
|
||||||
: permutation ( n seq -- seq' )
|
: permutation ( n seq -- seq' )
|
||||||
[ permutation-indices ] keep nths ;
|
[ permutation-indices ] keep nths ;
|
||||||
|
|
||||||
: all-permutations ( seq -- seq' )
|
: all-permutations ( seq -- seq' )
|
||||||
[ length factorial iota ] keep
|
[ permutation-iota ] keep
|
||||||
'[ _ permutation ] map ;
|
'[ _ permutation ] map ;
|
||||||
|
|
||||||
: each-permutation ( seq quot -- )
|
: each-permutation ( seq quot -- )
|
||||||
[ [ length factorial iota ] keep ] dip
|
[ [ permutation-iota ] keep ] dip
|
||||||
'[ _ permutation @ ] each ; inline
|
'[ _ permutation @ ] each ; inline
|
||||||
|
|
||||||
: map-permutation ( seq quot -- )
|
: map-permutations ( seq quot -- seq' )
|
||||||
[ [ length factorial iota ] keep ] dip
|
[ [ permutation-iota ] keep ] dip
|
||||||
'[ _ permutation @ ] map ; inline
|
'[ _ permutation @ ] map ; inline
|
||||||
|
|
||||||
|
: filter-permutations ( seq quot -- seq' )
|
||||||
|
selector [ each-permutation ] dip ; inline
|
||||||
|
|
||||||
: reduce-permutations ( seq identity quot -- result )
|
: reduce-permutations ( seq identity quot -- result )
|
||||||
swapd each-permutation ; inline
|
swapd each-permutation ; inline
|
||||||
|
|
||||||
|
@ -142,6 +148,9 @@ PRIVATE>
|
||||||
: map-combinations ( seq k quot -- )
|
: map-combinations ( seq k quot -- )
|
||||||
combinations-quot map ; inline
|
combinations-quot map ; inline
|
||||||
|
|
||||||
|
: filter-combinations ( seq k quot -- seq' )
|
||||||
|
selector [ each-combination ] dip ; inline
|
||||||
|
|
||||||
: map>assoc-combinations ( seq k quot exemplar -- )
|
: map>assoc-combinations ( seq k quot exemplar -- )
|
||||||
[ combinations-quot ] dip map>assoc ; inline
|
[ combinations-quot ] dip map>assoc ; inline
|
||||||
|
|
||||||
|
@ -169,4 +178,3 @@ PRIVATE>
|
||||||
: selections ( seq n -- selections )
|
: selections ( seq n -- selections )
|
||||||
dup 0 > [ (selections) ] [ 2drop { } ] if ;
|
dup 0 > [ (selections) ] [ 2drop { } ] if ;
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue