]> gitweb.factorcode.org Git - factor.git/commitdiff
add if-zero/when-zero/unless-zero to core/ and update usages
authorDoug Coleman <doug.coleman@gmail.com>
Tue, 11 Aug 2009 22:59:40 +0000 (17:59 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Tue, 11 Aug 2009 22:59:40 +0000 (17:59 -0500)
core/arrays/arrays.factor
core/io/encodings/utf8/utf8.factor
core/math/integers/integers.factor
core/math/parser/parser.factor
core/sequences/sequences-docs.factor
core/sequences/sequences.factor
core/splitting/splitting.factor

index 4a998a1ebb118d7e15a9bcb4f04681ff640d0471..dd70e45b6b15eb485dacb804bad7e5c88fb8ac65 100644 (file)
@@ -14,7 +14,7 @@ M: array resize resize-array ;
 
 M: object new-sequence drop 0 <array> ;
 
-M: f new-sequence drop dup zero? [ drop f ] [ 0 <array> ] if ;
+M: f new-sequence drop [ f ] [ 0 <array> ] if-zero ;
 
 M: array equal?
     over array? [ sequence= ] [ 2drop f ] if ;
index 4846b06f32d29023bbf2d257a24c2554d3852b61..a722655cad4a81dfcecf2e094bab0ae2a23392ad 100755 (executable)
@@ -73,14 +73,14 @@ M: utf8 encode-char
 PRIVATE>
 
 : code-point-length ( n -- x )
-    dup zero? [ drop 1 ] [
+    [ 1 ] [
         log2 {
             { [ dup 0 6 between? ] [ 1 ] }
             { [ dup 7 10 between? ] [ 2 ] }
             { [ dup 11 15 between? ] [ 3 ] }
             { [ dup 16 20 between? ] [ 4 ] }
         } cond nip
-    ] if ;
+    ] if-zero ;
 
 : code-point-offsets ( string -- indices )
     0 [ code-point-length + ] accumulate swap suffix ;
index bb7fc107b2aec2a255f1ba1f048dcd8ff79907b3..2b35ef76fd72a75edde2cb855a90b57f521f52a2 100644 (file)
@@ -121,14 +121,14 @@ M: bignum (log2) bignum-log2 ;
     over zero? [
         2drop 0.0
     ] [
-        dup zero? [
-            2drop 1/0.
+        [
+            drop 1/0.
         ] [
             pre-scale
             /f-loop over odd?
             [ zero? [ 1 + ] unless ] [ drop ] if
             post-scale
-        ] if
+        ] if-zero
     ] if ; inline
 
 M: bignum /f ( m n -- f )
index 437308d53f8f316f5c4c3e2b372630fc283db028..ef8f350e27c9e4870633da2e69e0e2a4228e4214 100644 (file)
@@ -131,7 +131,7 @@ M: ratio >base
     [
         dup 0 < negative? set
         abs 1 /mod
-        [ dup zero? [ drop "" ] [ (>base) sign append ] if ]
+        [ [ "" ] [ (>base) sign append ] if-zero ]
         [
             [ numerator (>base) ]
             [ denominator (>base) ] bi
index 71d42705a2d71f0b98f149e95acc3bd5abd9fd3c..d7db7f5242f8dd3fc2f9df48a2982daad137c731 100755 (executable)
@@ -1214,7 +1214,7 @@ HELP: follow
 { $examples "Get random numbers until zero is reached:"
     { $unchecked-example
     "USING: random sequences prettyprint math ;"
-    "100 [ random dup zero? [ drop f ] when ] follow ."
+    "100 [ random [ f ] when-zero ] follow ."
     "{ 100 86 34 32 24 11 7 2 }"
 } } ;
 
index f0dc6d36c7da928ea0b4920b5afe6be1fb3462a3..2e41d9d2e19d8accc58240a61c04d228dcf5ec63 100755 (executable)
@@ -29,13 +29,27 @@ M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ;
 
 : empty? ( seq -- ? ) length 0 = ; inline
 
+<PRIVATE
+
+: (if-empty) ( seq quot1 quot2 quot3 -- )
+    [ [ drop ] prepose ] [ ] tri* if ; inline
+
+PRIVATE>
+
 : if-empty ( seq quot1 quot2 -- )
-    [ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline
+    [ dup empty? ] (if-empty) ; inline
 
 : when-empty ( seq quot -- ) [ ] if-empty ; inline
 
 : unless-empty ( seq quot -- ) [ ] swap if-empty ; inline
 
+: if-zero ( n quot1 quot2 -- )
+    [ dup zero? ] (if-empty) ; inline
+
+: when-zero ( seq quot -- ) [ ] if-zero ; inline
+
+: unless-zero ( seq quot -- ) [ ] swap if-zero ; inline
+
 : delete-all ( seq -- ) 0 swap set-length ;
 
 : first ( seq -- first ) 0 swap nth ; inline
index 5ec396e5ba6301376bc6f134f5c9581ad0ca8f3d..7aae30f20b356667fab9f1ef25ee456ff7ecc93d 100644 (file)
@@ -58,7 +58,7 @@ PRIVATE>
 : (split) ( separators n seq -- )
     3dup rot [ member? ] curry find-from drop
     [ [ swap subseq , ] 2keep 1 + swap (split) ]
-    [ swap dup zero? [ drop ] [ tail ] if , drop ] if* ; inline recursive
+    [ swap [ tail ] unless-zero , drop ] if* ; inline recursive
 
 : split, ( seq separators -- ) 0 rot (split) ;