]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: adding ?last for symmetry.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 20:04:24 +0000 (13:04 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 20:04:24 +0000 (13:04 -0700)
basis/compiler/cfg/ssa/construction/construction.factor
basis/farkup/farkup.factor
core/sequences/sequences-docs.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor

index 4f156f38d64e131c784bc07eb2b74cd0b77d1fe4..bb1c20df9f7cd0caba0b7c336076a0ae2da18efd 100644 (file)
@@ -92,7 +92,7 @@ SYMBOLS: stacks pushed ;
     if ;
 
 : (top-name) ( vreg -- vreg' )
-    stacks get at [ f ] [ last ] if-empty ;
+    stacks get at ?last ;
 
 : top-name ( vreg -- vreg' )
     (top-name)
index b4e5a2b981e674df616da3770c685a2436452887..9473ccedfb66f757e729ebc257c58391f1aabdb5 100644 (file)
@@ -150,9 +150,6 @@ DEFER: (parse-paragraph)
     rest
     dup last empty? [ but-last ] when ;
 
-: ?last ( seq -- elt/f )
-    [ f ] [ last ] if-empty ;
-
 : coalesce ( rows -- rows' )
     V{ } clone [
         '[
index 05365baf453461d43f876fa21a1868eeb0428a3c..5c68a55873e96c669c56604d05032c9136b81cfd 100644 (file)
@@ -187,6 +187,10 @@ HELP: ?second
 { $values { "seq" sequence } { "elt/f" "an object or " { $link f } } }
 { $description "A forgiving version of " { $link second } ". If the sequence has less than two elements, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
 
+HELP: ?last
+{ $values { "seq" sequence } { "elt/f" "an object or " { $link f } } }
+{ $description "A forgiving version of " { $link last } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
+
 HELP: nth-unsafe
 { $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
 { $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
index c912fa346dcf61d96a5dbbe0a7f0f21f2f2b85e8..37e03ff9d206152b7be0c431bb2e5129cf9a4985 100644 (file)
@@ -245,6 +245,10 @@ unit-test
 [ f ] [ { } ?first ] unit-test
 [ 0 ] [ 10 iota ?first ] unit-test
 
+[ f ] [ f ?last ] unit-test
+[ f ] [ { } ?last ] unit-test
+[ 9 ] [ 10 iota ?last ] unit-test
+
 [ -1/0. 0 remove-nth! ] must-fail
 [ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
 [ "" ] [ "" [ CHAR: \s = ] trim-head ] unit-test
index 0971982e53c20b5ed6ab8970789b9cf9776093e4..32cbce114f04dfbe4c3026f3934be7f7044fb382 100644 (file)
@@ -177,6 +177,7 @@ PRIVATE>
 
 : ?first ( seq -- elt/f ) 0 swap ?nth ; inline
 : ?second ( seq -- elt/f ) 1 swap ?nth ; inline
+: ?last ( seq -- elt/f ) [ length 1 - ] [ ?nth ] bi ; inline
 
 MIXIN: virtual-sequence
 GENERIC: virtual-exemplar ( seq -- seq' )