sequences: adding ?last for symmetry.

db4
John Benediktsson 2011-10-13 13:04:24 -07:00
parent 3207244c60
commit 4baf14972f
5 changed files with 10 additions and 4 deletions

View 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)

View 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 [
'[

View 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." } ;

View 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

View 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' )