Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Useful Missing Series! Functions

From: robbo1mark:aol at: 4-Jul-2001 6:56

Everybody, Here's a few simple, useful series! functions which are currently missing from REBOL. These are SWAP, SLICE & POP. SWAP unsurprisingly swaps the positions of two values in a series!. SLICE return a copy of a series between two inclusive index positions. POP returns the first value of a series and removes it from the series. Here's the FUNC's below. Feel free to post re-factored optimised versions, I welcome this. cheers, Mark Dickson ----------------------------------------------------------------- SWAP FUNCTION swap: func [ series [series!] /local x y ] [ x: first series y: second series remove/part series 2 return back back insert series reduce [y x] ] ; examples
>> swap "ehllo"
== "hello"
>> swap [b a c]
== [a b c]
>> swap at [a c b] 2
== [b c]
>> head swap at [a c b] 2
== [a b c] SLICE FUNCTION slice: func [ series [series!] start [integer!] end [integer!] ] [ return copy/part at series start (end - start) + 1 ] ; examples
>> slice [ a b c d e f g] 2 4
== [b c d]
>> slice "alphabet soup" 1 5
== "alpha"
>> slice at "alphabet soup" 9 1 5
== " soup"
>>
POP FUNCTION pop: func [ series [series!] /local x ][ x: first series series: remove series return x ] ; examples
>> pop a: [1 2 3]
== 1
>> a
== [2 3]
>> pop a: "Hello"
== #"H"
>> a
== "ello"