sequences: define ?first and ?second.
parent
fb178b19a3
commit
3207244c60
|
@ -152,7 +152,7 @@ ERROR: no-defined-persistent object ;
|
|||
|
||||
: 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
|
||||
|
|
|
@ -82,8 +82,6 @@ DEFER: (parse-paragraph)
|
|||
[ [ 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 ]
|
||||
|
|
|
@ -277,7 +277,7 @@ M: ftp-disconnect handle-passive-command ( stream obj -- )
|
|||
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
|
||||
|
@ -300,7 +300,7 @@ ERROR: no-directory-permissions ;
|
|||
"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
|
||||
|
|
|
@ -70,7 +70,7 @@ CONSTANT: hat-switch-matching-hash
|
|||
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 ;
|
||||
|
|
|
@ -26,6 +26,6 @@ M: merged virtual@ ( n seq -- n' seq' )
|
|||
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
|
||||
|
|
|
@ -15,8 +15,7 @@ C: <word> word
|
|||
[ 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 ;
|
||||
|
|
|
@ -83,8 +83,6 @@ M: closer process
|
|||
! 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?
|
||||
|
|
|
@ -179,6 +179,14 @@ HELP: ?nth
|
|||
{ $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." } ;
|
||||
|
|
|
@ -241,6 +241,10 @@ unit-test
|
|||
[ -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
|
||||
|
|
|
@ -175,6 +175,9 @@ PRIVATE>
|
|||
: ?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' )
|
||||
|
|
|
@ -506,7 +506,7 @@ TYPED:: virtual-address-segment ( elf: Elf32/64_Ehdr address -- program-header/f
|
|||
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
|
||||
|
@ -515,7 +515,7 @@ TYPED:: virtual-address-section ( elf: Elf32/64_Ehdr address -- section-header/f
|
|||
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> ;
|
||||
|
|
|
@ -65,7 +65,7 @@ CONSTRUCTOR: mdb-connection ( instance -- mdb-connection ) ;
|
|||
|
||||
: 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
|
||||
|
|
|
@ -240,9 +240,8 @@ M: mdb-cursor find
|
|||
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
|
||||
|
|
|
@ -72,7 +72,7 @@ M: summed set-length ( n seq -- )
|
|||
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> ;
|
||||
|
|
|
@ -72,7 +72,7 @@ MEMO: default-dictionary ( -- counts )
|
|||
load-dictionary ;
|
||||
|
||||
: (correct) ( word dictionary -- word/f )
|
||||
corrections [ f ] [ first ] if-empty ;
|
||||
corrections ?first ;
|
||||
|
||||
: correct ( word -- word/f )
|
||||
default-dictionary (correct) ;
|
||||
|
|
Loading…
Reference in New Issue