Idea: swap with multiple arguments
Seb via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 26 05:29:04 PDT 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:
>> 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
More information about the Digitalmars-d
mailing list