]> gitweb.factorcode.org Git - factor.git/commitdiff
Add cut-all, human-sort to sequences.lib
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 18 Dec 2007 08:37:06 +0000 (02:37 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 18 Dec 2007 08:37:06 +0000 (02:37 -0600)
extra/sequences/lib/lib.factor

index f5adccf445f7f8254136f7b9aefcfc933db020de..ba2fb055e258e014f7c9a32b0faedd974b4411fb 100644 (file)
@@ -1,5 +1,6 @@
 USING: combinators.lib kernel sequences math namespaces assocs 
 random sequences.private shuffle math.functions mirrors ;
+USING: arrays math.parser sorting strings ;
 IN: sequences.lib
 
 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -104,3 +105,20 @@ PRIVATE>
 
 : power-set ( seq -- subsets )
     2 over length exact-number-strings swap [ nths ] curry map ;
+
+: cut-find ( seq pred -- before after )
+    dupd find drop dup [ cut ] when ;
+
+: cut3 ( seq pred -- first mid last )
+    [ cut-find ] keep [ not ] compose cut-find ;
+
+: (cut-all) ( seq pred quot -- )
+    [ >r cut3 r> dip >r >r , r> [ , ] when* r> ] 2keep
+    pick [ (cut-all) ] [ 3drop ] if ;
+
+: cut-all ( seq pred quot -- first mid last )
+    [ (cut-all) ] { } make ;
+
+: human-sort ( seq -- newseq )
+    [ dup [ digit? ] [ string>number ] cut-all ] { } map>assoc
+    sort-values keys ;