enumerable

What is the difference between map, each, and collect? [duplicate]

落爺英雄遲暮 提交于 2019-11-27 03:06:21
This question already has an answer here: what's different between each and collect method in Ruby [duplicate] 7 answers In Ruby, is there any difference between the functionalities of each , map , and collect ? each is different from map and collect , but map and collect are the same (technically map is an alias for collect , but in my experience map is used a lot more frequently). each performs the enclosed block for each element in the ( Enumerable ) receiver: [1,2,3,4].each {|n| puts n*2} # Outputs: # 2 # 4 # 6 # 8 map and collect produce a new Array containing the results of the block

Why is Enumerable.Range faster than a direct yield loop?

一世执手 提交于 2019-11-27 02:19:05
问题 The code below is checking performance of three different ways to do same solution. public static void Main(string[] args) { // for loop { Stopwatch sw = Stopwatch.StartNew(); int accumulator = 0; for (int i = 1; i <= 100000000; ++i) { accumulator += i; } sw.Stop(); Console.WriteLine("time = {0}; result = {1}", sw.ElapsedMilliseconds, accumulator); } //Enumerable.Range { Stopwatch sw = Stopwatch.StartNew(); var ret = Enumerable.Range(1, 100000000).Aggregate(0, (accumulator, n) => accumulator

Group hashes by keys and sum the values

亡梦爱人 提交于 2019-11-26 21:54:01
I have an array of hashes: [{"Vegetable"=>10}, {"Vegetable"=>5}, {"Dry Goods"=>3>}, {"Dry Goods"=>2}] I need to use inject here I think but I've really been struggling. I want a new hash that reflects the sum of the previous hash's duplicate keys: [{"Vegetable"=>15}, {"Dry Goods"=>5}] I'm in control of the code that outputs this hash so I can modify it if necessary. The results were mainly hashes because this could end up nested any number of levels deep and then it's easy to call flatten on the array but not flatten the keys/values of the hash too: def recipe_pl(parent_percentage=nil)

IEnumerable doesn't have a Count method

瘦欲@ 提交于 2019-11-26 20:27:20
I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() == 0); } } public IEnumerable<RuleViolation> GetRuleViolations(){ //code here } Why is it that when I do .Count() above it is underlined in red? I got the following error: Error 1 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Count' and no extension method 'Count' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?) c:\users\a\documents\visual studio 2010\Projects\NerdDinner

What are the benefits of making properties non-enumerable?

荒凉一梦 提交于 2019-11-26 19:27:24
问题 Enumerability is one of the three attributes of a property: writability, enumerability, and configurability. My questions are: What are the benefit of making properties non-enumerable in JavaScript? I know we are hiding the property by making them non-enumerable, but what are the benefit of property hiding? Can we access non-enumerable properties? If yes, then what is the benefit of making them non-enumerable? Are all predefined properties of Objects set as non-enumerable? Such as the case of

How do you find a min / max with Ruby?

旧时模样 提交于 2019-11-26 18:45:43
问题 I want to do something simple and straightforward, like min(5,10) , or Math.max(4,7) . Are there functions to this effect in Ruby? 回答1: You can do [5, 10].min or [4, 7].max They come from the Enumerable module, so anything that includes Enumerable will have those methods available. v2.4 introduces own Array#min and Array#max , which are way faster than Enumerable's methods because they skip calling #each . EDIT @nicholasklick mentions another option, Enumerable#minmax, but this time returning

Random array using LINQ and C#

荒凉一梦 提交于 2019-11-26 17:50:44
问题 I was reading an article on MSDN Magazine about using the Enumerable class in LINQ to generate a random array. The article uses VB.NET and I'm not immediately sure what the equivalent is in C#: Dim rnd As New System.Random() Dim numbers = Enumerable.Range(1, 100). _ OrderBy(Function() rnd.Next) 回答1: The Developer Fusion VB.Net to C# converter says that the equivalent C# code is: System.Random rnd = new System.Random(); IEnumerable<int> numbers = Enumerable.Range(1, 100).OrderBy(r => rnd.Next(

Why does Enumerable.All return true for an empty sequence? [duplicate]

北城余情 提交于 2019-11-26 16:12:58
This question already has an answer here: Why does IQueryable.All() return true on an empty collection? 11 answers var strs = new Collection<string>(); bool b = strs.All(str => str == "ABC"); The code creates an empty collection of string, then tries to determine if all the elements in the collection are "ABC". If you run it, b will be true. But the collection does not even have any elements in it, let alone any elements that equal to "ABC". Is this a bug, or is there a reasonable explanation? It's certainly not a bug. It's behaving exactly as documented : true if every element of the source

What is the effect of AsEnumerable() on a LINQ Entity?

寵の児 提交于 2019-11-26 15:25:34
Reading the questions here and here has given me some insight into the situation, and it seems like using the AsEnumerable is memory consuming. Is there a better way to do this LINQ and the way it is done now, is the data that comes out reliable? Removing the AsEnumerable results in a "Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator." var results = from p in pollcards.AsEnumerable() join s in spoils.AsEnumerable() on new { Ocr = p.OCR, fileName = p.PrintFilename } equals new { Ocr = s.seq, fileName = s.inputFileName } where p.Version

Is there an inverse &#39;member?&#39; method in ruby?

梦想的初衷 提交于 2019-11-26 11:30:39
问题 I often find myself checking if some value belongs to some set. As I understand, people normally use Enumerable#member? for this. end_index = [\'.\', \',\'].member?(word[-1]) ? -3 : -2 However, this feels a little less elegant than most of things in Ruby. I\'d rather write this code as end_index = word[-1].is_in?(\'.\', \',\') ? -3 : -2 but I fail to find such method. Does it even exist? If not, any ideas as to why? 回答1: Not in ruby but in ActiveSupport: characters = ["Konata", "Kagami",