if ;
: (top-name) ( vreg -- vreg' )
- stacks get at [ f ] [ last ] if-empty ;
+ stacks get at ?last ;
: top-name ( vreg -- vreg' )
(top-name)
rest
dup last empty? [ but-last ] when ;
-: ?last ( seq -- elt/f )
- [ f ] [ last ] if-empty ;
-
: coalesce ( rows -- rows' )
V{ } clone [
'[
{ $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." } ;
[ 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
: ?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' )