enumerator

“Unzip” IEnumerable dynamically in C# or best alternative

戏子无情 提交于 2019-12-10 13:06:53
问题 Lets assume you have a function that returns a lazily-enumerated object: struct AnimalCount { int Chickens; int Goats; } IEnumerable<AnimalCount> FarmsInEachPen() { .... yield new AnimalCount(x, y); .... } You also have two functions that consume two separate IEnumerable s, for example: ConsumeChicken(IEnumerable<int>); ConsumeGoat(IEnumerable<int>); How can you call ConsumeChicken and ConsumeGoat without a) converting FarmsInEachPen() ToList() beforehand because it might have two zillion

Ruby - Compare two Enumerators elegantly

纵然是瞬间 提交于 2019-12-10 03:24:23
问题 I've got two long streams of numbers coming from two different sources (binary data) in Ruby (1.9.2). The two sources are encapsulated in the form of two Enumerators. I want to check that the two streams are exactly equal. I've come with a couple solutions, but both seem quite inelegant. The first one simply transforms both into an array: def equal_streams?(s1, s2) s1.to_a == s2.to_a end This works, but it is not very performant, memory-wise, specially if the streams have lots of information.

Scalaz 7 Iteratee to process large zip file (OutOfMemoryError)

随声附和 提交于 2019-12-09 18:20:25
问题 I'm trying to use the scalaz iteratee package to process a large zip file in constant space. I have a long-running process I need to perform on each file in the zip file. Those processes can (and should) be run in parallel. I created an EnumeratorT that inflates each ZipEntry into a File object. The signature looks like: def enumZipFile(f:File):EnumeratorT[IoExceptionOr[IO[File]], IO] I want to attach an IterateeT that will perform the long-running process on each file. I basically end up

Collection was modified, enumeration operation may not execute

拟墨画扇 提交于 2019-12-09 17:14:30
问题 I have multithreads application and i get this error ************** Exception Text ************** System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List`1.Enumerator.MoveNextRare() at System.Collections.Generic.List`1.Enumerator.MoveNext() ... I probably have problem with my collection, because on one thread i read my collection and on

C# IEnumerable, IEnumerator Reset Function Not Get Called

自作多情 提交于 2019-12-09 17:12:16
问题 I'm basicly trying to make my class able to iterate using foreach . I read this tutorial. MSDN. It seems very straight forward. However, I have a problem when I want to iterate second time. I debugged it; and it turned out that it doesn't call the Reset() function. Class A class A : IEnumerable, IEnumerator { int[] data = { 0, 1, 2, 3, 4 }; int position = -1; public object Current { get { return data[position]; } } public bool MoveNext() { position++; return (position < data.Length); } public

escaping the .each { } iteration early in Ruby

限于喜欢 提交于 2019-12-09 07:24:20
问题 code: c = 0 items.each { |i| puts i.to_s # if c > 9 escape the each iteration early - and do not repeat c++ } I want to grab the first 10 items then leave the "each" loop. What do I replace the commented line with? is there a better approach? something more Ruby idiomatic? 回答1: While the break solution works, I think a more functional approach really suits this problem. You want to take the first 10 elements and print them so try items.take(10).each { |i| puts i.to_s } 回答2: There is no ++

Next key in C# Dictionary

假如想象 提交于 2019-12-08 18:46:54
问题 How to get an Enumerator to an item in a -Sorted- dictionary using key? Note: GetEnumerator() gets an Enumerator to first element.. But I need to get an Enumerator to the element with a given key in order to gain access to next elements using MoveNext() for example... Edit: Or a way to access next elements... Edit: I prefer a const time method... Thanks 回答1: var enumerator = dictionary.Keys.SkipWhile(k => k != myKey) Where myKey is the key you're looking for. And you can use the OrderBy

ArgumentError in #new, Subclassing Enumerator

孤街浪徒 提交于 2019-12-07 13:59:30
问题 I'm subclassing Enumerator like this: class CuadraticPrimeGenerator < Enumerator def initialize(a=1,b=49) super do |y| x = 1 loop do y << x**2 + a*x + b x += 1 end end end end However... > CuadraticPrimeGenerator.new(1,49) 0027.rb:41:in `initialize': 49 is not a symbol (TypeError) from 0027.rb:41:in `initialize' from 0027.rb:48:in `new' from 0027.rb:48:in `<main>' Thoughts? 回答1: What about: class CuadraticPrimeGenerator < Enumerator def initialize(a=1,b=49) super() do |y| count, x = 0, 1 loop

C# How to make a recursive version of GetEnumerator()

时光怂恿深爱的人放手 提交于 2019-12-07 04:02:50
问题 Can somebody give me advice on how to create a recursive version of GetEnumerator()? The well-known Towers of Hanoi problem may serve as an example that is comparable to the actual problem I have. A simple algorithm to show all moves for a stack of disks of height n is: void MoveTower0 (int n, Needle start, Needle finish, Needle temp) { if (n > 0) { MoveTower0 (n - 1, start, temp, finish); Console.WriteLine ("Moving disk from {0} to {1}", start, finish); MoveTower0 (n - 1, temp, finish, start

Where does a generic List<> implement Reset?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 11:00:22
when I go to the definition of List<> I can see it has a public struct Enumerator that implements the interfaces IEnumerator<T> , IDisposable and IEnumerator. IEnumerator should force the implementation of Reset - besides Current and MoveNext. Yet only Current and MoveNext are implemented. How can that be? Where do I find the Reset() of List<>? var list = new List<int>(); list.Add(23); list.Add(44); var Enumerator = list.GetEnumerator(); while (Enumerator.MoveNext()) { Console.WriteLine(Enumerator.Current); } Enumerator. And when I try it in code there is no Reset(): Ok - I tried to show a