ruby,arrays,dictionary,enumerable
The block passed to map! needs to return a value in all cases for this to work. http://www.ruby-doc.org/core-2.2.0/Array.html#method-i-map-21 def LetterChanges(str) abc = [*("a".."z")] result = str.split(//) result.map! do |x| if abc.include?(x) if x == "z" x = "A" else x = abc[abc.index(x)+1] if x == "a" || x == "e"...
According to the documentation this method returns the first element in the enumerable object that the block returns true. Therefore, the first number in that range that is both divisible by 2 and 3 is 6 and thus it is returned. If this were not the case and no number...
Sum always uses the type of the enumerable (or the result type of the mapping function) and has overloads for the different numeric types. In this case, simply alter the Select as so to get the values to be added as longs1 (and the result to be a long). //...
Use Where:- var result = numbers.Where((v, i) => i != MasterIndex).ToList(); Working Fiddle....
You are missing the initial value for inject. This will work: value.to_s.chars.map {|e| e.to_i } .inject(0){|sum, e| sum += e ** value.to_s.length } From the docs: If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of...
There's no need for an overly clever comparator here, you're sorting by numbers so simple negation can reverse the sorting and sort_by can take care of the rest. For example: arr.sort_by { |e| [-e.last.length, e.first] } or if you prefer numeric indexes: arr.sort_by { |e| [-e[1].length, e[0]] } will give...
You can Reverse the list after creating it with Enumerable.Range: cboYearList.ItemsSource = Enumerable.Range(DateTime.Today.Year, 1950).Reverse().ToList(); Or if you want to keep your OrderByDescending, you need to pass a key selector (the i => i at the end): cboYearList.ItemsSource = Enumerable.Range( DateTime.Today.Year,1950).OrderByDescending(i => i).ToList(); ...
You can do as below :- animals = { 'leopard' => 1, 'gorilla' => 3, 'hippo' => 4, 'zebra' => 1, 'lion' => 2, 'eagle' => 3, 'ostrich' => 2, 'alligator' => 6 } animals_by_count = {} animals.each do |animal, count| if animals_by_count.has_key?(count) animals_by_count[count].push(animal) else animals_by_count[count] = [animal] end end...
python,loops,iteration,python-3.4,enumerable
Any iterator is "one-shot" in sense that, when it is fully executed, it becomes empty and can't be used anymore. When you called logging.debug( list(e) ), you have used it in the list() function and so exhausted it. So, the following attempt to use it in for cycle gives nothing....
From the documentation for Enumerable: The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. So the Enumerable module requires that classes which include it implement each on...
There are many ways to do this. (You may have noticed that I've done a lot of editing of my answer, explaining in some detail how a method works, only to realize there's a better way to do it, so out comes the machete.) Here are two solutions. The first...
haskell,floating-point,enumerable
The origin of the succ function itself actually has nothing to do with the Haskell data types or enumerations, in fact the succ function came first. The succ function is actually the successor function in the axiom of infinity which allows us to create numbers in the first place. It...
c#,lambda,expression,expression-trees,enumerable
Calm down folks, after some research I found what was missing in my code... On the fist case: Expression.Call( typeof(Enumerable), "Select", new Type[] { typeof(SomeClass), typeof(string) }, Expression.Constant(someList), // <---------------- HERE IT IS Expression.Lambda( someProperty, someParam ) ); To the second case, I created the "new" expression through the code...
ruby,permutation,enumeration,enumerable,enumerator
Results For your example, x = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a + ['+','/'] start = "ABCDEFGHIJK/".split("") the following is obtained with the enumerator head_start_permutation I've constructed below: y = x.head_start_permutation(start) #=> #<Enumerator: #<Enumerator::Generator:0x000001011e62f0>:each> y.peek.join(' ') #=> "A B C D E F G H I J K /" y.next.join(' ')...
I am so silly. The method #enum_for gives me all the power here. I can implement Charlie's syntax of tree.breadth_first.find(13) by adding the conventional line return self.enum_for(__method__) unless block_given? in each of my traversal methods. The tree.breadth_first will return an Enumerator which enumerates according to the breadth-first algorithm; any Enumerable...
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 Hash, length simply retrieves the pre-computed size of the data structure. It should...
Conceptually, Enumerator is a type of collection. Enumerable#inject accumulates a value across the members of a collection, it makes little sense for it to return a Enumerator. You can get the job done by changing numbers.inject(0) &block to: numbers.inject(0, &block) ...
Yes, this bookkeeping with i is usually a sign there should be something better. I came up with: ar =[ { name: "foo1", location: "new york" }, { name: "foo2", location: "new york" }, { name: "foo3", location: "new york" }, { name: "bar1", location: "new york" }, { name:...
As far as I can tell, you should get the exact same error with both your Enumerable version and your Array/Hash monkey patch. I do. Are you sure you're using the same deephash in both cases? Normally when you loop each on a hash, you'd pass in both key and...
javascript,syntax,prototype,enumerable
The title of your question doesn't seem to be related to the problem, but here we go: foo = $ = function(word) { return new foo(word); }; var foo = function(word) { ... }; is equivalent to var foo; foo = $ = function(word) { return new foo(word); }; foo...
This works: result = Hash.new(0) f = [["Wed, 12-31", 120.0],["Thu, 01-01", 120.0], ["Thu, 01-01", 120.0]] f.each { |subarray| result[subarray[0]] += subarray[1] } puts result If you would like to be fancy you could use .inject()...
list_of_ips.any? {|ip| ip.ip_address == '192.168.1.27'} ...