HELP: zip
{ $values
{ "keys" sequence } { "values" sequence }
- { "assoc" assoc } }
+ { "alist" "an array of key/value pairs" } }
{ $description "Combines two sequences pairwise into a single sequence of key/value pairs." }
{ $examples
{ $example "USING: prettyprint assocs ;"
"{ { 1 4 } { 2 5 } { 3 6 } }"
}
} ;
-{ unzip zip } related-words
HELP: zip-as
{ $values
HELP: zip-index
{ $values
{ "values" sequence }
- { "assoc" assoc }
+ { "alist" "an array of key/value pairs" }
}
{ $examples
"Zip a sequnce with its indices:"
}
{ $description "Zip a sequence with its index and return an associative list of type " { $snippet "exemplar" } " where the input sequence is the keys and the indices are the values." } ;
-HELP: map-index
-{ $values
- { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
-{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
-{ $examples { $example "USING: arrays assocs prettyprint ;"
-"{ 10 20 30 } [ 2array ] map-index ."
-"{ { 10 0 } { 20 1 } { 30 2 } }"
-} } ;
-
-HELP: map-index-as
-{ $values
- { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "obj" object } }
-{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the " { $snippet "exemplar" } "." }
-{ $examples { $example "USING: arrays assocs prettyprint ;"
-"{ 10 20 30 } [ 2array ] V{ } map-index-as ."
-"V{ { 10 0 } { 20 1 } { 30 2 } }"
-} } ;
-{ map-index map-index-as } related-words
-
-
{ unzip zip zip-as zip-index zip-index-as } related-words
{ { 1 f } { f 2 } } sift-values
] unit-test
-! map-index, map-index-as
-{
- { 11 23 35 }
-} [ { 11 22 33 } [ + ] map-index ] unit-test
-
-{
- V{ 11 23 35 }
-} [ V{ 11 22 33 } [ + ] map-index ] unit-test
-
-{
- V{ 11 23 35 }
-} [ { 11 22 33 } [ + ] V{ } map-index-as ] unit-test
-
-{
- B{ 11 23 35 }
-} [ { 11 22 33 } [ + ] B{ } map-index-as ] unit-test
-
-{
- BV{ 11 23 35 }
-} [ { 11 22 33 } [ + ] BV{ } map-index-as ] unit-test
-
! zip, zip-as
{
{ { 1 4 } { 2 5 } { 3 6 } }
} [ { 1 2 3 } { 4 5 6 } zip ] unit-test
{
- V{ { 1 4 } { 2 5 } { 3 6 } }
+ { { 1 4 } { 2 5 } { 3 6 } }
} [ V{ 1 2 3 } { 4 5 6 } zip ] unit-test
{
} [ { 11 22 33 } zip-index ] unit-test
{
- V{ { 11 0 } { 22 1 } { 33 2 } }
+ { { 11 0 } { 22 1 } { 33 2 } }
} [ V{ 11 22 33 } zip-index ] unit-test
{
[ [ set-at ] with-assoc 2each ] keep
] if ; inline
- : zip ( keys values -- assoc )
- over zip-as ; inline
-
-: map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... obj )
- dup sequence? [
- [ dup length iota ] 2dip 2map-as
- ] [
- [ dup length iota ] 2dip [ over length ] dip new-assoc
- ! Need to do 2array/first2 here because of quot's stack effect
- [ [ [ first2 swap ] dip set-at ] curry compose 2each ] keep
- ] if ; inline
-
-: map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
- over map-index-as ; inline
+: zip ( keys values -- alist )
+ { } zip-as ; inline
: zip-index-as ( values exemplar -- assoc )
- [ [ 2array ] ] dip map-index-as ; inline
+ [ dup length iota ] dip zip-as ; inline
-: zip-index ( values -- assoc )
- dup zip-index-as ; inline
+: zip-index ( values -- alist )
+ { } zip-index-as ; inline
: unzip ( assoc -- keys values )
dup assoc-empty? [ drop { } { } ] [ >alist flip first2 ] if ;
"{ 10 0 }\n{ 20 1 }\n{ 30 2 }"
} } ;
+HELP: map-index
+{ $values
+ { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "newseq" sequence } }
+{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the input sequence." }
+{ $examples
+ { $example "USING: arrays sequences prettyprint ;"
+ "{ 10 20 30 } [ 2array ] map-index ."
+ "{ { 10 0 } { 20 1 } { 30 2 } }"
+ }
+} ;
+
+HELP: map-index-as
+{ $values
+ { "seq" sequence } { "quot" { $quotation ( ... elt index -- ... newelt ) } } { "exemplar" sequence } { "newseq" sequence } }
+{ $description "Calls the quotation with the element of the sequence and its index on the stack, with the index on the top of the stack. Collects the outputs of the quotation and outputs them in a sequence of the same type as the " { $snippet "exemplar" } " sequence." }
+{ $examples
+ { $example "USING: arrays sequences prettyprint ;"
+ "{ 10 20 30 } [ 2array ] V{ } map-index-as ."
+ "V{ { 10 0 } { 20 1 } { 30 2 } }"
+ }
+} ;
+
+{ map-index map-index-as } related-words
+
HELP: change-nth
{ $values { "i" "a non-negative integer" } { "seq" "a mutable sequence" } { "quot" { $quotation ( ..a elt -- ..b newelt ) } } }
{ $description "Applies the quotation to the " { $snippet "i" } "th element of the sequence, storing the result back into the sequence." }
: each-index ( ... seq quot: ( ... elt index -- ... ) -- ... )
(each-index) each-integer ; inline
+: map-index-as ( ... seq quot: ( ... elt index -- ... newelt ) exemplar -- ... newseq )
+ [ dup length iota ] 2dip 2map-as ; inline
+
+: map-index ( ... seq quot: ( ... elt index -- ... newelt ) -- ... newseq )
+ { } map-index-as ; inline
+
: interleave ( ... seq between quot: ( ... elt -- ... ) -- ... )
pick empty? [ 3drop ] [
[ [ drop first-unsafe ] dip call ]