From: John Benediktsson Date: Wed, 12 Nov 2014 02:13:01 +0000 (-0800) Subject: assocs: moving map-index back to sequences, change zip and zip-index to make alists. X-Git-Tag: unmaintained~3368 X-Git-Url: https://gitweb.factorcode.org/gitweb.cgi?p=factor.git;a=commitdiff_plain;h=b377ea313cd8b3747dc9f6f72a445291b8ff093c assocs: moving map-index back to sequences, change zip and zip-index to make alists. --- diff --git a/core/assocs/assocs-docs.factor b/core/assocs/assocs-docs.factor index f03d04bca3..779248f856 100644 --- a/core/assocs/assocs-docs.factor +++ b/core/assocs/assocs-docs.factor @@ -517,7 +517,7 @@ HELP: unzip 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 ;" @@ -525,7 +525,6 @@ HELP: zip "{ { 1 4 } { 2 5 } { 3 6 } }" } } ; -{ unzip zip } related-words HELP: zip-as { $values @@ -543,7 +542,7 @@ HELP: zip-as HELP: zip-index { $values { "values" sequence } - { "assoc" assoc } + { "alist" "an array of key/value pairs" } } { $examples "Zip a sequnce with its indices:" @@ -568,24 +567,4 @@ HELP: zip-index-as } { $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 diff --git a/core/assocs/assocs-tests.factor b/core/assocs/assocs-tests.factor index dc036799b4..86890058d7 100644 --- a/core/assocs/assocs-tests.factor +++ b/core/assocs/assocs-tests.factor @@ -213,34 +213,13 @@ unit-test { { 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 { @@ -276,7 +255,7 @@ 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 { diff --git a/core/assocs/assocs.factor b/core/assocs/assocs.factor index ce532a36a7..786bd64f02 100644 --- a/core/assocs/assocs.factor +++ b/core/assocs/assocs.factor @@ -206,26 +206,14 @@ M: assoc value-at* swap [ = nip ] curry assoc-find nip ; [ [ 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 ; diff --git a/core/sequences/sequences-docs.factor b/core/sequences/sequences-docs.factor index 1bb69f422a..3caf128de2 100644 --- a/core/sequences/sequences-docs.factor +++ b/core/sequences/sequences-docs.factor @@ -365,6 +365,30 @@ HELP: each-index "{ 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." } diff --git a/core/sequences/sequences.factor b/core/sequences/sequences.factor index 8eaa62c43b..a17d645013 100644 --- a/core/sequences/sequences.factor +++ b/core/sequences/sequences.factor @@ -571,6 +571,12 @@ PRIVATE> : 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 ]