use if/when/unless-empty, docs

db4
Doug Coleman 2008-09-06 17:10:32 -05:00
parent 0589e061e1
commit b1d26e100a
10 changed files with 60 additions and 24 deletions

View File

@ -208,9 +208,9 @@ M: anonymous-complement (classes-intersect?)
: min-class ( class seq -- class/f ) : min-class ( class seq -- class/f )
over [ classes-intersect? ] curry filter over [ classes-intersect? ] curry filter
dup empty? [ 2drop f ] [ [ drop f ] [
tuck [ class<= ] with all? [ peek ] [ drop f ] if tuck [ class<= ] with all? [ peek ] [ drop f ] if
] if ; ] if-empty ;
GENERIC: (flatten-class) ( class -- ) GENERIC: (flatten-class) ( class -- )

View File

@ -44,11 +44,11 @@ M: builtin-class (classes-intersect?)
M: anonymous-intersection (flatten-class) M: anonymous-intersection (flatten-class)
participants>> [ flatten-builtin-class ] map participants>> [ flatten-builtin-class ] map
dup empty? [ [
drop builtins get sift [ (flatten-class) ] each builtins get sift [ (flatten-class) ] each
] [ ] [
unclip [ assoc-intersect ] reduce [ swap set ] assoc-each unclip [ assoc-intersect ] reduce [ swap set ] assoc-each
] if ; ] if-empty ;
M: anonymous-complement (flatten-class) M: anonymous-complement (flatten-class)
drop builtins get sift [ (flatten-class) ] each ; drop builtins get sift [ (flatten-class) ] each ;

View File

@ -8,14 +8,14 @@ PREDICATE: intersection-class < class
"metaclass" word-prop intersection-class eq? ; "metaclass" word-prop intersection-class eq? ;
: intersection-predicate-quot ( members -- quot ) : intersection-predicate-quot ( members -- quot )
dup empty? [ [
drop [ drop t ] [ drop t ]
] [ ] [
unclip "predicate" word-prop swap [ unclip "predicate" word-prop swap [
"predicate" word-prop [ dup ] swap [ not ] 3append "predicate" word-prop [ dup ] swap [ not ] 3append
[ drop f ] [ drop f ]
] { } map>assoc alist>quot ] { } map>assoc alist>quot
] if ; ] if-empty ;
: define-intersection-predicate ( class -- ) : define-intersection-predicate ( class -- )
dup participants intersection-predicate-quot define-predicate ; dup participants intersection-predicate-quot define-predicate ;

View File

@ -26,7 +26,7 @@ ERROR: duplicate-slot-names names ;
: check-duplicate-slots ( slots -- ) : check-duplicate-slots ( slots -- )
slot-names duplicates slot-names duplicates
dup empty? [ drop ] [ duplicate-slot-names ] if ; [ duplicate-slot-names ] unless-empty ;
ERROR: invalid-slot-name name ; ERROR: invalid-slot-name name ;

View File

@ -8,14 +8,14 @@ PREDICATE: union-class < class
"metaclass" word-prop union-class eq? ; "metaclass" word-prop union-class eq? ;
: union-predicate-quot ( members -- quot ) : union-predicate-quot ( members -- quot )
dup empty? [ [
drop [ drop f ] [ drop f ]
] [ ] [
unclip "predicate" word-prop swap [ unclip "predicate" word-prop swap [
"predicate" word-prop [ dup ] prepend "predicate" word-prop [ dup ] prepend
[ drop t ] [ drop t ]
] { } map>assoc alist>quot ] { } map>assoc alist>quot
] if ; ] if-empty ;
: define-union-predicate ( class -- ) : define-union-predicate ( class -- )
dup members union-predicate-quot define-predicate ; dup members union-predicate-quot define-predicate ;

View File

@ -21,7 +21,7 @@ M: object dispose
: dispose-each ( seq -- ) : dispose-each ( seq -- )
[ [
[ [ dispose ] curry [ , ] recover ] each [ [ dispose ] curry [ , ] recover ] each
] { } make dup empty? [ drop ] [ peek rethrow ] if ; ] { } make [ peek rethrow ] unless-empty ;
: with-disposal ( object quot -- ) : with-disposal ( object quot -- )
over [ dispose ] curry [ ] cleanup ; inline over [ dispose ] curry [ ] cleanup ; inline

View File

@ -59,7 +59,7 @@ HOOK: (file-appender) io-backend ( path -- stream )
HOOK: root-directory? io-backend ( path -- ? ) HOOK: root-directory? io-backend ( path -- ? )
M: object root-directory? ( path -- ? ) M: object root-directory? ( path -- ? )
dup empty? [ drop f ] [ [ path-separator? ] all? ] if ; [ f ] [ [ path-separator? ] all? ] if-empty ;
ERROR: no-parent-directory path ; ERROR: no-parent-directory path ;
@ -80,7 +80,7 @@ ERROR: no-parent-directory path ;
: head-path-separator? ( path1 ? -- ?' ) : head-path-separator? ( path1 ? -- ?' )
[ [
dup empty? [ drop t ] [ first path-separator? ] if [ t ] [ first path-separator? ] if-empty
] [ ] [
drop f drop f
] if ; ] if ;

View File

@ -18,7 +18,7 @@ M: growable stream-flush drop ;
<string-writer> swap [ output-stream get ] compose with-output-stream* <string-writer> swap [ output-stream get ] compose with-output-stream*
>string ; inline >string ; inline
M: growable stream-read1 dup empty? [ drop f ] [ pop ] if ; M: growable stream-read1 [ f ] [ pop ] if-empty ;
: harden-as ( seq growble-exemplar -- newseq ) : harden-as ( seq growble-exemplar -- newseq )
underlying>> like ; underlying>> like ;
@ -39,13 +39,13 @@ M: growable stream-read-until
] if ; ] if ;
M: growable stream-read M: growable stream-read
dup empty? [ [
2drop f drop f
] [ ] [
[ length swap - 0 max ] keep [ length swap - 0 max ] keep
[ swap growable-read-until ] 2keep [ swap growable-read-until ] 2keep
set-length set-length
] if ; ] if-empty ;
M: growable stream-read-partial M: growable stream-read-partial
stream-read ; stream-read ;

View File

@ -335,6 +335,42 @@ HELP: if-empty
"6" "6"
} ; } ;
HELP: when-empty
{ $values
{ "seq" sequence } { "quot1" quotation } }
{ $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and the " { $snippet "quot1" } " is called." }
{ $examples "This word is equivalent to " { $link if-empty } " with an empty second quotation:"
{ $example
"USING: sequences prettyprint ;"
"{ } [ { 4 5 6 } ] [ ] if-empty ."
"{ 4 5 6 }"
}
{ $example
"USING: sequences prettyprint ;"
"{ } [ { 4 5 6 } ] when-empty ."
"{ 4 5 6 }"
}
} ;
HELP: unless-empty
{ $values
{ "seq" sequence } { "quot2" quotation } }
{ $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped. Otherwise, the " { $snippet "quot2" } " is called on the sequence.." }
{ $examples "This word is equivalent to " { $link if-empty } " with an empty first quotation:"
{ $example
"USING: sequences prettyprint ;"
"{ 4 5 6 } [ ] [ sum ] if-empty ."
"15"
}
{ $example
"USING: sequences prettyprint ;"
"{ 4 5 6 } [ sum ] unless-empty ."
"15"
}
} ;
{ if-empty when-empty unless-empty } related-words
HELP: delete-all HELP: delete-all
{ $values { "seq" "a resizable sequence" } } { $values { "seq" "a resizable sequence" } }
{ $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." } { $description "Resizes the sequence to zero length, removing all elements. Not all sequences are resizable." }

View File

@ -34,7 +34,7 @@ M: sequence shorten 2dup length < [ set-length ] [ 2drop ] if ;
: when-empty ( seq quot1 -- ) [ ] if-empty ; inline : when-empty ( seq quot1 -- ) [ ] if-empty ; inline
: unless-empty ( seq quot1 -- ) [ ] swap if-empty ; inline : unless-empty ( seq quot2 -- ) [ ] swap if-empty ; inline
: delete-all ( seq -- ) 0 swap set-length ; : delete-all ( seq -- ) 0 swap set-length ;
@ -91,7 +91,7 @@ M: sequence set-nth-unsafe set-nth ;
! The f object supports the sequence protocol trivially ! The f object supports the sequence protocol trivially
M: f length drop 0 ; M: f length drop 0 ;
M: f nth-unsafe nip ; M: f nth-unsafe nip ;
M: f like drop dup empty? [ drop f ] when ; M: f like drop [ f ] when-empty ;
INSTANCE: f immutable-sequence INSTANCE: f immutable-sequence
@ -630,14 +630,14 @@ M: slice equal? over slice? [ sequence= ] [ 2drop f ] if ;
0 [ length + ] reduce ; 0 [ length + ] reduce ;
: concat ( seq -- newseq ) : concat ( seq -- newseq )
dup empty? [ [
drop { } { }
] [ ] [
[ sum-lengths ] keep [ sum-lengths ] keep
[ first new-resizable ] keep [ first new-resizable ] keep
[ [ over push-all ] each ] keep [ [ over push-all ] each ] keep
first like first like
] if ; ] if-empty ;
: joined-length ( seq glue -- n ) : joined-length ( seq glue -- n )
>r dup sum-lengths swap length 1 [-] r> length * + ; >r dup sum-lengths swap length 1 [-] r> length * + ;