]> gitweb.factorcode.org Git - factor.git/commitdiff
grouping.extras: much faster group-map.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 16 Dec 2020 17:46:41 +0000 (09:46 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 16 Dec 2020 17:46:41 +0000 (09:46 -0800)
extra/grouping/extras/extras.factor

index 7ed07da895eaa4ffb9cc0c47d024d65b998ae9eb..4a33665539374f0df91791f5db62d971d78d63f0 100644 (file)
@@ -24,9 +24,15 @@ MACRO:: clump-map-as ( quot exemplar n -- result )
 :: short-groups ( seq n -- seq' )
     seq dup length dup n mod [ drop ] [ - head-slice ] if-zero ;
 
-:: group-map-as ( seq quot exemplar n -- result )
-    seq n short-groups n <groups>
-    [ n firstn quot call ] exemplar map-as ; inline
+MACRO:: group-map-as ( quot exemplar n -- result )
+    n 1 + :> n+1
+    [
+        [ length n /i ] [ '[ _ nth-unsafe ] ] bi
+        [ n <iota> n firstn ] 2dip '[
+            [ _ n napply quot call ] n nkeep
+            [ n + ] n napply n+1 nrot
+        ] exemplar replicate-as n nnip
+    ] ;
 
 : group-map ( seq quot n -- result )
     { } swap group-map-as ; inline