Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > I'll have the duck!

Reply
Thread Tools

I'll have the duck!

 
 
transfire@gmail.com
Guest
Posts: n/a
 
      07-23-2006
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.


 
Reply With Quote
 
 
 
 
John Carter
Guest
Posts: n/a
 
      07-24-2006
duck == mixin

Consider the Enumerable mixin.



John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : http://www.velocityreviews.com/forums/(E-Mail Removed)
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.

 
Reply With Quote
 
 
 
 
Jake McArthur
Guest
Posts: n/a
 
      07-24-2006
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, (E-Mail Removed) 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.
>



 
Reply With Quote
 
M. Edward (Ed) Borasky
Guest
Posts: n/a
 
      07-24-2006
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?

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      07-24-2006
On Monday 24 July 2006 11:40, John Carter wrote:
> duck =3D=3D 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=20
different - you have a kind of reversed dispatcher that adds classes to=20
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=20
subclasses of <animal>

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

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

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

define method color (fish :: <fish>)
=A0 "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)
=A0 if x =3D=3D=3D Animal
=A0 =A0 if x =3D=3D=3D Fish
=A0 =A0 =A0 "grey"
=A0 =A0 elsif x =3D=3D=3D Duck
=A0 =A0 =A0 "brown"
=A0 =A0 end
=A0 end
end =A0


don't want to hang around in dylan for too long, but this was a new way (at=
=20
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
=A0 def compact
=A0 =A0 delete_if{|e| e.nil? }
=A0 end

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

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

=46oo.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=20
butt-ugly to someone else

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

these assumptions are just in my honest opinion might as well be pretty=
=20
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 : (E-Mail Removed)
> 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.


 
Reply With Quote
 
Kroeger, Simon (ext)
Guest
Posts: n/a
 
      07-24-2006
=20

> From: (E-Mail Removed) [(E-Mail Removed)]=20
> Sent: Monday, July 24, 2006 1:10 AM
>=20
> [...]
> Now anything that responded to #to_a could use #transform.=20
> 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?
>=20
> T.


Like this *evil grin* ? (Ok, there are still classes...)
------------------------------------------------------------------------
----
class Object
def method_missing sym, *args
@@ducks.select{|d| d.first =3D=3D sym}.each do |duck|
return send(duck[1], *args) if duck.last.all?{|i|
respond_to? i}
end
super
end
=20
def self.duck sym, real , *interface
(@@ducks ||=3D []) << [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) #=3D> "42"
p('A'.next_to_s) #=3D> "B"
p(5.5.next_to_s) #=3D> "6"
------------------------------------------------------------------------
----

cheers

Simon

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      07-24-2006
Robert Dober wrote:
> On 7/24/06, (E-Mail Removed) <(E-Mail Removed)> 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/monitorfu...dex.html#6.0.0

> 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

 
Reply With Quote
 
benjohn@fysh.org
Guest
Posts: n/a
 
      07-24-2006
> 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




 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      07-24-2006
Robert Dober wrote:
> On 7/24/06, Alex Young <(E-Mail Removed)> wrote:
>
>>
>> Robert Dober wrote:
>> > On 7/24/06, (E-Mail Removed) <(E-Mail Removed)> 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/monitorfu...dex.html#6.0.0

>
>
>
> 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

 
Reply With Quote
 
Alex Young
Guest
Posts: n/a
 
      07-24-2006
Robert Dober wrote:
> On 7/24/06, Alex Young <(E-Mail Removed)> wrote:

<snip>
> The beauty of duck-typing is that you don't have to, though.
>
>
> Yes I agree 100% but the not-so-beauty is that I cannot (veryeasily)
>
> 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

>
> yes I could, why not, nice idea

I think it does everything you're after.

>
> 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.

>
> Is this really a feature, all the times, I do not think so.

Absolutely. It's what makes the entire Enumerable module so useful.
Every single Enumerable method works that way - the object only has to
support the #each method for all of Enumerable's methods to be
applicable. The #to_s method is similar - anything that supports #to_s
has a whole raft of functionality available to it because methods know
that they've got a way of treating it as a string.

--
Alex

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do I have to have the WEP # set for file sharing? =?Utf-8?B?SGVscCBtZSBJIGNhbicndCBzaGFyZSE=?= Wireless Networking 3 11-01-2005 05:37 AM
have you got any of these i can have spike240 Case Modding 4 09-14-2005 03:48 AM
do i have to have.... =?Utf-8?B?amFrZQ==?= Wireless Networking 1 03-11-2005 06:05 PM
do I have to have a windows OS to start a New pc when the mwssage no ntldr foun darinsray Microsoft Certification 3 04-28-2004 06:50 AM
do I have to have a windows OS to start a New pc when the mwssage no ntldr foun darinsray Microsoft Certification 0 04-24-2004 06:15 AM



Advertisments