Jump to page: 1 2
Thread overview
Is there a way to pipeline program with random-access ranges in C#?
Mar 19, 2018
Dukc
Mar 19, 2018
rumbu
Mar 19, 2018
Dukc
Mar 20, 2018
Kagamin
Mar 20, 2018
Dukc
Mar 20, 2018
Kagamin
Mar 21, 2018
Dukc
Mar 21, 2018
Kagamin
Mar 22, 2018
Dukc
Mar 22, 2018
Kagamin
Mar 22, 2018
Dukc
March 19, 2018
This topic is technically in wrong place, since the problem is with C#, not D. But because what I'm asking is more idiomatic 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 to chain range-evaluating commands like Phobos, but on C# or JavaScript. Either does, because I'm using Bridge.Net to compile C# to JavaScript, but I would prefer C# because of the 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?
March 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 with C#, not D. But because what I'm asking is more idiomatic 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 to chain range-evaluating commands like Phobos, but on C# or JavaScript. Either does, because I'm using Bridge.Net to compile C# to JavaScript, but I would prefer C# because of the 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?
March 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), Tuple.Create) or similar when I need indexing in C# :).

I'm still interested in the answer, though.
March 20, 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.
March 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:
>> 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)
March 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
March 21, 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:
>> 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.
March 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.
March 22, 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:
>> ...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 input-ranges in C# (or Rust, judging by a quick look I recently took) but can be used to fetch one. A bit cumbersome perhaps, but logical. I think I understand it now. Thank you.
March 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 input-ranges in C# (or Rust, judging by a quick look I 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.
« First   ‹ Prev
1 2