Useful Missing Series! Functions
[1/3] 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"
[2/3] from: jeff:rebol at: 4-Jul-2001 10:05
> SWAP FUNCTION
> swap: func [
<<quoted lines omitted: 4>>
> return back back insert series reduce [y x]
> ]
swap: func [series [series!]][ head reverse/part series 2 ]
[3/3] from: robbo1mark::aol::com at: 4-Jul-2001 13:22
Thanks Jeff that is exactly the kind of "factoring"
I was looking for! I coded them up at lunch time at work
& didn't really have time to think too deeply about them.
It's really cool when people with greater knowledge show me a better / simpler / faster
way of doing things
and having a preference for minimalism / simplicity I really appreciate it!
Any improvements on the other two anybody?
Thanks Jeff, my REBOL friend 8-)
Mark Dickson
In a message dated Wed, 4 Jul 2001 1:10:20 PM Eastern Daylight Time, Jeff Kreis <[jeff--rebol--net]>
writes:
<<
> SWAP FUNCTION
> swap: func [
<<quoted lines omitted: 4>>
> return back back insert series reduce [y x]
> ]
swap: func [series [series!]][ head reverse/part series 2 ]
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted