digitalmars.D.learn - Is there a way to pipeline program with random-access ranges in C#?
- Dukc (16/16) Mar 19 2018 This topic is technically in wrong place, since the problem is
- rumbu (3/19) Mar 19 2018 Sorry, but I fail to understand your requirements. Do you have a
- Dukc (19/21) Mar 19 2018 Doing this without writing a loop or using arrays for memoizing
- Kagamin (3/14) Mar 20 2018 Looks like you need to partition, select, aggregate and another
- Dukc (12/26) Mar 20 2018 Won't quite do it, because that would not iterate backwards.
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
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
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
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
On Tuesday, 20 March 2018 at 08:05:14 UTC, Kagamin wrote:On Monday, 19 March 2018 at 17:33:31 UTC, Dukc wrote: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)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
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
On Tuesday, 20 March 2018 at 15:57:16 UTC, Kagamin wrote:On Tuesday, 20 March 2018 at 15:06:14 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...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.If you want to have index, there's https://msdn.microsoft.com/en-us/library/bb534869(v=vs.110).aspxWow, didn't know that. Thanks, It'll be useful.
Mar 21 2018
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
On Wednesday, 21 March 2018 at 08:40:25 UTC, Kagamin wrote:On Wednesday, 21 March 2018 at 07:40:01 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....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 22 2018
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
On Thursday, 22 March 2018 at 09:59:28 UTC, Kagamin wrote:On Thursday, 22 March 2018 at 09:04:12 UTC, Dukc wrote:Oops. Well, all the better!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.
Mar 22 2018