FAQ Database Discussion Community


Rationale behind Haskells `succ` on numbers (floats)

haskell,floating-point,enumerable
I was a bit surprised to learn that Haskell defines its succ function on numbers as adding one: succ :: a -> a The successor of a value. For numeric types, succ adds 1. Although for integral values, this seems reasonable there are a few problems: what if you define...

Understanding `detect` method

ruby,enumerable
I have trouble understanding the detect method in Enumerable. I tried with the sample code: (1..6).detect { |i| i % 2 == 0 and i % 3 == 0 } #=> 6 But I'm still mystified. Any help would be much appreciated....

Why doesn't Ruby inject return an enumerator?

ruby,enumerable
I was expecting Enumerable#inject would be returning an enumerator like other methods and was passing it a block; but it was throwing error. Tried the following in pry: >> numbers = (1..12) => 1..12 >> numbers.each_with_index => #<Enumerator: ...> >> numbers.each_with_index.map => #<Enumerator: ...> >> numbers.inject(0) TypeError: 0 is not...

Why does Enumerable not have a length attribute in Ruby?

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

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

javascript,syntax,prototype,enumerable
$('example') console logs foo {word: "example", letter: function} ^ The log showed me letter is a prototype function of foo var foo,$; (function() { foo = $ = function(word) { return new foo(word); }; var foo = function(word) { //#A this.word=word; console.log(this.word); return this; }; foo.fn = foo.prototype = {...

Ruby Custom Sorting

ruby,sorting,enumerable
I have got following array in Ruby. arr = [ [13.0, ["mango", "banana", "jackfruit"]], [10.0, ["mango", "Milk", "Cofee"]], [4.0, ["mango"]] [5.0, ["jackfruit"]], [6.0, ["mango", "banana"]], [5.0, ["mango", "banana"]], [10.0, ["banana", "jackfruit"]], [5.0, ["banana"]], ] I want the result to be sorted by following rules: Sort by the Fruit arrays length...

Split an array into slices, with groupings

arrays,ruby,enumerable
I've got some Ruby code here, that works, but I'm certain I'm not doing it as efficiently as I can. I have an Array of Objects, along this line: [ { name: "foo1", location: "new york" }, { name: "foo2", location: "new york" }, { name: "foo3", location: "new york"...

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

ruby,class,module,enumerable
I have some data saved in deeply nested Hashes and Arrays and I have run into trouble with the text encoding of the data. I know for fact that the texts are encoded in "UTF-8", so I decided to go over each element and force the encoding. So, I created...

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

ruby,arrays,dictionary,enumerable
The objective is to move each letter to the next letter in the alphabet, within the map, it successfully changed the letter but once i'm out of there the value disappears, except the vowels. How come? def LetterChanges(str) abc = [*("a".."z")] result = str.split(//) result.map! do |x| if abc.include?(x) if...

Enumerable range in descending order

c#,wpf,linq,enumerable
I am binding a combobox using enumerable.range() and it works fine. Now I am trying to display the results in descending order, how can I do that? cboYearList.ItemsSource = Enumerable.Range( DateTime.Today.Year,1950).ToList().OrderByDescending(); ...