3 ms·
I, too, think like that, although Ruby's nomenclature frequently confuses me. Coming from Scheme/Haskell I'm quite at home with 'map', 'filter', and 'fold'. Ho
by losvedir 14y ago
I, too, think like that, although Ruby's nomenclature frequently confuses me. Coming from Scheme/Haskell I'm quite at home with 'map', 'filter', and 'fold'.
However, Ruby uses the methods you mention, "collect" and "inject", and these in turn have a few synonyms. And as best I can tell, Ruby's 'filter' variant, 'find' collides with ActiveRecord. What's the general community take on these functions? Which name is commonly used, and how do you do filter in Rails?
- cdcarter 14y agoI'd say that most Rubyists use #select, whereas #find is a more literate alias that few use outside of AR.
- yxhuvud 14y agoThey are not synonyms. Find will only find one result.
- philwelch 14y agoIn Ruby, I always use "map" instead of "collect", partially because it's four fewer letters and partially because its intent is a little more obvious.
- masklinn 14y ago> I, too, think like that, although Ruby's nomenclature frequently confuses me. Most of them come from Smalltalk's Iterable protocol[0]: that's the source for `select:aBlock`, `collect:aBlock`, `inject:thisValue into:binaryBlock` (Smalltalk uses `fold` when there's no starting value, and `inject` as "fold by injecting initial value"), `detect:aBlock` and `reject:aBlock`. Smalltalk also uses `do:aBlock` where Ruby uses `each`, likely due to `do` being a keyword in Ruby. Also, `find` is not `filter` (that's `select`, aliased to `find_all`). `find` is also `find` in Haskell[1] (and maps to Smalltalk's `detect`, also useable in Ruby). > 'find' collides with ActiveRecord Uh... no, ActiveRecord collides with Enumerable, I'm pretty damn sure Enumerable came first. > how do you do filter in Rails? What's wrong with #find? Or #where? Or #find_by_*? Same name, different namespace, different meaning. Kind-of the point. [0] http://www.gnu.org/software/smalltalk/manual-base/html_node/Iterable_002denumeration.html#Iterable_002denumeration http://www.gnu.org/software/smalltalk/manual-base/html_node/... [1] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-List.html#v:find http://hackage.haskell.org/packages/archive/base/latest/doc/...