enumerable

understanding comparable mixin and enumerable mixin

依然范特西╮ 提交于 2019-12-05 08:18:43
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? 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 instances? What should be the comparison based on? Is it enough to compare their name? their breed? their DNA? It

Linq statement for an infinite sequence of successive halves

陌路散爱 提交于 2019-12-05 04:53:49
Given a starting number, imagine an infinite sequence of its successive halves. 1, 0.5, 0.25, 0.125, ... (Ignore any numerical instabilities inherent in double .) Can this be done in a single expression without writing any custom extension methods or generator methods? I don't know of a single-expression way but I found this clever generator code here: http://csharpindepth.com/articles/Chapter11/StreamingAndIterators.aspx public static IEnumerable<TSource> Generate<TSource>(TSource start, Func<TSource,TSource> step) { TSource current = start; while (true) { yield return current; current = step

Common Ancestor to Java Array and List

时光怂恿深爱的人放手 提交于 2019-12-04 22:51:13
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? 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 the caller than specifying an array as a method parameter, which forces a single implementation. I agree it's

Ruby : Choosing between each, map, inject, each_with_index and each_with_object

ぃ、小莉子 提交于 2019-12-04 18:07:23
问题 When I started writing Ruby many years ago, it took me a while to understand the difference between each and map. It only got worse when I discovered all the other Enumerable and Array methods. With the help of the official documentation and many StackOverflow questions, I slowly began to understand what those methods did. Here is what took me even longer to understand though : Why should I use one method or another? Are there any guidelines? I hope this question isn't a duplicate : I'm more

Create List of Tuples from List using LINQ

本秂侑毒 提交于 2019-12-04 14:32:00
I'm trying to create a list of tuples from a list using LINQ, but can't work out how to do it. What I've got is various data in an external file, which I'm reading sections using a standard method into a List<Single> , I then need to turn this into lists of groups of sequential elements of this list (which may have a variable number of elements in the groups). To state it another way: List<Single> with n elements goes to List<Tuple<Single, Single>> with (n/2) elements or List<Single> with n elements goes to List<Tuple<Single, Single, Single>> with (n/3) elements I couldn't work out how to do

How can I make DataTable enumerable?

心不动则不痛 提交于 2019-12-04 09:03:30
I cannot use AsEnumerable() on DataTable, I'm using C# 3 but I'm just targeting 2.0 framework (LINQ capability is courtesy of LINQBridge ). Is there any way I can make DataTable enumerable without using Select() ? bool isExisting = (bdsAttachments.DataSource as DataTable).Select().Any(xxx => (string)dr["filename"] == filename); Update: I wanted it to make it look like this: bool isExisting = (bdsAttachments.DataSource as DataTable).AsEnumerable().Any(xxx => (string)dr["filename"] == filename); I'm getting an inkling that the Select method of DataTable returns a copy, I'm thinking to just use

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; } }

Ruby: Is there something like Enumerable#drop that returns an enumerator instead of an array?

拈花ヽ惹草 提交于 2019-12-04 03:15:48
I have some big fixed-width files and I need to drop the header line. Keeping track of an iterator doesn't seem very idiomatic. # This is what I do now. File.open(filename).each_line.with_index do |line, idx| if idx > 0 ... end end # This is what I want to do but I don't need drop(1) to slurp # the file into an array. File.open(filename).drop(1).each_line do { |line| ... } What's the Ruby idiom for this? If you need it more than once, you could write an extension to Enumerator . class Enumerator def enum_drop(n) with_index do |val, idx| next if n == idx yield val end end end File.open(testfile

generic Enumeration to Iterable converter [closed]

☆樱花仙子☆ 提交于 2019-12-03 23:27:23
HttpServletRequest is using a lot of java.util.Enumeration. I would like to use them in for-each, so i need to convert them into interable. this is not a problem, but I since I have more than one project needing this I need a library to do this. I would rather not make my own - is there any standard library that supports this kind of decoration? Is there a built-in construct to convert an Enumeration to an Iterable? java.util.Collections has a list method that copies an Enumeration into a List , which you can then use in a for-each loop (see javadoc ). Asaf Here is the javadoc from Guava

Ruby : Choosing between each, map, inject, each_with_index and each_with_object

喜你入骨 提交于 2019-12-03 12:55:20
When I started writing Ruby many years ago, it took me a while to understand the difference between each and map . It only got worse when I discovered all the other Enumerable and Array methods. With the help of the official documentation and many StackOverflow questions , I slowly began to understand what those methods did. Here is what took me even longer to understand though : Why should I use one method or another? Are there any guidelines? I hope this question isn't a duplicate : I'm more interested in the "Why?" than the "What?" or "How?", and I think it could help Ruby newcomers. A more