From 33eee7638b4726ec0e41618902b5c3efd91a2200 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 6 Sep 2008 23:20:27 -0500 Subject: [PATCH] Add nsequence --- .../generalizations-docs.factor | 19 ++++++++++++++++++- basis/generalizations/generalizations.factor | 7 +++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/basis/generalizations/generalizations-docs.factor b/basis/generalizations/generalizations-docs.factor index a702f452da..a2b318cb39 100755 --- a/basis/generalizations/generalizations-docs.factor +++ b/basis/generalizations/generalizations-docs.factor @@ -2,6 +2,16 @@ USING: help.syntax help.markup kernel sequences quotations math arrays ; IN: generalizations +HELP: nsequence +{ $values { "n" integer } { "seq" "an exemplar" } } +{ $description "A generalization of " { $link 2sequence } ", " +{ $link 3sequence } ", and " { $link 4sequence } " " +"that constructs a sequence from the top " { $snippet "n" } " elements of the stack." +} +{ $examples + { $example "CHAR: f CHAR: i CHAR: s CHAR: h 4 \"\" nsequence ." "\"fish\"" } +} ; + HELP: narray { $values { "n" integer } } { $description "A generalization of " { $link 1array } ", " @@ -9,6 +19,8 @@ HELP: narray "that constructs an array from the top " { $snippet "n" } " elements of the stack." } ; +{ nsequence narray } related-words + HELP: firstn { $values { "n" integer } } { $description "A generalization of " { $link first } ", " @@ -127,11 +139,15 @@ HELP: nkeep { $see-also keep nslip } ; ARTICLE: "generalizations" "Generalized shuffle words and combinators" -"A number of stack shuffling words and combinators for use in " +"The " { $vocab-link "generalizations" } " vocabulary defines a number of stack shuffling words and combinators for use in " "macros where the arity of the input quotations depends on an " "input parameter." +$nl +"Generalized sequence operations:" { $subsection narray } +{ $subsection nsequence } { $subsection firstn } +"Generated stack shuffle operations:" { $subsection ndup } { $subsection npick } { $subsection nrot } @@ -139,6 +155,7 @@ ARTICLE: "generalizations" "Generalized shuffle words and combinators" { $subsection nnip } { $subsection ndrop } { $subsection nrev } +"Generalized combinators:" { $subsection ndip } { $subsection nslip } { $subsection nkeep } diff --git a/basis/generalizations/generalizations.factor b/basis/generalizations/generalizations.factor index e4d5249a30..c97e9c7b91 100755 --- a/basis/generalizations/generalizations.factor +++ b/basis/generalizations/generalizations.factor @@ -5,10 +5,13 @@ USING: kernel sequences sequences.private namespaces math math.ranges combinators macros quotations fry arrays ; IN: generalizations -MACRO: narray ( n -- quot ) - [ ] [ '[ , f ] ] bi +MACRO: nsequence ( n seq -- quot ) + [ drop ] [ '[ , , new-sequence ] ] 2bi [ '[ @ [ , swap set-nth-unsafe ] keep ] ] reduce ; +MACRO: narray ( n -- quot ) + '[ , { } nsequence ] ; + MACRO: firstn ( n -- ) dup zero? [ drop [ drop ] ] [ [ [ '[ , _ nth-unsafe ] ] map ]