Any solutions to Ruby's inconsistencies?

Eleanor McHugh wrote:

Collect makes much more sense to me than map as I’m collecting a bunch
of results and storing them in an array.

You’re describing #select

2009/3/5 Todd B. [email protected]:

Agreed. Â But you are collecting a collection, which seems inane and
illogical to me. Â You are, however, absolutely right about the
fascinating flexibility of Ruby (though sometimes that thought is
coupled with deplorable distain of an old codger for excessive use :slight_smile:
Actually, I’m not an old codger, but I do come from old-school DB
design, so I guess that counts. Â I also come from a math background,
so “map” and “transform” were common terms, never the verb “collect”;
not even in engineering or physics. Â You don’t “collect” a group of
things. Â They are already collected. Â That would be sort of like using
the verb “gather”.

You are iterating the array visiting the elements one by one,
transforming each by the block, and stuffing the results into another
array. If that is not collecting the results of the transformation I
don’t know what would be collecting things then.

Thanks

Michal

C. Dagnon wrote:

  1. And what’s up with Ruby incorrectly naming their Map ‘Hash’?
    [snip]
    A ‘Hash’ would instead be a type of set.

What do you mean in that last sentence?

On 6 Mar 2009, at 12:28, Albert S. wrote:

Eleanor McHugh wrote:

Collect makes much more sense to me than map as I’m collecting a
bunch
of results and storing them in an array.

You’re describing #select

Actually the description is sufficiently generic to be valid for both
select and collect. With select obviously the results will be a subset
of the enumerator contents whereas with collect it will be a
transformed copy of the enumerator contents, but in both cases I’ll
have a new array containing a set of results harvested from the
original enumerator.

Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net

raise ArgumentError unless @reality.responds_to? :reason

On Thu, Mar 5, 2009 at 4:41 PM, Yossef M. [email protected]
wrote:

‘detect’ is a synonym for ‘find’ (or maybe it’s the other way around).
As so often genius goes undetected and unrewarded (but also
unpunished, while, when detected the later is not always the case).

But the idea of synonym not being a symmetric relation really made my
day.

Thx. Yossef

N.B. I spelled your name correctly.
This time, do not push it too much though :wink:

R.

On Fri, Mar 6, 2009 at 5:10 AM, Michal S. [email protected]
wrote:

You are iterating the array visiting the elements one by one,
transforming each by the block, and stuffing the results into another
array. If that is not collecting the results of the transformation I
don’t know what would be collecting things then.

I knew that someone would pipe in that we are collecting objects and
not transforming them, but you still have a left-hand assignment
dependency unless you use “!”; either way, you are collecting all of
them (well, that is if you don’t do some block-internal trickery). To
me, “collect”, “grab”, “gather”, “prune”, are all lexically equivalent
to “select”. In the current case, it’s like saying I want to collect
a flock of sheep, when that action – though makes sense when
physically grabbing them – has nothing to do with the use in the
programming language.

Anyways, like Eleanor mentioned, Ruby gives you the power to say, for
example… flock_1.sheer… if you are going to sheer flock_1, and
renders the actual map vs. collect argument a little bit moot. I
suppose it really depends on your use of the language and what team
language standards you have set down.

I like #map because I see in my mind a->b, even though the a into b
doesn’t have to be one to one. Like I said earlier; I think this is
an artifact of CS thinking. #map is easier to type too.

To each his or her own :slight_smile:

Todd

Albert S. wrote:

C. Dagnon wrote:

  1. And what’s up with Ruby incorrectly naming their Map ‘Hash’?
    [snip]
    A ‘Hash’ would instead be a type of set.

What do you mean in that last sentence?

A set simply contains values while mathematically a hash is a process
use to relate a value to a (hopefully advantageous) calculated value.
As used here, referring to a type of collection, a hash is the same as a
set: storing single, unrelated values. The hash’s benefit is being able
to quickly check/retrieve individual items. For each of them there is
only one user-defined value involved per item in each collection.
(Typically users don’t know or care what the hash algorithm or hash
values are thanks to encapsulation or other abstractions.)

Maps instead use one user-defined value to relate to a second
user-defined value by a user-defined relationship, the mapping. All 3
parts are knowledge defined external to the Map. The keys (and values)
can be stored as a hash set or ordered list or whatever makes sense.
Since the key-value relationship is user-defined you don’t see generic
Map collections internally using an algorithm to magically go from key
to value: They have to concretely store the pairings so they know what
they are.

Interesting, though, as after you got me thinking about it I started to
wonder myself. Initially Hash and Map seem so distinctly alien to each
other that I can only hope the above proves clear enough.

Thank you for the question, and please let me know if I answered the
wrong one!

Michal S. wrote:

2009/3/5 Todd B. [email protected]:

You are iterating the array visiting the elements one by one,
transforming each by the block, and stuffing the results into another
array. If that is not collecting the results of the transformation I
don’t know what would be collecting things then.

Again I would submit that this is the Lisp way of thinking about it, as
I showed in the example I gave, which is I guess where the ‘collect’
term comes from. In Lisp you loop over something (or several things)
and optionally collect results as a side-effect of the loop.

But often you apply a function which maps one set to another, that is,
Enumerable#map. The term ‘map’ is common in mathematics and in many
programming languages. Even Lisp uses ‘map’ when that the explicit
operation (and not a loop side-effect).

It is also helpful to recognize that if you were born in India you are
more likely to watch Cricket instead of Baseball. In other words,
cultural preferences are not absolute. If from the beginning ruby had
‘map’ but not the ‘collect’ synonym, there is no reason to believe that
you would find this situation unusual.