Merge branch 'master' into improved-aa

db4
Slava Pestov 2009-10-01 23:16:54 -05:00
commit 96d593dd7c
3 changed files with 26 additions and 8 deletions

View File

@ -54,14 +54,18 @@ TUPLE: A
: <direct-A> ( alien len -- specialized-array ) A boa ; inline : <direct-A> ( alien len -- specialized-array ) A boa ; inline
: <A> ( n -- specialized-array ) [ \ T <underlying> ] keep <direct-A> ; inline : <A> ( n -- specialized-array )
[ \ T <underlying> ] keep <direct-A> ; inline
: (A) ( n -- specialized-array ) [ \ T (underlying) ] keep <direct-A> ; inline : (A) ( n -- specialized-array )
[ \ T (underlying) ] keep <direct-A> ; inline
: malloc-A ( len -- specialized-array ) [ \ T heap-size calloc ] keep <direct-A> ; inline : malloc-A ( len -- specialized-array )
[ \ T heap-size calloc ] keep <direct-A> ; inline
: byte-array>A ( byte-array -- specialized-array ) : byte-array>A ( byte-array -- specialized-array )
dup length \ T heap-size /mod 0 = [ drop \ T bad-byte-array-length ] unless >c-ptr dup length \ T heap-size /mod 0 =
[ drop \ T bad-byte-array-length ] unless
<direct-A> ; inline <direct-A> ; inline
M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline

View File

@ -276,9 +276,19 @@ HELP: reduce-index
"153" "153"
} } ; } } ;
HELP: accumulate-as
{ $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "exemplar" sequence } { "final" "the final result" } { "newseq" "a new sequence" } }
{ $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of the same type as " { $snippet "exemplar" } " containing intermediate results, together with the final result."
$nl
"The first element of the new sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the old sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the old sequence."
$nl
"When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." } ;
HELP: accumulate HELP: accumulate
{ $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new sequence" } } { $values { "identity" object } { "seq" sequence } { "quot" { $quotation "( prev elt -- next )" } } { "final" "the final result" } { "newseq" "a new array" } }
{ $description "Combines successive elements of the sequence using a binary operation, and outputs a sequence of intermediate results together with the final result. The first element of the new sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the old sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the old sequence." { $description "Combines successive elements of the sequence using a binary operation, and outputs an array of intermediate results, together with the final result."
$nl
"The first element of the new sequence is " { $snippet "identity" } ". Then, on the first iteration, the two inputs to the quotation are " { $snippet "identity" } ", and the first element of the old sequence. On successive iterations, the first input is the result of the previous iteration, and the second input is the corresponding element of the old sequence."
$nl $nl
"When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." } "When given the empty sequence, outputs an empty sequence together with the " { $snippet "identity" } "." }
{ $examples { $examples
@ -1400,7 +1410,7 @@ ARTICLE: "sequences-access" "Accessing sequence elements"
ARTICLE: "sequences-add-remove" "Adding and removing sequence elements" ARTICLE: "sequences-add-remove" "Adding and removing sequence elements"
"Adding elements:" "Adding elements:"
{ $subsections prefix suffix } { $subsections prefix suffix insert-nth }
"Removing elements:" "Removing elements:"
{ $subsections remove remq remove-nth } ; { $subsections remove remq remove-nth } ;
@ -1495,6 +1505,7 @@ ARTICLE: "sequences-combinators" "Sequence combinators"
map-index map-index
map-reduce map-reduce
accumulate accumulate
accumulate-as
produce produce
produce-as produce-as
} }

View File

@ -432,8 +432,11 @@ PRIVATE>
: change-each ( seq quot -- ) : change-each ( seq quot -- )
over map-into ; inline over map-into ; inline
: accumulate-as ( seq identity quot exemplar -- final newseq )
[ [ swap ] dip [ curry keep ] curry ] dip map-as ; inline
: accumulate ( seq identity quot -- final newseq ) : accumulate ( seq identity quot -- final newseq )
swapd [ [ call ] [ 2drop ] 3bi ] curry { } map-as ; inline { } accumulate-as ; inline
: 2each ( seq1 seq2 quot -- ) : 2each ( seq1 seq2 quot -- )
(2each) each-integer ; inline (2each) each-integer ; inline