digitalmars.D - Idea: swap with multiple arguments
- Andrei Alexandrescu (4/4) May 23 2016 So swap(a, b) swaps the contents of a and b. This could be easily
- Steven Schveighoffer (3/7) May 23 2016 One thing that screams out to me: this should be called rotate, not swap...
- Tourist (3/14) May 23 2016 Unless it swaps randomly :)
- Q. Schroll (5/14) May 23 2016 Just name Andrei's function rotate and make swap just an alias of
- Jonathan M Davis via Digitalmars-d (12/18) May 23 2016 That would probably be better. My immediate thought on reading Andrei's
- Observer (32/45) May 23 2016 If you don't see value in it, you've neglected to learn
- Jonathan M Davis via Digitalmars-d (5/7) May 24 2016 I wasn't aware that postscript was a programming language. All I know ab...
- Observer (17/24) May 24 2016 It is a full-fledged programming language, and quite nice for
- Adrian Matoga (4/10) May 24 2016 Forth also has "swap" and "rot". The former moves 2nd value to
- =?UTF-8?Q?Ali_=c3=87ehreli?= (3/10) May 23 2016 Yes, rotate(), but then I would never remember what direction it rotates...
- Steven Schveighoffer (4/14) May 23 2016 Use the law of UFCS:
- Jonathan M Davis via Digitalmars-d (5/18) May 23 2016 Hmmm. And I would have assumed that it rotated in the other direction. T...
- ixid (5/10) May 24 2016 Why would you assume it would rotate left? As a general rule to
- H. S. Teoh via Digitalmars-d (8/19) May 24 2016 And why would you assume that it would rotate right? The default
- Wyatt (22/27) May 24 2016 In the APL family, we have dyadic ⌽ and ⊖:
- NynnFR (8/14) May 24 2016 Wow: I didn't see any APL code for years ! ;)
- Era Scarecrow (20/23) May 24 2016 hmmm rotate left would be a lot easier (and cleaner) to
- Era Scarecrow (6/8) May 23 2016 If we take a cue from assembly instructions, there's rol and ror
- =?UTF-8?B?Tm9yZGzDtnc=?= (7/10) May 25 2016 I agree. So we need
- Andrei Alexandrescu (3/9) May 23 2016 Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments
- Steven Schveighoffer (6/16) May 23 2016 OK, let me rephrase. For 3 arguments, swap is a totally unintuitive name...
- Xinok (11/16) May 23 2016 It doesn't make sense for "swap" to make take than two arguments
- H. S. Teoh via Digitalmars-d (8/12) May 23 2016 As others have said, 'swap' is a horrible name for this. I'd go with
- Piotr Szturmaj (7/16) May 24 2016 I would do it as rotate(RandomAccessRange, ptrdiff_t shift).
- Xinok (27/28) May 24 2016 I wish to make a different point which is more general regarding
- Andrei Alexandrescu (5/7) May 24 2016 I agree. I'd be hard pressed to find a use for swap/rotate with more
- Andrei Alexandrescu (4/8) May 24 2016 This may be a misunderstanding. The original discussion was about
- Xinok (9/20) May 24 2016 No misunderstanding; I realize that what you're proposing is a
- Martin Tschierschke (7/12) May 25 2016 A newbee question about language design:
- Brian Schott (3/4) May 25 2016 Here: https://en.wikipedia.org/wiki/Comma_operator
- Andrei Alexandrescu (10/20) May 25 2016 No fundamental breakage except probably for DRY violation (consider a
- Observer (19/24) May 25 2016 There's something about this notation that immediately makes
- Seb (8/38) May 26 2016 There's indexed, but it doesn't swap - it only provides access
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
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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap. -Steve
May 23 2016
On Monday, 23 May 2016 at 20:27:43 UTC, Steven Schveighoffer wrote:On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Unless it swaps randomly :)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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap. -Steve
May 23 2016
On Monday, 23 May 2016 at 20:27:43 UTC, Steven Schveighoffer wrote:On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Just name Andrei's function rotate and make swap just an alias of it with exactly two parameters. No confusion and everyone is happy.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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap. -Steve
May 23 2016
On Monday, May 23, 2016 16:27:43 Steven Schveighoffer via Digitalmars-d wrote:On 5/23/16 4:01 PM, Andrei Alexandrescu wrote: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 DavisSo 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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap.
May 23 2016
On Monday, 23 May 2016 at 21:38:54 UTC, Jonathan M Davis wrote: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.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.
May 23 2016
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
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: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).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.
May 24 2016
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
On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Yes, rotate(), but then I would never remember what direction it rotates. AliSo 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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap. -Steve
May 23 2016
On 5/23/16 5:47 PM, Ali Çehreli wrote:On 05/23/2016 01:27 PM, Steven Schveighoffer wrote:Use the law of UFCS: x.rotate(y, z); // x-> y, y -> z, z -> x -SteveOn 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Yes, rotate(), but then I would never remember what direction it rotates.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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap.
May 23 2016
On Monday, May 23, 2016 18:10:02 Steven Schveighoffer via Digitalmars-d wrote:On 5/23/16 5:47 PM, Ali ehreli 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 DavisOn 05/23/2016 01:27 PM, Steven Schveighoffer wrote:Use the law of UFCS: x.rotate(y, z); // x-> y, y -> z, z -> xOn 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Yes, rotate(), but then I would never remember what direction it rotates.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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap.
May 23 2016
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 DavisWhy 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
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: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.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 DavisWhy 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
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
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[...]-WyattWow: 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
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
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
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. AliI agree. So we need rotate{Forward,Backward} or rotate{Left,Right} then. I vote for the former case.
May 25 2016
On 05/23/2016 04:27 PM, Steven Schveighoffer wrote:On 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments "rotate" is overkill. -- AndreiSo 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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap.
May 23 2016
On 5/23/16 6:22 PM, Andrei Alexandrescu wrote:On 05/23/2016 04:27 PM, Steven Schveighoffer wrote: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. -SteveOn 5/23/16 4:01 PM, Andrei Alexandrescu wrote:Problem is the usefulness peaks at 2-3 args, and for 2 and 3 arguments "rotate" is overkill. -- AndreiSo 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? -- AndreiOne thing that screams out to me: this should be called rotate, not swap.
May 23 2016
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? -- AndreiIt 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
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? -- AndreiAs 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
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: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.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? -- AndreiAs 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.
May 24 2016
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
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
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#.bringToFrontThis 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
On Tuesday, 24 May 2016 at 18:51:32 UTC, Andrei Alexandrescu wrote:On 05/24/2016 02:48 PM, Xinok wrote: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.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#.bringToFrontThis 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
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? -- AndreiA 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
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
On 05/25/2016 01:08 PM, Martin Tschierschke wrote:On Monday, 23 May 2016 at 20:01:08 UTC, Andrei Alexandrescu wrote: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. AndreiSo 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? -- AndreiA 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
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
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: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.htmlA 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 26 2016