www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a way to pipeline program with random-access ranges in C#?

reply Dukc <ajieskola gmail.com> writes:
This topic is technically in wrong place, since the problem is 

D than elsewhere, I think I have the best changes to get 
understood here.

So, I'm looking for some library, or technique, that allows me to 

JavaScript. Either does, because I'm using Bridge.Net to compile 

system.

LinQ is otherwise just what I described, except that it can work 
only with input ranges that can be reseted to their initial 
state. That leads me to do a lot of for loops. In D I virtually 
never need them.

I am wondering, does anybody know an alternative which can 
forward forwarding/bidirectional/random-access capability of 
source ranges too?
Mar 19 2018
parent reply rumbu <rumbu rumbu.ro> writes:
On Monday, 19 March 2018 at 11:35:46 UTC, Dukc wrote:
 This topic is technically in wrong place, since the problem is 

 in D than elsewhere, I think I have the best changes to get 
 understood here.

 So, I'm looking for some library, or technique, that allows me 

 JavaScript. Either does, because I'm using Bridge.Net to 

 type system.

 LinQ is otherwise just what I described, except that it can 
 work only with input ranges that can be reseted to their 
 initial state. That leads me to do a lot of for loops. In D I 
 virtually never need them.

 I am wondering, does anybody know an alternative which can 
 forward forwarding/bidirectional/random-access capability of 
 source ranges too?
Sorry, but I fail to understand your requirements. Do you have a practical example?
Mar 19 2018
parent reply Dukc <ajieskola gmail.com> writes:
On Monday, 19 March 2018 at 14:41:27 UTC, rumbu wrote:
 Sorry, but I fail to understand your requirements. Do you have 
 a practical example?
Doing this without writing a loop or using arrays for memoizing half-finished calculations: public static int Foo(int[] input) { int result = 10; for (int i = input.Length / 4; i >= 0; i -= 4) { int sum = 0; for (int j = i; j < i +4 && j < input.Length; j++) sum += input[j]; sum *= i; result = (result + sum) / 2; } return result; } To be honest, this is as artificial as it looks. When I look again at my code, it seems that my main problem was that I have not made a habit to input.Zip(Enumerable.Range(0, inputLength), I'm still interested in the answer, though.
Mar 19 2018
parent reply Kagamin <spam here.lot> writes:
On Monday, 19 March 2018 at 17:33:31 UTC, Dukc wrote:
 public static int Foo(int[] input)
 {   int result = 10;
     for (int i = input.Length / 4; i >= 0; i -= 4)
     {   int sum = 0;
         for (int j = i; j < i +4 && j < input.Length; j++) sum 
 += input[j];
         sum *= i;
         result = (result + sum) / 2;
     }
     return result;
 }
Looks like you need to partition, select, aggregate and another aggregate.
Mar 20 2018
parent reply Dukc <ajieskola gmail.com> writes:
On Tuesday, 20 March 2018 at 08:05:14 UTC, Kagamin wrote:
 On Monday, 19 March 2018 at 17:33:31 UTC, Dukc wrote:
 public static int Foo(int[] input)
 {   int result = 10;
     for (int i = input.Length / 4; i >= 0; i -= 4)
     {   int sum = 0;
         for (int j = i; j < i +4 && j < input.Length; j++) sum 
 += input[j];
         sum *= i;
         result = (result + sum) / 2;
     }
     return result;
 }
Looks like you need to partition, select, aggregate and another aggregate.
Won't quite do it, because that would not iterate backwards. But anyway, I made this extension function today which solves most of my problems, albeit not that one above: public static IEnumerable<Sequence<T, int>> Enumerate<T>(this IEnumerable<T> range) { return range.Zip(Enumerable.Range(0, int.MaxValue), (x, y) => new Sequence<T, int>(x, y)); } (Of course, were I compiling to .net framework instead of JavaScript I would have to use Tuple or ValueTuple instead of Sequence)
Mar 20 2018
parent reply Kagamin <spam here.lot> writes:
On Tuesday, 20 March 2018 at 15:06:14 UTC, Dukc wrote:
 Won't quite do it, because that would not iterate backwards.
Linq has no chunking, so you would need to write it, maybe similar to SelectMany, but with the opposite meaning.
 public static IEnumerable<Sequence<T, int>> Enumerate<T>(this 
 IEnumerable<T> range)
 {   return range.Zip(Enumerable.Range(0, int.MaxValue), (x, y) 
 => new Sequence<T, int>(x, y));
 }
If you want to have index, there's https://msdn.microsoft.com/en-us/library/bb534869(v=vs.110).aspx
Mar 20 2018
parent reply Dukc <ajieskola gmail.com> writes:
On Tuesday, 20 March 2018 at 15:57:16 UTC, Kagamin wrote:
 On Tuesday, 20 March 2018 at 15:06:14 UTC, Dukc wrote:
 Won't quite do it, because that would not iterate backwards.
Linq has no chunking, so you would need to write it, maybe similar to SelectMany, but with the opposite meaning.
...except that IEnumerables cannot popBack(), so can only do that by doing an additional copy and reversing that. But I quess there's no better alternative, short of doing it C-style...
 If you want to have index, there's 
 https://msdn.microsoft.com/en-us/library/bb534869(v=vs.110).aspx
Wow, didn't know that. Thanks, It'll be useful.
Mar 21 2018
parent reply Kagamin <spam here.lot> writes:
On Wednesday, 21 March 2018 at 07:40:01 UTC, Dukc wrote:
 ...except that IEnumerables cannot popBack(), so can only do 
 that by doing an additional copy and reversing that. But I 
 quess there's no better alternative, short of doing it 
 C-style...
A random access range would be represented as IList<T>, backward iteration can be just a part of chunking.
Mar 21 2018
parent reply Dukc <ajieskola gmail.com> writes:
On Wednesday, 21 March 2018 at 08:40:25 UTC, Kagamin wrote:
 On Wednesday, 21 March 2018 at 07:40:01 UTC, Dukc wrote:
 ...except that IEnumerables cannot popBack(), so can only do 
 that by doing an additional copy and reversing that. But I 
 quess there's no better alternative, short of doing it 
 C-style...
A random access range would be represented as IList<T>, backward iteration can be just a part of chunking.
So the real difference is that random-accesss ranges are not took) but can be used to fetch one. A bit cumbersome perhaps, but logical. I think I understand it now. Thank you.
Mar 22 2018
parent reply Kagamin <spam here.lot> writes:
On Thursday, 22 March 2018 at 09:04:12 UTC, Dukc wrote:
 So the real difference is that random-accesss ranges are not 

 took) but can be used to fetch one. A bit cumbersome perhaps, 
 but logical. I think I understand it now. Thank you.
IList<T> inherits from IEnumerable<T>, which is an input range.
Mar 22 2018
parent Dukc <ajieskola gmail.com> writes:
On Thursday, 22 March 2018 at 09:59:28 UTC, Kagamin wrote:
 On Thursday, 22 March 2018 at 09:04:12 UTC, Dukc wrote:
 So the real difference is that random-accesss ranges are not 

 recently took) but can be used to fetch one. A bit cumbersome 
 perhaps, but logical. I think I understand it now. Thank you.
IList<T> inherits from IEnumerable<T>, which is an input range.
Oops. Well, all the better!
Mar 22 2018