]> gitweb.factorcode.org Git - factor.git/commitdiff
math.combinatorics: make permutations-quot to reduce code duplication.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 6 Apr 2013 20:12:57 +0000 (13:12 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 6 Apr 2013 20:12:57 +0000 (13:12 -0700)
basis/math/combinatorics/combinatorics.factor

index 69f2f2aca5ceb8cdef1ef287c49283ce55eeda29..490d49699200f620c215efb88d22296f6d8ca8d2 100644 (file)
@@ -68,13 +68,18 @@ M: permutations hashcode* tuple-hashcode ;
 
 INSTANCE: permutations immutable-sequence
 
+<PRIVATE
+
+: permutations-quot ( seq quot -- seq quot' )
+    [ [ permutation-iota ] keep ] dip '[ _ permutation @ ] ; inline
+
+PRIVATE>
+
 : each-permutation ( seq quot -- )
-    [ [ permutation-iota ] keep ] dip
-    '[ _ permutation @ ] each ; inline
+    permutations-quot each ; inline
 
 : map-permutations ( seq quot -- seq' )
-    [ [ permutation-iota ] keep ] dip
-    '[ _ permutation @ ] map ; inline
+    permutations-quot map ; inline
 
 : filter-permutations ( seq quot -- seq' )
     selector [ each-permutation ] dip ; inline
@@ -83,9 +88,8 @@ INSTANCE: permutations immutable-sequence
     [ ] map-permutations ;
 
 : find-permutation ( seq quot -- elt )
-    [ dup [ permutation-iota ] keep ] dip
-    '[ _ permutation @ ] find drop
-    [ swap permutation ] [ drop f ] if* ; inline
+    [ permutations-quot find drop ]
+    [ drop over [ permutation ] [ 2drop f ] if ] 2bi ; inline
 
 : reduce-permutations ( seq identity quot -- result )
     swapd each-permutation ; inline