sequences: define ?first and ?second.

db4
John Benediktsson 2011-10-13 12:53:46 -07:00
parent fb178b19a3
commit 3207244c60
15 changed files with 28 additions and 19 deletions

View File

@ -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

View File

@ -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 ]

View File

@ -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

View File

@ -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 ;

View File

@ -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

View File

@ -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 ;

View File

@ -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?

View File

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

View File

@ -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

View File

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

View File

@ -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> ;

View File

@ -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

View File

@ -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

View File

@ -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> ;

View File

@ -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) ;