Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Dukc via Digitalmars-d-learn

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 
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 inherits from IEnumerable, which is an input range.


Oops. Well, all the better!


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Kagamin via Digitalmars-d-learn

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 inherits from IEnumerable, which is an input range.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-22 Thread Dukc via Digitalmars-d-learn

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, 
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.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-21 Thread Kagamin via Digitalmars-d-learn

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, backward 
iteration can be just a part of chunking.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-21 Thread Dukc via Digitalmars-d-learn

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.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-20 Thread Kagamin via Digitalmars-d-learn

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> Enumerate(this 
IEnumerable range)
{   return range.Zip(Enumerable.Range(0, int.MaxValue), (x, y) 
=> new Sequence(x, y));

}


If you want to have index, there's 
https://msdn.microsoft.com/en-us/library/bb534869(v=vs.110).aspx


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-20 Thread Dukc via Digitalmars-d-learn

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> Enumerate(this 
IEnumerable range)
{   return range.Zip(Enumerable.Range(0, int.MaxValue), (x, y) => 
new Sequence(x, y));

}

(Of course, were I compiling to .net framework instead of 
JavaScript I would have to use Tuple or ValueTuple instead of 
Sequence)


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-20 Thread Kagamin via Digitalmars-d-learn

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.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-19 Thread Dukc via Digitalmars-d-learn

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.


Re: Is there a way to pipeline program with random-access ranges in C#?

2018-03-19 Thread rumbu via Digitalmars-d-learn

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?


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

2018-03-19 Thread Dukc via Digitalmars-d-learn
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?