math arrays ;\r
IN: generalizations\r
\r
+HELP: nsequence\r
+{ $values { "n" integer } { "seq" "an exemplar" } }\r
+{ $description "A generalization of " { $link 2sequence } ", "\r
+{ $link 3sequence } ", and " { $link 4sequence } " "\r
+"that constructs a sequence from the top " { $snippet "n" } " elements of the stack."\r
+}\r
+{ $examples\r
+ { $example "CHAR: f CHAR: i CHAR: s CHAR: h 4 \"\" nsequence ." "\"fish\"" }\r
+} ;\r
+\r
HELP: narray\r
{ $values { "n" integer } }\r
{ $description "A generalization of " { $link 1array } ", "\r
"that constructs an array from the top " { $snippet "n" } " elements of the stack."\r
} ;\r
\r
+{ nsequence narray } related-words\r
+\r
HELP: firstn\r
{ $values { "n" integer } }\r
{ $description "A generalization of " { $link first } ", "\r
{ $see-also keep nslip } ;\r
\r
ARTICLE: "generalizations" "Generalized shuffle words and combinators"\r
-"A number of stack shuffling words and combinators for use in "\r
+"The " { $vocab-link "generalizations" } " vocabulary defines a number of stack shuffling words and combinators for use in "\r
"macros where the arity of the input quotations depends on an "\r
"input parameter."\r
+$nl\r
+"Generalized sequence operations:"\r
{ $subsection narray }\r
+{ $subsection nsequence }\r
{ $subsection firstn }\r
+"Generated stack shuffle operations:"\r
{ $subsection ndup }\r
{ $subsection npick }\r
{ $subsection nrot }\r
{ $subsection nnip }\r
{ $subsection ndrop }\r
{ $subsection nrev }\r
+"Generalized combinators:"\r
{ $subsection ndip }\r
{ $subsection nslip }\r
{ $subsection nkeep }\r
math.ranges combinators macros quotations fry arrays ;\r
IN: generalizations\r
\r
-MACRO: narray ( n -- quot )\r
- [ <reversed> ] [ '[ , f <array> ] ] bi\r
+MACRO: nsequence ( n seq -- quot )\r
+ [ drop <reversed> ] [ '[ , , new-sequence ] ] 2bi\r
[ '[ @ [ , swap set-nth-unsafe ] keep ] ] reduce ;\r
\r
+MACRO: narray ( n -- quot )\r
+ '[ , { } nsequence ] ;\r
+\r
MACRO: firstn ( n -- )\r
dup zero? [ drop [ drop ] ] [\r
[ [ '[ , _ nth-unsafe ] ] map ]\r