Adding 'switch' word to sequences-lib.

db4
John Benediktsson 2008-09-30 08:07:18 -07:00
parent 165b98419a
commit 4b5ed34f9e
2 changed files with 16 additions and 0 deletions

View File

@ -27,3 +27,14 @@ HELP: enumerate
{ $values { "seq" sequence } { "seq'" sequence } }
{ $description "Returns a new sequence where each element is an array of { index, value }" } ;
HELP: switch
{ $values { "x" number } { "y" number } { "seq" sequence } }
{ $description "Swaps the values between indices 'x' and 'y' in the sequence." }
{ $examples
{ $example
"USING: sequences.lib ;"
"1 2 \"asdf\" [ switch ] keep"
"\"adsf\"" }
} ;

View File

@ -173,3 +173,8 @@ USE: random
: enumerate ( seq -- seq' )
<enum> >alist ;
! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
: switch ( x y seq -- )
3dup swap [ nth ] dip [ [ move ] keep ] 2dip rot set-nth ;