digitalmars.D.learn - Adjacent Pairs Range
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (4/4) Sep 12 2015 How do I most elegantly iterate all the adjacent pairs in an
- Bahman Movaqar (9/15) Sep 12 2015 That's call `collate`ing IIRC.
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (3/15) Sep 12 2015 InputRange please, not RandomAccessRanges ;)
- Bahman Movaqar (35/36) Sep 12 2015 Oops! Here's one using only `InputRange` interface:
- Bahman Movaqar (5/6) Sep 12 2015 I believe I need to warn you that I'm just learning D; so take my
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (6/9) Sep 12 2015 I wrote my own as adjacentTuples and adjacentPairs:
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/6) Sep 12 2015 Should this go into Phobos?
- deed (4/8) Sep 12 2015 Why not just:
- =?UTF-8?B?Tm9yZGzDtnc=?= (3/5) Sep 13 2015 Assumes arrays. Better is
- Sebastiaan Koppe (12/16) Sep 13 2015 What about using zip and a slice?
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (8/9) Sep 14 2015 Slicing requires a RandomAccessRange (Array). This is too
- Per =?UTF-8?B?Tm9yZGzDtnc=?= (3/4) Sep 14 2015 Correction:
- Sebastiaan Koppe (5/14) Sep 15 2015 dropOne then.
How do I most elegantly iterate all the adjacent pairs in an `InputRange` using Phobos? Something like [1,2,3,4] => [(1,2), (2,3), (3,4)]
Sep 12 2015
On 09/12/2015 02:47 PM, "Nordlöw" wrote:How do I most elegantly iterate all the adjacent pairs in an `InputRange` using Phobos? Something like [1,2,3,4] => [(1,2), (2,3), (3,4)]That's call `collate`ing IIRC. A quick solution would be using `std.range.transposed`: auto a = [1,2,3,4]; auto ll = [a, a[1..$]]; transpose(ll); // returns [[1, 2], [2, 3], [3, 4], [4]] Though you have to take care of the dangling last element yourself. -- Bahman Movaqar
Sep 12 2015
On Saturday, 12 September 2015 at 10:35:41 UTC, Bahman Movaqar wrote:On 09/12/2015 02:47 PM, "Nordlöw" wrote:InputRange please, not RandomAccessRanges ;)How do I most elegantly iterate all the adjacent pairs in an `InputRange` using Phobos? Something like [1,2,3,4] => [(1,2), (2,3), (3,4)]That's call `collate`ing IIRC. A quick solution would be using `std.range.transposed`: auto a = [1,2,3,4]; auto ll = [a, a[1..$]]; transpose(ll); // returns [[1, 2], [2, 3], [3, 4], [4]]
Sep 12 2015
On 09/12/2015 03:09 PM, "Nordlöw" wrote:InputRange please, not RandomAccessRanges ;)Oops! Here's one using only `InputRange` interface: T[][] collate(T)(T[] a) { alias CollateResult = Tuple!(T[][], "result", T, "tlHd"); CollateResult _collate(CollateResult collres) { if (!a.empty) { auto newTlHd = a.front; a.popFront(); return _collate( CollateResult( collres.result ~ [collres.tlHd, newTlHd], newTlHd ) ); } else { return collres; } } if (!a.empty) { auto tlHd = a.front; a.popFront(); return _collate( CollateResult([], tlHd) ).result; } else { return []; } } unittest { writeln([10, 20, 30].collate!int); } -- Bahman Movaqar
Sep 12 2015
On 09/12/2015 04:04 PM, Bahman Movaqar wrote:Oops! Here's one using only `InputRange` interface:I believe I need to warn you that I'm just learning D; so take my solution at your own risk :-) -- Bahman Movaqar
Sep 12 2015
On Saturday, 12 September 2015 at 11:34:03 UTC, Bahman Movaqar wrote:On 09/12/2015 03:09 PM, "Nordlöw" wrote:I wrote my own as adjacentTuples and adjacentPairs: https://github.com/nordlow/justd/blob/master/range_ex.d#L702 Note: No yet extended to N > 2. An alternative naming would be overlappingTuples/Pairs.InputRange please, not RandomAccessRanges ;)Oops! Here's one using only `InputRange` interface:
Sep 12 2015
On Saturday, 12 September 2015 at 11:46:55 UTC, Nordlöw wrote:I wrote my own as adjacentTuples and adjacentPairs: https://github.com/nordlow/justd/blob/master/range_ex.d#L702 Note: No yet extended to N > 2. An alternative naming would be overlappingTuples/Pairs.Should this go into Phobos?
Sep 12 2015
On Saturday, 12 September 2015 at 10:17:19 UTC, Nordlöw wrote:How do I most elegantly iterate all the adjacent pairs in an `InputRange` using Phobos? Something like [1,2,3,4] => [(1,2), (2,3), (3,4)]Why not just: zip(arr[0 .. $-1], arr[1 .. $]) ?
Sep 12 2015
On Sunday, 13 September 2015 at 01:49:56 UTC, deed wrote:zip(arr[0 .. $-1], arr[1 .. $]) ?Assumes arrays. Better is zip(arr.dropOne, arr)
Sep 13 2015
On Saturday, 12 September 2015 at 10:17:19 UTC, Nordlöw wrote:How do I most elegantly iterate all the adjacent pairs in an `InputRange` using Phobos? Something like [1,2,3,4] => [(1,2), (2,3), (3,4)]What about using zip and a slice? ``` void main() { auto a = [1,2,3,4]; import std.range : zip; import std.stdio; writeln(a.zip(a[1..$])); // [Tuple!(int, int)(1, 2), Tuple!(int, int)(2, 3), Tuple!(int, int)(3, 4)] } ```
Sep 13 2015
On Monday, 14 September 2015 at 05:37:05 UTC, Sebastiaan Koppe wrote:What about using zip and a slice?Slicing requires a RandomAccessRange (Array). This is too restrictive. We want to change operations such as adjacentTuples with for example map and reduce without the need for temporary copies of the whole range. This is the thing about D's standard library. Read up on D's range concepts.
Sep 14 2015
On Monday, 14 September 2015 at 10:45:52 UTC, Per Nordlöw wrote:restrictive. We want to change operations such asCorrection: We want to *chain* operations such as...
Sep 14 2015
On Monday, 14 September 2015 at 10:45:52 UTC, Per Nordlöw wrote:On Monday, 14 September 2015 at 05:37:05 UTC, Sebastiaan Koppe wrote:dropOne then. I saw your adjacentTuples. Two questions: a) can't you use a ringbuffer instead of copy when N > 2? b) shouldn't front() return a range over that ringbuffer?What about using zip and a slice?Slicing requires a RandomAccessRange (Array). This is too restrictive. We want to change operations such as adjacentTuples with for example map and reduce without the need for temporary copies of the whole range. This is the thing about D's standard library. Read up on D's range concepts.
Sep 15 2015