Menu
  • HOME
  • TAGS

Array.map { |x| change value } is removing it from the array, why?

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"...

Understanding `detect` method

ruby,enumerable

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...

c# Enumerable.Sum change return type

c#,types,enumerable

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). //...

Excluding one item from list (by Index), and take all others

c#,.net,linq,list,enumerable

Use Where:- var result = numbers.Where((v, i) => i != MasterIndex).ToList(); Working Fiddle....

my enumerable line for narcissistic numbers

ruby,enumerable

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...

Ruby Custom Sorting

ruby,sorting,enumerable

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...

Enumerable range in descending order

c#,wpf,linq,enumerable

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(); ...

Populate new_hash where the unique values from old_hash are the keys, and the keys from old_hash are values, grouped into arrays.

ruby,arrays,hash,enumerable

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 `for` does not iterate over enumerate object

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....

Why is `each` in ruby not defined in the enumerable module?

ruby,enumerable

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...

Counting multiple fields in a hash

ruby,enumerable

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...

Rationale behind Haskells `succ` on numbers (floats)

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...

Enumerable Select by Expression Tree

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: How do you set an Enumerator's state?

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(' ')...

Overwriting #each; can I pass it args through #map, #select, etc?

ruby,enumerable

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...

Why does Enumerable not have a length attribute in Ruby?

ruby,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...

Why doesn't Ruby inject return an enumerator?

ruby,enumerable

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) ...

Split an array into slices, with groupings

arrays,ruby,enumerable

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:...

Why a block invoked by a Module can't modify objects from implementing classes in Ruby?

ruby,class,module,enumerable

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...

How to add Object.prototype.x? set to enumerable:false

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...

Convert Array to Hash removing duplicate keys and adding values at the same time

ruby,arrays,hash,enumerable

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()...

check if a variable is in array in ruby

ruby,arrays,enumerable

list_of_ips.any? {|ip| ip.ip_address == '192.168.1.27'} ...