[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I'll have the duck!

Trans

7/23/2006 11:10:00 PM

I promised myself I'd shut-up for awhile, maybe I still should, but I
just couldn't help myself with this one...

Today I wrote

data = data.transform

but I meant to write

data = transform(data)

While I knew data would be hash, I certainly didn't want to write a new
Hash method just for this --you reserve extensions for really general
reusable stuff, right? Besides I wanted to remain open to duck typing
and hence any hash-like object.

That's when it hit me. We haven't yet taken the full duck type plunge.
We've just strapped a quasi-duck onto an old oop type dog. We're still
defining our methods base on types, not duck types. But what would
defining on duck type mean intead? As it turns out it measn defining on
method dependencies.

Take #transform. Forget what "class" it belongs to. What does it do?
For the sake of this dialog lets say it does this:

def transform
data.to_a
end

Simple. Now, if I want to call #transform on data, all it needs to do
is repond_to #to_a. It need not matter at all what class it is. So
imagine if you will, that instead of "classes" and "methods", we could
define "ducks" and "quacks".

duck to_a
quack transform
self.to_a
end
end

Now anything that responded to #to_a could use #transform. I'm not sure
how far this can be taken. Can classes be undone altogegther? But in
anycase, it seems very cool, and I wonder what kind of overall effect
it could have on coding?

T.


94 Answers

John Carter

7/24/2006 2:40:00 AM

0

Ronald E Jeffries

7/24/2006 2:44:00 AM

0

On Mon, 24 Jul 2006 08:09:58 +0900, transfire@gmail.com wrote:

>Now anything that responded to #to_a could use #transform. I'm not sure
>how far this can be taken. Can classes be undone altogegther? But in
>anycase, it seems very cool, and I wonder what kind of overall effect
>it could have on coding?

there's a language called "self" that has no classes, only prototypes. if you
can find some material on that, i think you'll find it interesting.

regards,

--
Ron Jeffries
www.XProgramming.com
I'm giving the best advice I have. You get to decide if it's true for you.

Jake McArthur

7/24/2006 3:00:00 AM

0

I much prefer its (less mature) derivative, Io. Everything is a
message to something else, and everything is a prototype. Very
simple, very powerful (even, dare I say, more powerful than Ruby,
albeit a bit less pretty).

- Jake McArthur

On Jul 23, 2006, at 9:45 PM, Ron Jeffries wrote:

> On Mon, 24 Jul 2006 08:09:58 +0900, transfire@gmail.com wrote:
>
>> Now anything that responded to #to_a could use #transform. I'm not
>> sure
>> how far this can be taken. Can classes be undone altogegther? But in
>> anycase, it seems very cool, and I wonder what kind of overall effect
>> it could have on coding?
>
> there's a language called "self" that has no classes, only
> prototypes. if you
> can find some material on that, i think you'll find it interesting.
>
> regards,
>
> --
> Ron Jeffries
> www.XProgramming.com
> I'm giving the best advice I have. You get to decide if it's true
> for you.
>


M. Edward (Ed) Borasky

7/24/2006 3:10:00 AM

0

Jake McArthur wrote:
> I much prefer its (less mature) derivative, Io. Everything is a
> message to something else, and everything is a prototype. Very simple,
> very powerful (even, dare I say, more powerful than Ruby, albeit a bit
> less pretty).
>
> - Jake McArthur
Actual, real programming languages with compilers or interpreters, a
code base, applications and vibrant communities are for wimps! Give me
an academic abstract language like the Pi-Calculus any day!

:)

Now that I think of it, Lisp was once an academic abstract language ...
some bozo had to spoil it by writing an interpreter for the IBM 704.

Speaking of languages, isn't FORTRAN 50 years old this year?

Michael Fellinger

7/24/2006 3:13:00 AM

0

On Monday 24 July 2006 11:40, John Carter wrote:
> duck == mixin
>
> Consider the Enumerable mixin.
>

Well... i don't think mixins are exactly that :)
what trans proposes is more like excessive duck-typing

imagine a duck
you can make it quack
and you know - what the duck quacks can be processes in many ways...

Duck.quack.record.to_tape.copy.to_cd.send_to producer

might be a bit excessive, but in essential that's it...
i've learned that kind of 'method' in Dylan, where you define that stuff a bit
different - you have a kind of reversed dispatcher that adds classes to
methods and decides which method to use given what object you operate on.

little syntax help - the stuff in <> are classes, <duck> and <fish> are
subclasses of <animal>

########################################

define method to-s (animal :: <animal>)
  color(animal)
end

define method color (duck :: <duck>)
  "brown"
end

define method color (fish :: <fish>)
  "grey"
end

########################################

the methods are not added to the object - they look like that afterwards:
(using ruby for that, so it looks a bit nicer ;)

def color (x)
  if x === Animal
    if x === Fish
      "grey"
    elsif x === Duck
      "brown"
    end
  end
end  


don't want to hang around in dylan for too long, but this was a new way (at
least for me) to express methods...
now, this could be driven further

you could attach methods to objects that respond to specific methods

########################################

methods to_a
  def compact
    delete_if{|e| e.nil? }
  end

  def sum
    inject{|s,v| s+v}
  end
end

class Foo
  def to_a
    [1,2,3,nil]
  end
end

Foo.new.compact.sum
# 6

########################################

something like that...
i find that is true ducktyping :)
however... the beauty is in the eye of the beholder, so it might look
butt-ugly to someone else :|

i just kinda like the idea itself - the implementation in ruby would be
enormous work (as i imagine it at least) - and would slow down ruby
further...

these assumptions are just in my honest opinion :) might as well be pretty
neat to integrate and work with... but i have my doubts...

>
>
> John Carter Phone : (64)(3) 358 6639
> Tait Electronics Fax : (64)(3) 359 4632
> PO Box 1645 Christchurch Email : john.carter@tait.co.nz
> New Zealand
>
> Carter's Clarification of Murphy's Law.
>
> "Things only ever go right so that they may go more spectacularly wrong
> later."
>
> From this principle, all of life and physics may be deduced.

Jeff Schwab

7/24/2006 3:33:00 AM

0

transfire@gmail.com wrote:
> I promised myself I'd shut-up for awhile, maybe I still should, but I
> just couldn't help myself with this one...
>
> Today I wrote
>
> data = data.transform
>
> but I meant to write
>
> data = transform(data)
>
> While I knew data would be hash, I certainly didn't want to write a new
> Hash method just for this --you reserve extensions for really general
> reusable stuff, right? Besides I wanted to remain open to duck typing
> and hence any hash-like object.
>
> That's when it hit me. We haven't yet taken the full duck type plunge.
> We've just strapped a quasi-duck onto an old oop type dog. We're still
> defining our methods base on types, not duck types. But what would
> defining on duck type mean intead? As it turns out it measn defining on
> method dependencies.
>
> Take #transform. Forget what "class" it belongs to. What does it do?
> For the sake of this dialog lets say it does this:
>
> def transform
> data.to_a
> end
>
> Simple. Now, if I want to call #transform on data, all it needs to do
> is repond_to #to_a. It need not matter at all what class it is. So
> imagine if you will, that instead of "classes" and "methods", we could
> define "ducks" and "quacks".
>
> duck to_a
> quack transform
> self.to_a
> end
> end

class Object
def transform
self.to_a
end
end

p({ 1 => 2, 3 => 4 }.transform)

Kroeger, Simon (ext)

7/24/2006 9:45:00 AM

0



> From: transfire@gmail.com [mailto:transfire@gmail.com]
> Sent: Monday, July 24, 2006 1:10 AM
>
> [...]
> Now anything that responded to #to_a could use #transform.
> I'm not sure
> how far this can be taken. Can classes be undone altogegther? But in
> anycase, it seems very cool, and I wonder what kind of overall effect
> it could have on coding?
>
> T.

Like this *evil grin* ? (Ok, there are still classes...)
------------------------------------------------------------------------
----
class Object
def method_missing sym, *args
@@ducks.select{|d| d.first == sym}.each do |duck|
return send(duck[1], *args) if duck.last.all?{|i|
respond_to? i}
end
super
end

def self.duck sym, real , *interface
(@@ducks ||= []) << [sym, real, interface]
end
end

def i_next_to_s
succ.to_s
end

def f_next_to_s
ceil.to_s
end

Object.duck :next_to_s, :i_next_to_s, :succ, :to_s
Object.duck :next_to_s, :f_next_to_s, :ceil, :to_s

p(41.next_to_s) #=> "42"
p('A'.next_to_s) #=> "B"
p(5.5.next_to_s) #=> "6"
------------------------------------------------------------------------
----

cheers

Simon

Alex Young

7/24/2006 9:48:00 AM

0

Robert Dober wrote:
> On 7/24/06, transfire@gmail.com <transfire@gmail.com> wrote:
<snip>
>> and hence any hash-like object.
>
> Yeah great, you see that is what troubles me, it is completely cool to talk
> about hash-like object, but what is a hash-like object? Which messages
> must
> a hash-like object respond to? All of Hash, I suppose, thus a subclass, or
> only some, then we can talk about protocols again, but the failure to be
> able to define the protocol just worries me.
That's the whole point. Duck-typing means that your class only needs to
define the methods (of Hash, in this case) that the called method
(transform here) needs, *not* the whole Hash class, *without* having to
specify a protocol. If you want to specify a protocol, there's one
example of how to do it here:

http://www.erikveen.dds.nl/monitorfunctions/index....

> Again very important: The failure to be able, not to have to, I am 100% for
> the enabeling approach, but who enables constraints and protocol checking?
See above. Second time I've used that link in this thread :-)

--
Alex

benjohn

7/24/2006 10:01:00 AM

0

> Now anything that responded to #to_a could use #transform. I'm not
sure
> how far this can be taken. Can classes be undone altogegther? But in
> anycase, it seems very cool, and I wonder what kind of overall effect
> it could have on coding?

:) I was thinking things like this a while ago, and a clever friend
pointed me to predicate classes (see google). The idea with them is that
you define a predicate such as "supports the method 'each'". You could
call this predicate Enumerable. The predicate is a declaration of your
interface requirements. You can then add methods to this Enumerable
predicate class, such as min, max, inject, etc. From then, anything that
is Enumerable will also support the things you put in to Enumerable.
Pretty cool.

I liked the idea, anyway. I like it because I think it would make it
easier to take things that you've already got, things that someone has
given to you perhaps, and say new things about them based on the fact
that they've pretty much got the interface you're looking for (if they
don't quite have the right interface, you can use predicate classes to
give them the additional "glue" that they need).

I also like it because I don't think that there's anything especially
fundamental about classes. With predicate classes, all you're really
saying is that "if I've got something that can do x and y, then I know
that I can equally validly think of it as something that can do z".

[I should point out here that I don't imagine Predicate Classes are "the
answer"; I'm just pointing to them as being interesting.]

Class hierarchies almost always seem arbitrary and task oriented to me.
That works well when you've a particular task in mind, but I think it
falls over when the task changes, or when you're more interested in the
information in some objects, rather than what they are currently being
used for.

I also think they're an impediment to being agile because you're
building up a hierarchy that may not exist. Sure, you can refactor, and
perhaps you've got automated tools that do that, but you shouldn't need
to. You, and any part of your program or someone else's, ought to be
able to look at some existing objects in any way they choose. You should
be able to say anything you like about your particular way of looking at
those things.

While I like Ruby a great deal (and I really mean that: it's made
programming fun again, for me), I don't think contemporary OO is all
that. Something that's wonderful about Ruby though, is that I think it's
got every chance of being the platform in which people find that out,
and find a better way.

On the other hand, I could be wrong, in which case Ruby's already there :)

Cheers,
Benjohn




Alex Young

7/24/2006 10:57:00 AM

0

Robert Dober wrote:
> On 7/24/06, Alex Young <alex@blackkettle.org> wrote:
>
>>
>> Robert Dober wrote:
>> > On 7/24/06, transfire@gmail.com <transfire@gmail.com> wrote:
>> <snip>
>> >> and hence any hash-like object.
>> >
>> > Yeah great, you see that is what troubles me, it is completely cool to
>> talk
>> > about hash-like object, but what is a hash-like object? Which messages
>> > must
>> > a hash-like object respond to? All of Hash, I suppose, thus a subclass,
>> or
>> > only some, then we can talk about protocols again, but the failure
>> to be
>> > able to define the protocol just worries me.
>> That's the whole point. Duck-typing means that your class only needs to
>> define the methods (of Hash, in this case) that the called method
>> (transform here) needs, *not* the whole Hash class, *without* having to
>> specify a protocol. If you want to specify a protocol, there's one
>> example of how to do it here:
>>
>> http://www.erikveen.dds.nl/monitorfunctions/index....
>
>
>
> Well I have read that, kind of, it is a little bit heavy, too heavy I am
> afraid.
> I still do not get the point, are you saying I am right (let dogs life) or
> are you saying I am wrong (let only ducks life).
It's my opinion that opposition to duck-typing is a bad idea (especially
when dealing with Ruby, given how pervasive it is), but that doesn't
make it wrong. It is perfectly possible for both dogs and ducks to
co-exist.

> Mixins are another nice way to think about it
>
> class Dog
> include Duck
> ... # no this is *not* the Perl6 Thingy Jabbawalky operator ;)
> end
>
> d = Puppet
> d.implements? Duck ==> true
If you're always supplying Duck's functionality as a mixin, you can do:

Duck === d
=> true

The beauty of duck-typing is that you don't have to, though.

If you wanted an Object#implements? method which doesn't rely on
implementation via mixin, you could do it like this:

class Object
def implements?(module)
my_methods = Set.new(self.public_methods)
module_methods = Set.new(module.instance_methods)
return my_methods.superset?(module_methods)
end
end

Again, that misses the point somewhat, though - duck-typing lets you not
have to think about the concept of a defined interface (in the Module
sense, at least). If your method only calls #foobar on a passed object,
then that object only need respond to the #foobar method.

> but maybe this is here already, gotta check.
> Thx for your considerations
> I repeat nevertheless
> Ducks are great, unless they kill Dogs ;)
You can always check the class of an object, and there are ways to make
the syntax less cumbersome than it might otherwise be.

--
Alex