Ara.T.Howard said:
> On 2 Apr 2004, Ville Vainio wrote:
>> It does make a difference, because sequences (or 'iterables') are a much
>> more fundamental concept than strings.
>
> i think alan turing would disagree.  strings are THE fundemental concept
> behind all computing and this has been agreed upon in computer science
> since
> 1936.

I am also having problems with the phrase "much more fundamental".  I
would agree that iterables/sequences are more *abstract* than String
(which is quite concrete).  And I would also agree that (in general) we
prefer to have dependencies run from concrete to abstract and not the
other way.

However, join is defined without any reference to the concrete class
String.  So the dependency is a very weak one.  In fact, if you define
join as it is in Python, you don't even need the "to_s" conversion so join
would have no reference to strings at all.  It need only assume that the
objects in the sequence are "joinable" in some fashion.

In fact, if you push this to its logical conclusion, you can even make the
Enumerable#join polymorphic on a string type (another common reason given
for " ".join).  First, define a pairwise join method ...

  class String
    def join(left, right)
      left.to_s + self + right.to_s
    end
  end

Now the join operation is polymorphic on the type of string.  Now define
Enumerable#join in terms of the pairwise join ...

  module Enumerable
    def join(delimit)
      inject { |res, item| delimit.join(res, item) }
    end
  end

There!  We know have the best of all worlds.  This is what we get:

o Enumerable#join is completely decoupled from string
o Join is polymorphic on the delimiter (which provides the pairwise join).
o And we get the nice, method chaining syntax!

To demonstrate the decoupling from string, consider the following.

  class Numeric
    def join(left, right)
      left * self + right
    end
  end

We use a Struct for our demo because the original Array#join still
overrides the new one in Enumerable ...

S = Struct.new(:a, :b, :c)
s = s.new(1,2,3)

s.join(100)  #=> 10203  # Note the non-string return!

Ok, that's bizzarre!  But it was fun getting there.  I'm thinking we don't
need an RCR for this new design though.  I'm happy with the current
situation.

Except ... is there a reason join is defined in Array and not Enumerable?

-- 
-- Jim Weirich     jim / weirichhouse.org    http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)