enumerable

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

Ruby Enumerables — what are they exactly?

两盒软妹~` 提交于 2019-12-09 03:42:26
Can someone explain in the most basic, laymans terms what a Ruby Enumerable is? I'm very new to coding and just starting to work with arrays and hashes. I read the word "Enumerables" everywhere but I don't understand what they are. Can someone explain in the most basic, laymans terms what a Ruby Enumerable is? It's a module that defines a bunch of methods, and when another class includes that module, those methods are available in that class. So if someone uses a method like each_with_index on an Array, and you say to yourself, "I wonder how that method works. I'll check the Array docs.", you

Why does Enumerable not have a length attribute in Ruby?

坚强是说给别人听的谎言 提交于 2019-12-08 18:39:52
问题 At least in Ruby 1.9.3, Enumerable objects do not have a length attribute. As far as I can tell, anything Enumerable is a set, as evidenced by methods like sort and find_index . A set always has a well-defined length (...right?), so why is this not a property? 回答1: Enumerable has the count method, which is usually going to be the intuitive "length" of the enumeration. But why not call it "length"? Well, because it operates very differently. In Ruby's built-in data structures like Array and

Select records which has no day-off throughout the week in List<T> - C#

泪湿孤枕 提交于 2019-12-08 15:04:39
问题 I have an Employee class which defined as this: Employee { public int Id { get; set; } public string Name { get; set; } public DateTime WorkDate { get; set; } public bool isOff { get; set; } } This is my class implementation and usage: List<Employee> workers = new List<Employee>() { new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false}, new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false}, new Employee {

Switching the receiver and the argument of `Enumerable#inject`

风格不统一 提交于 2019-12-08 09:03:35
问题 When Enumerable#inject is used, most of the times, we want the result to be the same class (and often the same object) as the initial object that appears as the argument of inject . For example, we use it like: [1, 2, 3, 4, 5] .inject(5){|i, e| i += e} # => 20 [3, 4, 5] .inject(1 => 1, 2 => 1){|h, e| h[e] = h[e - 1] + h[e - 2]; h} # => {1=>1, 2=>1, 3=>2, 4=>3, 5=>5} From the point of view that this operation takes the initial value and modifies it into the final output, it would be more

understanding comparable mixin and enumerable mixin

旧城冷巷雨未停 提交于 2019-12-07 04:01:28
问题 I am a newbie and learning ruby. Would like to have a better understanding of the question asked. I don't understand the use of comparable mixin and enumerable mixin. I mean we don't include these in our class when we need to use them, right? if we want to compare two objects we simply write x > y. Then what is the use of explicitly using them? 回答1: Great Question Akash! Sometimes it's not "simple" how two objects can be compared! What if you have a Dog class? How do you compare two Dog

Common Ancestor to Java Array and List

风流意气都作罢 提交于 2019-12-06 17:14:34
问题 In .NET, both array and list have Enumerable as ancestor, so a method that accept Enumerable as an argument can receive both array and list as its argument. I wonder if there is a similar thing in Java? 回答1: No, there's no equivalent in Java. I would generally suggest that you design API methods to receive List<T> , Collection<T> or Iterable<T> . While these preclude directly calling the method with an array, you can wrap an array very easily using Arrays.asList . This is more flexible for

Does Hash override Enumerable#map()?

徘徊边缘 提交于 2019-12-06 00:20:32
Given that map() is defined by Enumerable , how can Hash#map yield two variables to its block? Does Hash override Enumerable#map() ? Here's a little example, for fun: ruby-1.9.2-p180 :001 > {"herp" => "derp"}.map{|k,v| k+v} => ["herpderp"] It doesn't override map Hash.new.method(:map).owner # => Enumerable It yields two variables which get collected into an array class Nums include Enumerable def each yield 1 yield 1, 2 yield 3, 4, 5 end end Nums.new.to_a # => [1, [1, 2], [3, 4, 5]] Given that map() is defined by Enumerable , how can Hash#map yield two variables to its block? It doesn't. It

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

元气小坏坏 提交于 2019-12-05 17:31:50
I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it). Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used? The int parameter represents the index of the current item within the current iteration . Each time you call one of the LINQ

Ruby Counting chars in a sequence not using regex

自古美人都是妖i 提交于 2019-12-05 12:32:13
Need help with this code on counting chars in a sequence. This is what I want: word("aaabbcbbaaa") == [["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]] word("aaaaaaaaaa") == [["a", 10]] word("") == [] Here is my code: def word(str) words=str.split("") count = Hash.new(0) words.map {|char| count[char] +=1 } return count end I got word("aaabbcbbaaa") => [["a", 6], ["b", 4], ["c", 1]], which is not what I want. I want to count each sequence. I prefer a none regex solution. Thanks. Split string by chars, then group chunks by char, then count chars in chunks: def word str str .chars .chunk{ |e| e