www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.range lockstep is not input range but opApply entity. Workarounds?

reply "mist" <none none.none> writes:
I basically want to be able to do stuff like this:
auto result = map!( (a, b) => a+b )( lockstep(range1, range2) );

Are there any standard short ways to wrap an input range around 
struct with opApply (which Lockstep is)?

Also what about redesigning Lockstep as a proper range? I could 
do a pull request but not sure about current intentions.
Dec 29 2012
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-48-29 14:12, mist <none none.none> wrote:

 I basically want to be able to do stuff like this:
 auto result = map!( (a, b) => a+b )( lockstep(range1, range2) );

 Are there any standard short ways to wrap an input range around struct  
 with opApply (which Lockstep is)?

 Also what about redesigning Lockstep as a proper range? I could do a  
 pull request but not sure about current intentions.
Use std.range.zip instead: auto result = map!( (a, b) => a+b )( zip(range1, range2) ); The reason there are two ways is lockstep works better with foreach: foreach (a, b; lockstep(A, B) ) { // Use a and b here. } Contrast with zip: foreach (a; zip(A, B) ) { // Use a[0] and a[1] here. } There have been suggestions to better integrate tuples in the language, so in the future zip may have all the advantages of lockstep (and vice versa), but don't cross your fingers. -- Simen
Dec 29 2012
next sibling parent "mist" <none none.none> writes:
Any objections to documentation update to cross-reference zip and 
lockstep to each other? Was not even searching for first one when 
found lockstep, huh.

On Saturday, 29 December 2012 at 14:19:39 UTC, Simen Kjaeraas 
wrote:
 On 2012-48-29 14:12, mist <none none.none> wrote:

 I basically want to be able to do stuff like this:
 auto result = map!( (a, b) => a+b )( lockstep(range1, range2) 
 );

 Are there any standard short ways to wrap an input range 
 around struct with opApply (which Lockstep is)?

 Also what about redesigning Lockstep as a proper range? I 
 could do a pull request but not sure about current intentions.
Use std.range.zip instead: auto result = map!( (a, b) => a+b )( zip(range1, range2) ); The reason there are two ways is lockstep works better with foreach: foreach (a, b; lockstep(A, B) ) { // Use a and b here. } Contrast with zip: foreach (a; zip(A, B) ) { // Use a[0] and a[1] here. } There have been suggestions to better integrate tuples in the language, so in the future zip may have all the advantages of lockstep (and vice versa), but don't cross your fingers.
Dec 29 2012
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 12/29/2012 06:19 AM, Simen Kjaeraas wrote:

 foreach (a; zip(A, B) ) {
 // Use a[0] and a[1] here.
 }

 There have been suggestions to better integrate tuples in the language,
 so in the future zip may have all the advantages of lockstep (and vice
 versa), but don't cross your fingers.
Tuples have already been integrated at least as much: :) import std.stdio; import std.range; void main() { int[] cats; int[] birds; foreach (cat, bird; zip(cats, birds)) { writeln(cat, bird); } } Ali
Dec 29 2012
parent reply "mist" <none none.none> writes:
Not clever enough to expand like this though:
map!( (a, b) => a+b )( zip(Range1, Range2) );

Using a => a[0]+a[1] is not that big deal though.

On Saturday, 29 December 2012 at 17:58:55 UTC, Ali Çehreli wrote:
 On 12/29/2012 06:19 AM, Simen Kjaeraas wrote:

 foreach (a; zip(A, B) ) {
 // Use a[0] and a[1] here.
 }

 There have been suggestions to better integrate tuples in the
language,
 so in the future zip may have all the advantages of lockstep
(and vice
 versa), but don't cross your fingers.
Tuples have already been integrated at least as much: :) import std.stdio; import std.range; void main() { int[] cats; int[] birds; foreach (cat, bird; zip(cats, birds)) { writeln(cat, bird); } } Ali
Dec 29 2012
parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On 2012-52-29 20:12, mist <none none.none> wrote:

 Not clever enough to expand like this though:
 map!( (a, b) => a+b )( zip(Range1, Range2) );

 Using a => a[0]+a[1] is not that big deal though.
That oughta be doable. However, seeing as std.functional only contains unaryFun and binaryFun (dranges has naryFun), this approach cannot currently extend to tuples with more than two fields. -- Simen
Dec 29 2012