enumerator

Best way to convert a non-generic collection to generic collection

痞子三分冷 提交于 2019-12-04 02:55:01
What is the best way to convert a non-generic collection to a generic collection? Is there a way to LINQ it? I have the following code. public class NonGenericCollection:CollectionBase { public void Add(TestClass a) { List.Add(a); } } public class ConvertTest { public static List<TestClass> ConvertToGenericClass( NonGenericCollection collection) { // Ask for help here. } } Thanks! Mark Brackett Since you can guarantee they're all TestClass instances, use the LINQ Cast<T> method : public static List<TestClass> ConvertToGenericClass(NonGenericCollection collection) { return collection.Cast

Why is there no ReverseEnumerator in C#?

北慕城南 提交于 2019-12-03 22:52:24
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 would be able to iterate Lists and LinkedLists in the same way rather than using indexer for one and

What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?

自古美人都是妖i 提交于 2019-12-03 09:48:10
问题 I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source: http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html However I don't completely get it. Can someone elaborate this? Perhaps with an example? Thanks for looking in! 回答1: Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException. Set keys = hashMap.keySet();

escaping the .each { } iteration early in Ruby

一笑奈何 提交于 2019-12-03 09:20:38
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? 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 } Sarah Vessels There is no ++ operator in Ruby. It's also convention to use do and end for multi-line blocks. Modifying your

What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?

偶尔善良 提交于 2019-12-03 03:10:19
I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source: http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html However I don't completely get it. Can someone elaborate this? Perhaps with an example? Thanks for looking in! evanwong Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException. Set keys = hashMap.keySet(); for (Object key : keys) { hashMap.put(someObject, someValue); //it will throw the

How can I make a ruby enumerator that does lazy iteration through two other enumerators?

ε祈祈猫儿з 提交于 2019-12-01 15:54:37
问题 Let's say I have two enumerators, enum1 and enum2 that must be lazily iterated through (because they have side effects). How do I construct a third enumerator enum3 where enum3.each{|x| x} would lazily return the equivalent of enum1 + enum2 ? In my real world use case, I'm streaming in two files, and need to stream out the concatenation. 回答1: This seems to work just how I want; enums.lazy.flat_map{|enum| enum.lazy } Here's the demonstration. Define these yielding methods with side-effects;

Thread safe Enumerator in Ruby

邮差的信 提交于 2019-11-30 19:44:45
TLDR: Is there a thread-safe version of the Enumerator class in Ruby? What I'm trying to do: I have a method in a Ruby On Rails application that I wanted to run concurrently. The method is supposed to create a zip file containing reports from the site, where each file in the zip is a PDF. The conversion from html to PDF is somewhat slow, thus the desire to multi-thread. How I expected to do it: I wanted to use 5 threads, so I figured I would have a shared Enumerator between the threads. Each thread would pop a value from the Enumerator, and run do stuff to it. Here's how I was thinking it

What is the purpose of the Enumerator class in Ruby

点点圈 提交于 2019-11-30 14:59:53
If I create an Enumertor like so: enum = [1,2,3].each => #<Enumerator: [1, 2, 3]:each> enum is an Enumerator. What is the purpose of this object? I can't say this: enum { |i| puts i } But I can say this: enum.each { |i| puts i } That seems redundant, because the Enumerator was created with .each . It seems like it's storing some data regarding the each method. I don't understand what's going on here. I'm sure there is some logical reason we have this Enumerator class, but what can it do that an Array can't? I thought maybe it was an ancestor of Array and other Enumerables, but it doesn't seem

Type signature in a where clause

一个人想着一个人 提交于 2019-11-30 08:06:12
问题 I've written a function similar to Data.Enumerator.List.map that makes an Iteratee compatible with an Enumerator that feeds a different Stream type. import Data.Enumerator test :: Monad m => (ao -> ai) -> Iteratee ai m b -> Iteratee ao m b test f iter = go $$ iter where go (Continue k) = continue $ \stream -> go $$ k (fmap f stream) go (Yield res _) = yield res EOF If I omit the type signature for go , this will work just fine. However, I'd like to include it but I'm unable to determine what

What is the purpose of the Enumerator class in Ruby

非 Y 不嫁゛ 提交于 2019-11-29 20:02:35
问题 If I create an Enumertor like so: enum = [1,2,3].each => #<Enumerator: [1, 2, 3]:each> enum is an Enumerator. What is the purpose of this object? I can't say this: enum { |i| puts i } But I can say this: enum.each { |i| puts i } That seems redundant, because the Enumerator was created with .each . It seems like it's storing some data regarding the each method. I don't understand what's going on here. I'm sure there is some logical reason we have this Enumerator class, but what can it do that