]> gitweb.factorcode.org Git - factor.git/commitdiff
sequences: define ?first and ?second.
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 19:53:46 +0000 (12:53 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 13 Oct 2011 19:53:46 +0000 (12:53 -0700)
15 files changed:
basis/db/tuples/tuples.factor
basis/farkup/farkup.factor
basis/ftp/server/server.factor
basis/game/input/iokit/iokit.factor
basis/sequences/merged/merged.factor
basis/wrap/words/words.factor
basis/xml/xml.factor
core/sequences/sequences-docs.factor
core/sequences/sequences-tests.factor
core/sequences/sequences.factor
extra/elf/elf.factor
extra/mongodb/connection/connection.factor
extra/mongodb/driver/driver.factor
extra/sequences/modified/modified.factor
extra/spelling/spelling.factor

index 19e7760c46c489e64b4ea299bffb0f1b2efe9def..2d2fe2314f55ef246c4e47c61336ae5da3e62d32 100644 (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
index 406dada1454dee054c277cfd259530693a7fd38b..b4e5a2b981e674df616da3770c685a2436452887 100644 (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 ]
index c1508f8ad5886c48846d559085e750f4153fb608..369c4969a640f22d06d839d9d7f646a94d8f6129 100644 (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
index 47597b931ede53911b9aa3932d9e0df75cedcbf6..3aa888c085941b1b73d66e02eb7bb0d7695b4674 100644 (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 ;
index c14ccf2f20e0e46274b6f9d6def6a3895595b70b..dbfc90d555061626b819f9ec0e2f8e80a376e56b 100644 (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
index bcf44601707a5bab048c6a198f9bc5ac3a8bbad3..8a2df38e36b66675f7f163e7ad59a6abc7f5fa3d 100644 (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 ;
index 7667ff3b21484a5fb495f86335021d3db536e258..66780e9d67288e210167ca94e27a91cba4f7fb10 100644 (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?
index f9c390ee4964a89710b02cb111dc7379db60bb30..05365baf453461d43f876fa21a1868eeb0428a3c 100644 (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." } ;
index ac5798d946ed141e49ed1664863738c6923538a3..c912fa346dcf61d96a5dbbe0a7f0f21f2f2b85e8 100644 (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
index 17d2f00607aadd344376a9679832cb5f99b921e1..0971982e53c20b5ed6ab8970789b9cf9776093e4 100644 (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' )
index 96c86b474f0e1d2710aafede9d3b31d3d4133f29..775e98ab23d515dea601bb09845bf86df053e48c 100644 (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> ;
index 7dea9389e0b79b2cf7199e527f8b179537ac2952..d2b8f0b611ef8bd3d0a7a00b9c8d3b7091be4b9b 100644 (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
index 3ae28d4c204948aba710d194e94d695e9e86bf11..e0d3be69be1f5e88b2a123cd081a6627a977986e 100644 (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
index 73fcc651bda4ef2de50b06751743de0de069c421..944242bac807405ee9516cd328a9333323d20129 100644 (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> ;
index b8a90bd2dae94fadad191f39dda0af47a9ae4af9..8a351780953c519ce366c64bede7fc6f65855170 100644 (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) ;