Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Duck typing alows true polymorfisim

Reply
Thread Tools

Duck typing alows true polymorfisim

 
 
atbusbook@aol.com
Guest
Posts: n/a
 
      08-25-2006
lets say you want a generic numerical algorithom like sum

Ruby

def sum lst
lst.inject(0){|total,current| total*current}
end

Java // i dont know if there is a numeric super class for numbers

class Sum{
public static int sum(int[] lst){
int total = 0;
for(int current : lst){
total+=current;
}
return total;
}
// repeat for all other number types
}

 
Reply With Quote
 
 
 
 
Simon Forman
Guest
Posts: n/a
 
      08-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> lets say you want a generic numerical algorithom like sum
>
> Ruby
>
> def sum lst
> lst.inject(0){|total,current| total*current}
> end
>
> Java // i dont know if there is a numeric super class for numbers
>
> class Sum{
> public static int sum(int[] lst){
> int total = 0;
> for(int current : lst){
> total+=current;
> }
> return total;
> }
> // repeat for all other number types
> }


What's your question? (Or, if no question, point?)

Totally off topic (and indeed "off-list"..) is that really how ruby
sums a list? How does that work? Doesn't the '*' mean multiply? and
what are the pipe symbols for? (Feel free to ignore these questions. I
should really go look it up myself if I'm so curious..)

Peace,
~Simon

 
Reply With Quote
 
 
 
 
John Bokma
Guest
Posts: n/a
 
      08-25-2006
(E-Mail Removed) wrote:

> lets say you want a generic numerical algorithom like sum


Hmmm, I thought you were going to announce that you were the first born
son of Xah Lee. Actually you did!

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
 
Reply With Quote
 
David Ells
Guest
Posts: n/a
 
      08-25-2006

(E-Mail Removed) wrote:
> lets say you want a generic numerical algorithom like sum
>
> Ruby
>
> def sum lst
> lst.inject(0){|total,current| total*current}
> end
>
> Java // i dont know if there is a numeric super class for numbers
>
> class Sum{
> public static int sum(int[] lst){
> int total = 0;
> for(int current : lst){
> total+=current;
> }
> return total;
> }
> // repeat for all other number types
> }



Any more to add? Your question is not exactly clear. Are you asking how
to do higher order procedures in Python? Here's sum as a higher-order
procedure... It sums the numbers from a to b (or any thing that can be
compared and added with "<" and "+").

def Sum(term, next, a, b):
if(a > b):
return 0
else:
return (term(a) + (Sum(term, next, next(a), b)

This function takes two functions (term and next) and two objects (in
our case numbers) representing the range to sum across. Now let's say
you want to add the numbers 1 through 10. The term for the summation
would just be the number, so the term function would simply be a
function called identity, and could be defined as

def identity(x):
return x

To get the next term, we are just adding one each time (i.e. 1, 2, 3,
4, ...). So our next function is

def increment(x):
return x += 1

Then we call Sum(identity, increment, 1, 10), and we get 1 + 2 + 3 + 4
+ and so on.

Now what if you wanted the sum of the CUBES of 1 through 10? Easy, we
replace the term function with a function cube(x) that returns x*x*x.

Sum(cube,increment,1,10)

Hence each term in our summation is the cube, but we are still
incrementing by one each time, so we get (1^3 + 2^3 + 3^3 + 4^3 + ...)

Similarly we can change the next function to skip certain number, meet
certain requirement, do some tranformation, whatever. The fact that
python accept and uses functions as parameters to other functions is
awesome in terms of power, but it can get a little mind boggling
sometimes.

 
Reply With Quote
 
atbusbook@aol.com
Guest
Posts: n/a
 
      08-25-2006
What was i thinkinng repace * with + i was'nt thinking i origanaly
thaught of sum of squares so i put a * insted of a +

 
Reply With Quote
 
Simon Forman
Guest
Posts: n/a
 
      08-25-2006
(E-Mail Removed) wrote:
> What was i thinkinng repace * with + i was'nt thinking i origanaly
> thaught of sum of squares so i put a * insted of a +


But again, what's your question?

 
Reply With Quote
 
David Vallner
Guest
Posts: n/a
 
      08-25-2006
(E-Mail Removed) wrote:
> Java // i dont know if there is a numeric super class for numbers


It is, confusingly enough, Number. And this is a silly example for Java,
since the language lacks an abstract numerical tower, which is a very
concrete deficiency of the language, and doesn't say anything about
polymorphism being limited with static typing.

You could probably do that algorithm as elegantly in OcaML (splitting
between integral and real number types, since those aren't unified in
the language), or better Haskell which treats all numbers uniformly.
Both are very, very strictly statically typed languages.

Your example doesn't prove anything about latent typing at all.

David Vallner

 
Reply With Quote
 
Daniel Martin
Guest
Posts: n/a
 
      08-26-2006
"Simon Forman" <(E-Mail Removed)> writes:

> (E-Mail Removed) wrote:
>> <Java vs. ruby half-argument>

>
> What's your question? (Or, if no question, point?)


This is atbusbook. He doesn't *have* a point - he does bizarre stuff
like this. Seriously - google his email address sometime, especially
on google groups.

I'm not going to attempt to understand him, and have concluded after
his "the perens in lisp dilects is there for a reson... macros" post
from August fifth that the best thing to do is ignore him until he
decides to start being coherent.


 
Reply With Quote
 
David Vallner
Guest
Posts: n/a
 
      08-26-2006
Daniel Martin wrote:
> This is atbusbook. He doesn't *have* a point - he does bizarre stuff
> like this. Seriously - google his email address sometime, especially
> on google groups.
>
> I'm not going to attempt to understand him, and have concluded after
> his "the perens in lisp dilects is there for a reson... macros" post
> from August fifth that the best thing to do is ignore him until he
> decides to start being coherent.


Wooer. Troll fight over Ruby? (Spot the book reference and win...
NOTHING at all!)

David Vallner

 
Reply With Quote
 
Simen Edvardsen
Guest
Posts: n/a
 
      08-30-2006
On 8/30/06, athornetdotdemondotnl"@ruby-lang.org Jeroen Wenting
<"jwenting> wrote:
>
> "Simon Forman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > (E-Mail Removed) wrote:
> >> lets say you want a generic numerical algorithom like sum
> >>

>
> > What's your question? (Or, if no question, point?)
> >

> Reads like the weekly "Ruby is better than Java because XXXXX" post.
>
>
>
>


The problem is that they don't have to come here to convince
*rubyists* that Ruby is better than language XXX. Perhaps some people
just are to stupid to think.

--
- Simen

 
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
duck duck go peter NZ Computing 1 01-09-2011 05:19 AM
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
explicit protocols and duck typing dg.google.groups@thesamovar.net Python 2 01-28-2008 12:26 AM
Duck typing alows true polymorfisim atbusbook@aol.com Python 15 09-01-2006 03:56 PM
Duck Typing srijit@yahoo.com Python 10 09-17-2003 03:48 AM



Advertisments