www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Idea: swap with multiple arguments

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
So swap(a, b) swaps the contents of a and b. This could be easily 
generalized to multiple arguments such that swap(a1, a2, ..., an) 
arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do 
know applications for three arguments. Thoughts? -- Andrei
May 23 2016
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap. -Steve
May 23 2016
next sibling parent Tourist <gravatar gravatar.com> writes:
On Monday, 23 May 2016 at 20:27:43 UTC, Steven Schveighoffer 
wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be 
 easily
 generalized to multiple arguments such that swap(a1, a2, ..., 
 an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, 
 etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap. -Steve
Unless it swaps randomly :)
May 23 2016
prev sibling next sibling parent Q. Schroll <qs.il.paperinik gmail.com> writes:
On Monday, 23 May 2016 at 20:27:43 UTC, Steven Schveighoffer 
wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be 
 easily generalized to multiple arguments such that swap(a1, 
 a2, ..., an) arranges things such that a1 gets an, a2 gets a1, 
 a3 gets a2, etc. I do know applications for three arguments. 
 Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap. -Steve
Just name Andrei's function rotate and make swap just an alias of it with exactly two parameters. No confusion and everyone is happy.
May 23 2016
prev sibling next sibling parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Monday, May 23, 2016 16:27:43 Steven Schveighoffer via Digitalmars-d wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap.
That would probably be better. My immediate thought on reading Andrei's suggestion for swap was that it would be way too easy to forget what's actually being swapped when there are more than two arguments. Rotate at least implies and ordering to it. Though I confess that I've never heard of a swap function (or anything like a swap function) that's swapped more than two arguments, and I'm not aware of any point in time when I thought that such a thing might be useful. So, I'm not exactly clamoring for a function like this, but that obviously doesn't mean that it's not worth adding, just that I don't personally see any particular value in it. - Jonathan M Davis
May 23 2016
parent reply Observer <here nowhere.net> writes:
On Monday, 23 May 2016 at 21:38:54 UTC, Jonathan M Davis wrote:
 One thing that screams out to me: this should be called 
 rotate, not swap.
That would probably be better. My immediate thought on reading Andrei's suggestion for swap was that it would be way too easy to forget what's actually being swapped when there are more than two arguments. Rotate at least implies an ordering to it. Though I confess that I've never heard of a swap function (or anything like a swap function) that's swapped more than two arguments, and I'm not aware of any point in time when I thought that such a thing might be useful. So, I'm not exactly clamoring for a function like this, but that obviously doesn't mean that it's not worth adding, just that I don't personally see any particular value in it.
If you don't see value in it, you've neglected to learn PostScript. There, a similar capability is used to "rotate" a certain number of the top elements on the operand stack. However, because PostScript is a graphical language, the "rotate" operator instead does a 2D coordinate-system rotation. For the functionality that Andrei wants, PostScript uses the "roll" operator, which performs a circular shift of the top operands on the stack. Aside from those operands themselves, it takes an argument n (the number of operands to roll) and an argument j (the number of positions to roll, which can be either positive ("upward" motion on the stack) or negative ("downward" motion on the stack)). To be more visual about it, if j is 1, the top element of the stack is popped and inserted further into the stack. https://www.adobe.com/products/postscript/pdfs/PLRM.pdf (page 483 by printed-page numbering, page 489 within the PDF file) has all the details for this operator. The point is that alternative terms like "roll" and "circular shift" might be useful in naming this function, instead of either swap (which I agree is confusing) or rotate. And while n can be imputed from the number of arguments passed, it might be useful to pass j explicitly, allowing shifts of more than one position and in either direction. (Speaking of graphical contexts, if you were working in 3D, you might consider the meaning of "roll" to be "reserved", in the sense of roll/pitch/yaw.) As for utility, if you're a PostScript programmer, where keeping track of data is done via a stack-oriented model, this capability gets used all the time, to bring relevant arguments to the top of the stack to be operated upon, or to shove arguments deeper into the stack for later use.
May 23 2016
next sibling parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Tuesday, May 24, 2016 02:40:24 Observer via Digitalmars-d wrote:
 If you don't see value in it, you've neglected to learn
 PostScript.
I wasn't aware that postscript was a programming language. All I know about it is that it's what printers talk (sometimes), and I assumed that it was a protocol. - Jonathan M Davis
May 24 2016
parent Observer <here nowhere.net> writes:
On Tuesday, 24 May 2016 at 08:56:31 UTC, Jonathan M Davis wrote:
 On Tuesday, May 24, 2016 02:40:24 Observer via Digitalmars-d 
 wrote:
 If you don't see value in it, you've neglected to learn 
 PostScript.
I wasn't aware that postscript was a programming language. All I know about it is that it's what printers talk (sometimes), and I assumed that it was a protocol.
It is a full-fledged programming language, and quite nice for manipulating graphic output to raster devices. There was even a Display PostScript version that targeted output to screens, though Adobe eventually dropped support for it. The PLRM book (PostScript Language Reference Manual) I mentioned earlier is the definitive description of the language. There is also a separate book, "Mathematical Illustrations: A Manual of Geometry and PostScript", by Bill Casselman, that describes how to use it to draw 3D models. Anyway, with respect to the current discussion, PostScript has a rich and elegant set of operators, to handle stack operations, math operations, array operations, dictionary (hash) operations, string operations, control operations, file operations, graphics operations (of course), and more. It's definitely worth a look for inspiration both when designing language features, and for the clarity of its documentation (the PLRM).
May 24 2016
prev sibling parent Adrian Matoga <dlang.spam matoga.info> writes:
On Tuesday, 24 May 2016 at 02:40:24 UTC, Observer wrote:
 As for utility, if you're a PostScript programmer, where keeping
 track of data is done via a stack-oriented model, this 
 capability
 gets used all the time, to bring relevant arguments to the top
 of the stack to be operated upon, or to shove arguments deeper
 into the stack for later use.
Forth also has "swap" and "rot". The former moves 2nd value to the top of the stack (i.e. a, b becomes b, a). The latter moves 3rd value to the top (i.e. a, b, c becomes b, c, a).
May 24 2016
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap. -Steve
Yes, rotate(), but then I would never remember what direction it rotates. Ali
May 23 2016
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/23/16 5:47 PM, Ali Çehreli wrote:
 On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap.
Yes, rotate(), but then I would never remember what direction it rotates.
Use the law of UFCS: x.rotate(y, z); // x-> y, y -> z, z -> x -Steve
May 23 2016
parent reply Jonathan M Davis via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Monday, May 23, 2016 18:10:02 Steven Schveighoffer via Digitalmars-d wrote:
 On 5/23/16 5:47 PM, Ali ehreli wrote:
 On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap.
Yes, rotate(), but then I would never remember what direction it rotates.
Use the law of UFCS: x.rotate(y, z); // x-> y, y -> z, z -> x
Hmmm. And I would have assumed that it rotated in the other direction. This is really going to need a very specific name like rotateLeft or rotateRight in order for it not to be error-prone. - Jonathan M Davis
May 23 2016
parent reply ixid <adamsibson hotmail.com> writes:
On Tuesday, 24 May 2016 at 01:18:05 UTC, Jonathan M Davis wrote:
 Hmmm. And I would have assumed that it rotated in the other 
 direction. This is really going to need a very specific name 
 like rotateLeft or rotateRight in order for it not to be 
 error-prone.

 - Jonathan M Davis
Why would you assume it would rotate left? As a general rule to avoid verbosity we could have a default assumption of things moving in one direction and the inverse gets named. So we would have rotate and rotateLeft.
May 24 2016
parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Tue, May 24, 2016 at 09:21:07AM +0000, ixid via Digitalmars-d wrote:
 On Tuesday, 24 May 2016 at 01:18:05 UTC, Jonathan M Davis wrote:
 Hmmm. And I would have assumed that it rotated in the other
 direction.  This is really going to need a very specific name like
 rotateLeft or rotateRight in order for it not to be error-prone.
 
 - Jonathan M Davis
Why would you assume it would rotate left? As a general rule to avoid verbosity we could have a default assumption of things moving in one direction and the inverse gets named. So we would have rotate and rotateLeft.
And why would you assume that it would rotate right? The default assumption, unless stated clearly and widely followed everywhere, is unclear and leads to misunderstandings. I'd rather unambiguously name them rotateRight and rotateLeft than the asymmetric rotate / rotateLeft. T -- What is Matter, what is Mind? Never Mind, it doesn't Matter.
May 24 2016
next sibling parent reply Wyatt <wyatt.epp gmail.com> writes:
On Tuesday, 24 May 2016 at 12:22:01 UTC, H. S. Teoh wrote:
 And why would you assume that it would rotate right? The 
 default assumption, unless stated clearly and widely followed 
 everywhere, is unclear and leads to misunderstandings. I'd 
 rather unambiguously name them rotateRight and rotateLeft than 
 the asymmetric rotate / rotateLeft.
In the APL family, we have dyadic ⌽ and ⊖: 3⌽⍳9 ⍝ left 4 5 6 7 8 9 1 2 3 ¯3⌽⍳9 ⍝ right 7 8 9 1 2 3 4 5 6 3 3⍴⍳9 1 2 3 4 5 6 7 8 9 1⊖3 3⍴⍳9 ⍝ up 4 5 6 7 8 9 1 2 3 ¯1⊖3 3⍴⍳9 ⍝ down 7 8 9 1 2 3 4 5 6 They rotate left and up in the positive case because that yields the natural indexing order. Right rotation gives you a reverse ordering. -Wyatt
May 24 2016
parent NynnFR <fopa1 wanadoo.fr> writes:
On Tuesday, 24 May 2016 at 14:49:20 UTC, Wyatt wrote:
 In the APL family, we have dyadic ⌽ and ⊖:
       3⌽⍳9 ⍝ left
 4 5 6 7 8 9 1 2 3
       ¯3⌽⍳9 ⍝ right
 7 8 9 1 2 3 4 5 6
[...]
 -Wyatt
Wow: I didn't see any APL code for years ! ;) That's fine but maybe not a very good example of what should be or not be in D... I agree that swap isn't a good name for a rotating operator and that rotateLeft / rotateRight are long names too. And why not swap!"1342"(a, b, c, d) giving (a, c, d, b) ?
May 24 2016
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Tuesday, 24 May 2016 at 12:22:01 UTC, H. S. Teoh wrote:
 And why would you assume that it would rotate right? The 
 default assumption, unless stated clearly and widely followed 
 everywhere, is unclear and leads to misunderstandings.
hmmm rotate left would be a lot easier (and cleaner) to implement than rotate right..? And could include tail optimizations? If i consider a purely functional language where you have access to the left-side of the argument list but not the right side... //Should probably heavily incorporate move as well... //best if only bitwise-copies are necessary to swap/rotate values void rotate(T...)(T[] arr) { T temp = arr[0]; void inner(T[] vars) { if (arr.length > 1) { vars[0] = vars[1]; inner(vars[1 .. $]); } else { vars[0] = temp; } } inner(arr); }
May 24 2016
prev sibling next sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Monday, 23 May 2016 at 21:47:31 UTC, Ali Çehreli wrote:
 Yes, rotate(), but then I would never remember what direction 
 it rotates.
If we take a cue from assembly instructions, there's rol and ror (rotate left/right). These are other instructions are normally unreachable in languages; Although it's usefulness can be wagered from time to time. There's also it's cousins rotate with carry (rcl and rcr).
May 23 2016
prev sibling parent =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Monday, 23 May 2016 at 21:47:31 UTC, Ali Çehreli wrote:
 Yes, rotate(), but then I would never remember what direction 
 it rotates.

 Ali
I agree. So we need rotate{Forward,Backward} or rotate{Left,Right} then. I vote for the former case.
May 25 2016
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 05/23/2016 04:27 PM, Steven Schveighoffer wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap.
Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments "rotate" is overkill. -- Andrei
May 23 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 5/23/16 6:22 PM, Andrei Alexandrescu wrote:
 On 05/23/2016 04:27 PM, Steven Schveighoffer wrote:
 On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I do
 know applications for three arguments. Thoughts? -- Andrei
One thing that screams out to me: this should be called rotate, not swap.
Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments "rotate" is overkill. -- Andrei
OK, let me rephrase. For 3 arguments, swap is a totally unintuitive name :) I personally think rotate is fine, is describing pretty much exactly what is happening, and scales well. But I guess there is some existing meaning in Phobos for it, so that may not fly. -Steve
May 23 2016
prev sibling next sibling parent Xinok <xinok live.com> writes:
On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be 
 easily generalized to multiple arguments such that swap(a1, a2, 
 ..., an) arranges things such that a1 gets an, a2 gets a1, a3 
 gets a2, etc. I do know applications for three arguments. 
 Thoughts? -- Andrei
It doesn't make sense for "swap" to make take than two arguments and it's unintuitive how it should rearrange the elements when you write swap(a1, a2, a3, ...). Who's to say that it should shift the elements to the left? I'm not saying this is useless but it would really need a better name. "swap" only makes intuitive sense when it takes two arguments, no more and no less. While this is technically a rotation, a "rotate" function generally takes an extra argument which is an element/index to rotate on. See: https://www.sgi.com/tech/stl/rotate.html
May 23 2016
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d" <digitalmars-d puremagic.com> writes:
On Mon, May 23, 2016 at 04:01:08PM -0400, Andrei Alexandrescu via Digitalmars-d
wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I
 do know applications for three arguments. Thoughts? -- Andrei
As others have said, 'swap' is a horrible name for this. I'd go with 'rotate', but then there's the question of which direction it rotates. Conceivably, it's equally valid for an to get a1, a1 to get a2, etc.. So 'rotateRight' would be most unambiguous, though a handful to type. T -- What's an anagram of "BANACH-TARSKI"? BANACH-TARSKI BANACH-TARSKI.
May 23 2016
parent Piotr Szturmaj <bncrbme jadamspam.pl> writes:
On 2016-05-24 02:16, H. S. Teoh via Digitalmars-d wrote:
 On Mon, May 23, 2016 at 04:01:08PM -0400, Andrei Alexandrescu via
Digitalmars-d wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I
 do know applications for three arguments. Thoughts? -- Andrei
As others have said, 'swap' is a horrible name for this. I'd go with 'rotate', but then there's the question of which direction it rotates. Conceivably, it's equally valid for an to get a1, a1 to get a2, etc.. So 'rotateRight' would be most unambiguous, though a handful to type.
I would do it as rotate(RandomAccessRange, ptrdiff_t shift). shift >= 1 means rotate right by amount of shift shift <= -1 means rotate left shift == 0 does nothing This way it's more general, it's possible to rotate by any amount, left or right.
May 24 2016
prev sibling next sibling parent reply Xinok <xinok live.com> writes:
On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote:
 ...
I wish to make a different point which is more general regarding ideas like these. I see a lot of proposals to add this or that to the standard library and a lot of debate pursues. One point I've never seen mentioned though is that these ideas would probably have limited usage in the real world. I think a good rule of thumb to consider is that the standard library should be *general purpose* and mostly contain common functionality, with some exceptions of course. We should consider whether these things will actually see common usage or just add bloat to the standard library. Also consider that once it's incorporated into Phobos, the responsibility of maintaining that code falls on the community rather than those few individuals who actually desire that functionality. I'm sure this feature has some useful application in some specific domain but I question it's value in the community at large. I personally see no value in having a swap which can rotate three or more arguments to the left, or however we may choose to incorporate this functionality. On the other hand, I use swap() with two arguments all the time; it's very common functionality that most of us has likely used at least a few times. BTW, Phobos already has a function called bringToFront which can rotate/roll ranges but the interface is a bit different compared to other languages. https://dlang.org/phobos/std_algorithm_mutation.html#.bringToFront
May 24 2016
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 05/24/2016 02:48 PM, Xinok wrote:
 One point I've never seen mentioned though is that these ideas would
 probably have limited usage in the real world.
I agree. I'd be hard pressed to find a use for swap/rotate with more than three arguments. It's just that generalization that seems natural yet with limited applicability has worked nicely in a few instances in the past. -- Andrei
May 24 2016
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 05/24/2016 02:48 PM, Xinok wrote:
 BTW, Phobos already has a function called bringToFront which can
 rotate/roll ranges but the interface is a bit different compared to
 other languages.

 https://dlang.org/phobos/std_algorithm_mutation.html#.bringToFront
This may be a misunderstanding. The original discussion was about rotating a fixed and compile-time-known number of values, not a range. -- Andrei
May 24 2016
parent Xinok <xinok live.com> writes:
On Tuesday, 24 May 2016 at 18:51:32 UTC, Andrei Alexandrescu 
wrote:
 On 05/24/2016 02:48 PM, Xinok wrote:
 BTW, Phobos already has a function called bringToFront which 
 can
 rotate/roll ranges but the interface is a bit different 
 compared to
 other languages.

 https://dlang.org/phobos/std_algorithm_mutation.html#.bringToFront
This may be a misunderstanding. The original discussion was about rotating a fixed and compile-time-known number of values, not a range. -- Andrei
No misunderstanding; I realize that what you're proposing is a variadic function which takes some arbitrary set of variables/l-values passed by reference. The message was directed at those discussing rotate/roll/etc from other languages. It seemed nobody was aware this functionality was already available in Phobos though understandable since bringToFront is an unusual name for this function.
May 24 2016
prev sibling parent reply Martin Tschierschke <mt smartdolphin.de> writes:
On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be 
 easily generalized to multiple arguments such that swap(a1, a2, 
 ..., an) arranges things such that a1 gets an, a2 gets a1, a3 
 gets a2, etc. I do know applications for three arguments. 
 Thoughts? -- Andrei
A newbee question about language design: When I looked first time at Ruby I liked the simple a,b = b,a syntax, so swap. Would it be theoretically possible to allow this? And if not, where does it breaks the general language design? Best regards mt.
May 25 2016
next sibling parent Brian Schott <briancschott gmail.com> writes:
On Wednesday, 25 May 2016 at 17:08:02 UTC, Martin Tschierschke 
wrote:
 And if not, where does it breaks the general language design?
Here: https://en.wikipedia.org/wiki/Comma_operator
May 25 2016
prev sibling next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 05/25/2016 01:08 PM, Martin Tschierschke wrote:
 On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote:
 So swap(a, b) swaps the contents of a and b. This could be easily
 generalized to multiple arguments such that swap(a1, a2, ..., an)
 arranges things such that a1 gets an, a2 gets a1, a3 gets a2, etc. I
 do know applications for three arguments. Thoughts? -- Andrei
A newbee question about language design: When I looked first time at Ruby I liked the simple a,b = b,a syntax, so swap. Would it be theoretically possible to allow this? And if not, where does it breaks the general language design? Best regards mt.
No fundamental breakage except probably for DRY violation (consider a and b may be arbitrarily complicated expressions yielding lvalues). Also, if expressions are involved everything must be carefully defined so e.g. (a[f()], b[i++]) = (b[i++], a[f()]) defines what operations are executed and in what order. The swap assignment is cute and I used to like it a lot more until I figured adds more problems than it solves. Andrei
May 25 2016
prev sibling parent reply Observer <here nowhere.com> writes:
On Wednesday, 25 May 2016 at 17:08:02 UTC, Martin Tschierschke 
wrote:
 A newbee question about language design:
 When I looked first time at Ruby I liked the simple a,b = b,a 
 syntax,
 so swap. Would it be theoretically possible to allow this?

 And if not, where does it breaks the general language design?
There's something about this notation that immediately makes me think more generally. swap is just the degenerate form of a more-general circular-shift operation in two different dimensions. This form assumes that the shifting stops after only a single shift position (or more generally, that the number of shift positions is odd); and having just two operands makes it unnecessary to specify whether the shifting is to the left or to the right. But even a circular-shift operation is itself just a degenerate form of a more-general arbitrary-permutation operation. Other permutations have common applicability in computer science, such as the bit-reversed addressing used on DSP chips to support butterfly operations in FFT (actually, DFT) algorithms. All of which makes me wonder: if we want to generalize swap, should we go farther than just one algorithmic stage? How about a very general routine that accepts a permutation mapping and a set of arguments, and scrambles the arguments according to the mapping?
May 25 2016
parent Seb <seb wilzba.ch> writes:
On Thursday, 26 May 2016 at 02:17:20 UTC, Observer wrote:
 On Wednesday, 25 May 2016 at 17:08:02 UTC, Martin Tschierschke 
 wrote:
 A newbee question about language design:
 When I looked first time at Ruby I liked the simple a,b = b,a 
 syntax,
 so swap. Would it be theoretically possible to allow this?

 And if not, where does it breaks the general language design?
There's something about this notation that immediately makes me think more generally. swap is just the degenerate form of a more-general circular-shift operation in two different dimensions. This form assumes that the shifting stops after only a single shift position (or more generally, that the number of shift positions is odd); and having just two operands makes it unnecessary to specify whether the shifting is to the left or to the right. But even a circular-shift operation is itself just a degenerate form of a more-general arbitrary-permutation operation. Other permutations have common applicability in computer science, such as the bit-reversed addressing used on DSP chips to support butterfly operations in FFT (actually, DFT) algorithms. All of which makes me wonder: if we want to generalize swap, should we go farther than just one algorithmic stage? How about a very general routine that accepts a permutation mapping and a set of arguments, and scrambles the arguments according to the mapping?
There's indexed, but it doesn't swap - it only provides access based on your permutation and only works if all data has a CommonType. For what it's worth, it's pretty cool to build rangified swaps of ranges - e.g. in combinatorics: http://docs.mir.dlang.io/latest/mir_combinatorics.html
May 26 2016