]> gitweb.factorcode.org Git - factor.git/commitdiff
math.combinatorics: use nth-unsafe more.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 6 Apr 2013 19:43:02 +0000 (12:43 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 6 Apr 2013 19:43:02 +0000 (12:43 -0700)
basis/math/combinatorics/combinatorics.factor

index 83c02a618bace6ce38ccfda790df2b6ead6eec97..c9109eb63df81bfef9f8dbed302b0cf68b019b9c 100644 (file)
@@ -49,10 +49,13 @@ MEMO: factorial ( n -- n! )
 : permutation-iota ( seq -- iota )
     length factorial iota ; inline
 
+: nths-unsafe ( indices seq -- seq' )
+    [ [ nth-unsafe ] curry ] keep map-as ;
+
 PRIVATE>
 
 : permutation ( n seq -- seq' )
-    [ permutation-indices ] keep nths ;
+    [ permutation-indices ] keep nths-unsafe ;
 
 TUPLE: permutations length seq ;
 
@@ -139,13 +142,13 @@ PRIVATE>
 :: combinations-quot ( seq k quot -- seq quot )
     seq length :> n
     n k nCk iota [
-        k n combination-indices seq nths quot call
+        k n combination-indices seq nths-unsafe quot call
     ] ; inline
 
 PRIVATE>
 
 : combination ( m seq k -- seq' )
-    swap [ length combination-indices ] [ nths ] bi ;
+    swap [ length combination-indices ] [ nths-unsafe ] bi ;
 
 TUPLE: combinations seq k length ;