May 25, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | 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 26, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Tschierschke | 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 26, 2016 Re: Idea: swap with multiple arguments | ||||
---|---|---|---|---|
| ||||
Posted in reply to Observer | 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. http://dlang.org/phobos/std_range.html#.indexed 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 |
Copyright © 1999-2021 by the D Language Foundation