REBOL 3.0

Comments on: Swap Function?

Carl Sassenrath, CTO
REBOL Technologies
26-May-2006 5:06 GMT

Article #0030
Main page || Index || Prior Article [0029] || Next Article [0031] || 13 Comments || Send feedback

Recently, I noticed that Cyphre implemented a swap function for series of graphics objects. The purpose was to quickly swap two values within a series (or between two series).

The idea of adding swap as a native function to REBOL has been suggested before (many years). Here is an example of what it would do:

Given:

v1: at series1 10
v2: at series2 20

would you prefer to do:

tmp: first v1
change/only v1 first v2
change/only v2 tmp

or:

swap v1 v2

The function would also work within the same series.

A /part refinement could allow swapping of multiple values.

Question: should such a function be provided (at the native level it could be highly optimized compared to the above change code)? Is it really that useful for string series (seems like it would rarely get used) or just blocks?

13 Comments

Comments:

Brian Hawley
26-May-2006 2:11
I like it!

You might as well implement swap for strings as well, particularly if you do the /part refinement. Someone will use it, and it will save more years of explaining why you don't have swap with strings when you have it with blocks... :)

Henrik
26-May-2006 2:56
Spooky, I was just thinking about this from my RPN based HP calculator. Very much needed, thank you.

BrianH, why not just say swap for any series! value?

Brian Hawley
26-May-2006 4:18
Hey, I'm just commenting on the blog as posted :)
rebolek
26-May-2006 5:03
Implement SWAP for binary! too, pleeeease:)
Ridcully
26-May-2006 12:54
:) I don't think it's necessary. As you have shown it can be done easily by within 3 lines of code anyway. Speaking of binary (rebolek): What about bitwise operators in general? I miss them quite a lot. Or is that a problem due to the different platforms? But Java also has them ...
Gregg Irwin
26-May-2006 13:03
What is the primary usage target? A common use of SWAP is in sorting of course, which we don't need it for in REBOL.

If you provide a swap function, I think people will expect it to work on scalar values. e.g.

swap: func [a [word!] b [word!] /local tmp][ set/any 'tmp get/any a set/any a get/any b set/any b get/any 'tmp ] a: 10 b: 20 swap 'a 'b

I wouldn't guess that SWAP would take two series arguments and swap the values at their current offsets, but strong idioms for that case might feel natural eventually.

Here's how I did an internal (swap within a series) series swap function (the main thing in examples here is *not* the implementation, but the interface; how do we want it to look at feel when we use it):

swap-values: func [ "Swap the values at the specified positions in a series." series [series!] index-1 [number! logic! pair!] index-2 [number! logic! pair!] /local tmp ][ tmp: pick series index-1 poke series index-1 pick series index-2 poke series index-2 tmp series ] b: [1 2 3 4 5 6 7 8 9 0] swap-values b 3 7

You could also do something like this:

swap-values-between: func [ series-1 index-1 series-2 index-2 /local tmp ][ tmp: pick series-2 index-2 poke series-2 index-2 pick series-1 index-1 poke series-1 index-1 tmp reduce [series-1 series-2] ] c: [1 2 3] d: [4 5 6] swap-values-between c 2 d 3 ; what about path notation?

Or this, which gives you the default behavior that Carl describes, but allows you to override it, and not have to make sure your series are at the proper offsets; but it separates the indexes from their associated series argument. There is also the question of what to return.

swap-values-between: func [ series-1 series-2 /at index-1 index-2 /local tmp ][ set [index-1 index-2] reduce [any [index-1 1] any [index-2 1]] tmp: pick series-2 index-2 poke series-2 index-2 pick series-1 index-1 poke series-1 index-1 tmp reduce [series-1 series-2] ] c: [1 2 3] d: [4 5 6] swap-values-between c d swap-values-between/at c d 2 3

What about the need to swap things in and out of a series?

exchange: func [ "Change a value in the series; return the original value." series [series!] index [number! logic! pair!] value /local val ][ val: pick series index poke series index value val ] exchange-based-swap: func [series index-1 index-2] [ series/:index-2: exchange series index-1 series/:index-2 series ]

Given a SWAP function, what would the code look like to swap every two elements in a series? Every N elements?

Again, what is the primar

Gregg Irwin
26-May-2006 13:05
Previous post too long. Sorry about that. Here's the tag:

Again, what is the primary target: images? data (as in DB records)? binary? How would /PART be used in those contexts?

I know, I ask too many questions. :)

Cyphre
26-May-2006 15:05
Gregg, I think it can be mainly used for fast swapping faces in pane(s). But as I see from other comments here it could be used for other tasks too. So I vote for generic SWAP native!
James_Nak
30-May-2006 19:02
:) If the resulting size of 3.0 is an issue then I say no and add these and other useful but createable functions to a library of useful functions that can added at will. That being said, I have at times wished I had a swap at my disposal. It would really be cool to have a tool/webpage interface where we could select functions easily that we need in a particular app and have them made available. An "Add to library" shop if you will. Hmm. I mean we already have lots of examples but one sometimes has to wade through the code to get the "function" one requires. Just a thought.
Anonymous
6-Jun-2006 9:54
>> last-post: 26-May-2006 == 26-May-2006 >> subtract now/date last-post == 11 >> ?? Front-Line Front-line: "back-burner" == "back-burner"
Carl Sassenrath
1-Sep-2006 14:05:25
I'm just pulling SWAP up to the top of "recent comments" area. (Relevant to POP/PULL discussions, article #41).
tara babu
31-Oct-2006 0:56:32
kuh wrote to kar do swap function ka bar anman
Ragav
27-Oct-2007 9:05:14
How to Swap a content from one folder to another

Post a Comment:

You can post a comment here. Keep it on-topic.

Name:

Blog id:

R3-0030


Comment:


 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote
 
 

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

REBOL 3.0
Updated 22-Jul-2017 - Edit - Copyright REBOL Technologies - REBOL.net