enumerator

ArgumentError in #new, Subclassing Enumerator

北城余情 提交于 2019-12-06 02:59:42
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? What about: class CuadraticPrimeGenerator < Enumerator def initialize(a=1,b=49) super() do |y| count, x = 0, 1 loop { y.yield(x**2 + a*x + b) } end end end 来源: https://stackoverflow.com/questions/16016995/argumenterror-in

How does Ruby's Enumerator object iterate externally over an internal iterator?

情到浓时终转凉″ 提交于 2019-12-05 22:59:37
问题 As per Ruby's documentation, the Enumerator object uses the each method (to enumerate) if no target method is provided to the to_enum or enum_for methods. Now, let's take the following monkey patch and its enumerator, as an example o = Object.new def o.each yield 1 yield 2 yield 3 end e = o.to_enum loop do puts e.next end Given that the Enumerator object uses the each method to answer when next is called, how do calls to the each method look like, every time next is called? Does the

Interface conflict resolution in C#

南笙酒味 提交于 2019-12-05 11:09:04
This is a spin-off question based on Eric Lippert's answer on this question . I would like to know why the C# language is designed not being able to detect the correct interface member in the following specific case. I am not looking on feedback whether designing a class this way is considered best practice. class Turtle { } class Giraffe { } class Ark : IEnumerable<Turtle>, IEnumerable<Giraffe> { public IEnumerator<Turtle> GetEnumerator() { yield break; } // explicit interface member 'IEnumerable.GetEnumerator' IEnumerator IEnumerable.GetEnumerator() { yield break; } // explicit interface

List<IEnumerator>.All(e => e.MoveNext()) doesn't move my enumerators on

霸气de小男生 提交于 2019-12-05 10:51:38
问题 I'm trying to track down a bug in our code. I've boiled it down to the snippet below. In the example below I have a grid of ints (a list of rows), but I want to find the indexes of the columns that have a 1. The implementation of this is to create an enumerator for each row and step through each column in turn by keeping the enumerators in step. class Program { static void Main(string[] args) { var ints = new List<List<int>> { new List<int> {0, 0, 1}, // This row has a 1 at index 2 new List

Why is there no ReverseEnumerator in C#?

帅比萌擦擦* 提交于 2019-12-05 10:50:35
问题 Does anyone know if there was a specific reason or design decision to not include a reverse enumerator in C#? It would be so nice if there was an equivalent to the C++ reverse_iterator just like Enumerator is the equivalent of the C++ iterator . Collections that can be reverse-iterated would just implement something like IReverseEnumerable and one could do something like: List<int>.ReverseEnumerator ritr = collection.GetReverseEnumerator(); while(rtir.MoveNext()) { // do stuff } This way, you

Is there a way to iterate through HttpServletRequest.getAttributeNames() more than once?

爱⌒轻易说出口 提交于 2019-12-05 06:49:34
I'm trying to log the contents of the HttpServletRequest attributes collection. I need to do this when the servlet first starts, and again right before the servlet is finished. I'm doing this in an attempt to understand a crufty and ill-maintained servlet. Because I need to have as little impact as possible, servlet filters are not an option. So here's the problem. When the servlet starts, I'll iterate through the enumeration returned by HttpServletRequest.getAttributeNames(). However, when I want to iterate through it again, getAttributeNames().hasMoreElements() returns "false"! I can't find

Ruby - Compare two Enumerators elegantly

别来无恙 提交于 2019-12-05 02:01:28
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. The other option is... ugh. def equal_streams?(s1, s2) s1.each do |e1| begin e2 = s2.next return

C# IEnumerable, IEnumerator Reset Function Not Get Called

泪湿孤枕 提交于 2019-12-04 05:04:09
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 void Reset() { position = -1; } public IEnumerator GetEnumerator() { return (IEnumerator)this; } }

How does Ruby's Enumerator object iterate externally over an internal iterator?

左心房为你撑大大i 提交于 2019-12-04 04:54:04
As per Ruby's documentation, the Enumerator object uses the each method (to enumerate) if no target method is provided to the to_enum or enum_for methods. Now, let's take the following monkey patch and its enumerator, as an example o = Object.new def o.each yield 1 yield 2 yield 3 end e = o.to_enum loop do puts e.next end Given that the Enumerator object uses the each method to answer when next is called, how do calls to the each method look like, every time next is called? Does the Enumeartor class pre-load all the contents of o.each and creates a local copy for enumeration? Or is there some

Collection was modified, enumeration operation may not execute

家住魔仙堡 提交于 2019-12-04 04:47:53
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 another thread i modify collection. public readonly ObservableCollectionThreadSafe<GMapMarker> Markers