: select-tuple ( query/tuple -- tuple/f )
>query 1 >>limit [ tuple>> ] [ query>statement ] bi
- do-select [ f ] [ first ] if-empty ;
+ do-select ?first ;
: count-tuples ( query/tuple -- n )
>query [ tuple>> ] [ <count-statement> ] bi do-count
[ [ parse-paragraph ] or-simple-title link boa ] if
] dip [ (parse-paragraph) cons ] [ 1list ] if* ;
-: ?first ( seq -- elt ) 0 swap ?nth ;
-
: parse-big-link ( before after -- link rest )
dup ?first CHAR: [ =
[ parse-link ]
229 server-response ;
: handle-MDTM ( obj -- )
- tokenized>> 1 swap ?nth [
+ tokenized>> ?second [
fixup-relative-path
dup file-info dup directory? [
drop not-a-plain-file
"Failed to change directory." 553 server-response ;
: handle-CWD ( obj -- )
- tokenized>> 1 swap ?nth [
+ tokenized>> ?second [
fixup-relative-path
dup can-serve-directory? [
set-current-directory
buttons-matching-hash device-elements-matching length ;
: ?axis ( device hash -- axis/f )
- device-elements-matching [ f ] [ first ] if-empty ;
+ device-elements-matching ?first ;
: ?x-axis ( device -- ? )
x-axis-matching-hash ?axis ;
seqs>> [ length /mod ] [ nth-unsafe ] bi ; inline
M: merged virtual-exemplar ( merged -- seq )
- seqs>> [ f ] [ first ] if-empty ; inline
+ seqs>> ?first ; inline
INSTANCE: merged virtual-sequence
[ append ] [ [ words-length ] bi@ ] 2bi <element> ;
: ?first2 ( seq -- first/f second/f )
- [ 0 swap ?nth ]
- [ 1 swap ?nth ] bi ;
+ [ ?first ] [ ?second ] bi ;
: split-words ( seq -- half-elements )
[ [ break?>> ] bi@ = ] monotonic-split ;
! this does *not* affect the contents of the stack
[ notags ] unless* ;
-: ?first ( seq -- elt/f ) 0 swap ?nth ;
-
: get-prolog ( seq -- prolog )
{ "" } ?head drop
?first dup prolog?
{ $values { "n" "an integer" } { "seq" sequence } { "elt/f" "an object or " { $link f } } }
{ $description "A forgiving version of " { $link nth } ". If the index is out of bounds, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
+HELP: ?first
+{ $values { "seq" sequence } { "elt/f" "an object or " { $link f } } }
+{ $description "A forgiving version of " { $link first } ". If the sequence is empty, or if the sequence is " { $link f } ", simply outputs " { $link f } "." } ;
+
+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: nth-unsafe
{ $values { "n" "an integer" } { "seq" sequence } { "elt" object } }
{ $contract "Unsafe variant of " { $link nth } " that does not perform bounds checks." } ;
[ -3 10 iota nth ] must-fail
[ 11 10 iota nth ] must-fail
+[ f ] [ f ?first ] unit-test
+[ f ] [ { } ?first ] unit-test
+[ 0 ] [ 10 iota ?first ] unit-test
+
[ -1/0. 0 remove-nth! ] must-fail
[ "" ] [ "" [ CHAR: \s = ] trim ] unit-test
[ "" ] [ "" [ CHAR: \s = ] trim-head ] unit-test
: ?nth ( n seq -- elt/f )
2dup bounds-check? [ nth-unsafe ] [ 2drop f ] if ; inline
+: ?first ( seq -- elt/f ) 0 swap ?nth ; inline
+: ?second ( seq -- elt/f ) 1 swap ?nth ; inline
+
MIXIN: virtual-sequence
GENERIC: virtual-exemplar ( seq -- seq' )
GENERIC: virtual@ ( n seq -- n' seq' )
elf elf-program-headers elf-loadable-segments [
[ p_vaddr>> dup ] [ p_memsz>> + ] bi [a,b)
address swap interval-contains?
- ] filter [ f ] [ first ] if-empty ;
+ ] find nip ;
TYPED:: virtual-address-section ( elf: Elf32/64_Ehdr address -- section-header/f )
elf address virtual-address-segment :> segment
sections [
[ sh_offset>> dup ] [ sh_size>> + ] bi [a,b)
faddress swap interval-contains?
- ] filter [ f ] [ first ] if-empty ;
+ ] find nip ;
TYPED:: elf-segment-data ( elf: Elf32/64_Ehdr header: Elf32/64_Phdr -- uchar-array/f )
header [ p_offset>> elf >c-ptr <displaced-alien> ] [ p_filesz>> ] bi uchar <c-direct-array> ;
: send-query-1result ( collection assoc -- result )
<mdb-query-msg> -1 >>return# send-query-plain
- objects>> [ f ] [ first ] if-empty ;
+ objects>> ?first ;
: send-cmd ( cmd -- result )
[ cmd-collection ] [ assoc>> ] bi send-query-1result ; inline
t >>explain find nip . ;
: find-one ( mdb-query-msg -- result/f )
- fix-query-collection
- 1 >>return# send-query-plain objects>>
- [ f ] [ first ] if-empty ;
+ fix-query-collection 1 >>return#
+ send-query-plain objects>> ?first ;
: count ( mdb-query-msg -- result )
[ count-cmd make-cmd ] dip
seqs>> [ set-length ] with each ;
M: summed virtual-exemplar ( summed -- seq )
- seqs>> [ f ] [ first ] if-empty ;
+ seqs>> ?first ;
: <2summed> ( seq seq -- summed-seq ) 2array <summed> ;
: <3summed> ( seq seq seq -- summed-seq ) 3array <summed> ;
load-dictionary ;
: (correct) ( word dictionary -- word/f )
- corrections [ f ] [ first ] if-empty ;
+ corrections ?first ;
: correct ( word -- word/f )
default-dictionary (correct) ;