]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: count is [ ] count-by, readd seq-copy-loop again
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 6 Dec 2022 03:36:40 +0000 (21:36 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 26 Feb 2023 23:11:03 +0000 (17:11 -0600)
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index cb81cc16da1c2329fc4ba26b46cd23312fc874a0..2c44ca94d3dc3c3d5af9863b6651ae00f96c1f16 100644 (file)
@@ -302,8 +302,10 @@ unit-test
 
 { 328350 } [ 100 <iota> [ sq ] map-sum ] unit-test
 
-{ 50 } [ 100 <iota> [ even? ] count ] unit-test
-{ 50 } [ 100 <iota> [ odd?  ] count ] unit-test
+{ 5 } [ { 1 f 3 f 5 f 7 f 9 f } count ] unit-test
+
+{ 50 } [ 100 <iota> [ even? ] count-by ] unit-test
+{ 50 } [ 100 <iota> [ odd?  ] count-by ] unit-test
 
 { { "b" "d" } } [ { 1 3 } { "a" "b" "c" "d" } nths ] unit-test
 { { "a" "b" "c" "d" } } [ { 0 1 2 3 } { "a" "b" "c" "d" } nths ] unit-test
index 7db35f65f9e9538efeede1aa71c99edf37d56a8e..4d5bc5219e77095ff603a1ba15a1dab95d796947 100644 (file)
@@ -364,6 +364,16 @@ C: <copier> copier
 : subseq-unsafe ( from to seq -- subseq )
     dup subseq-unsafe-as ; inline
 
+: seq-copy-loop ( dst dst-i src src-i src-stop -- dst )
+    2dup >= [
+        4drop
+    ] [
+        [
+            [ copy-nth-of-unsafe ] 4keep
+            [ 1 + ] 2dip 1 +
+        ] dip seq-copy-loop
+    ] if ; inline recursive
+
 PRIVATE>
 
 : subseq-as ( from to seq exemplar -- subseq )
@@ -732,6 +742,9 @@ PRIVATE>
 : nths ( indices seq -- seq' )
     [ [ nth ] curry ] keep map-as ;
 
+: nths-of ( seq indices -- seq' )
+    swap nths ; inline
+
 : any? ( ... seq quot: ( ... elt -- ... ? ) -- ... ? )
     find drop >boolean ; inline
 
@@ -1168,12 +1181,12 @@ M: repetition sum [ elt>> ] [ length>> ] bi * ; inline
 : map-sum ( ... seq quot: ( ... elt -- ... n ) -- ... n )
     [ 0 ] 2dip [ dip + ] with-assoc each ; inline
 
-: count ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
-    [ 1 0 ? ] compose map-sum ; inline
-
 : count-by ( ... seq quot: ( ... elt -- ... ? ) -- ... n )
     [ 1 0 ? ] compose map-sum ; inline
 
+: count ( ... seq -- ... n )
+    [ ] count-by ; inline
+
 : cartesian-each ( ... seq1 seq2 quot: ( ... elt1 elt2 -- ... ) -- ... )
     [ with each ] 2curry each ; inline