}\r
} ;\r
\r
+HELP: set-firstn\r
+{ $values { "n" integer } }\r
+{ $description "A generalization of " { $link set-first } " "\r
+"that sets the first " { $snippet "n" } " elements of a sequence from the top " { $snippet "n" } " elements of the stack." } ;\r
+\r
HELP: npick\r
{ $values { "n" integer } }\r
{ $description "A generalization of " { $link dup } ", "\r
HELP: n*quot\r
{ $values\r
{ "n" integer } { "quot" quotation }\r
- { "quot'" quotation }\r
+ { "quotquot" quotation }\r
}\r
{ $examples\r
{ $example "USING: generalizations prettyprint math ;"\r
narray\r
nsequence\r
firstn\r
+ set-firstn\r
nappend\r
nappend-as\r
} ;\r
[ { "xyc" "xyd" } ] [ "x" "y" { "c" "d" } [ 3append ] 2 nwith map ] unit-test\r
\r
[ 1 2 3 4 ] [ { 1 2 3 4 } 4 firstn ] unit-test\r
+[ { 1 2 3 4 } ] [ 1 2 3 4 { f f f f } [ 4 set-firstn ] keep ] unit-test\r
+[ 1 2 3 4 { f f f } [ 4 set-firstn ] keep ] must-fail\r
[ ] [ { } 0 firstn ] unit-test\r
[ "a" ] [ { "a" } 1 firstn ] unit-test\r
\r
MACRO: -nrot ( n -- )
1 - [ ] [ '[ swap _ dip ] ] repeat ;
+MACRO: set-firstn-unsafe ( n -- )
+ [ 1 + ]
+ [ iota [ '[ _ rot [ set-nth-unsafe ] keep ] ] map ] bi
+ '[ _ -nrot _ spread drop ] ;
+
+MACRO: set-firstn ( n -- )
+ dup zero? [ drop [ drop ] ] [
+ [ 1 - swap bounds-check 2drop ]
+ [ set-firstn-unsafe ]
+ bi-curry '[ _ _ bi ]
+ ] if ;
+
MACRO: ndrop ( n -- )
[ drop ] n*quot ;