Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Passing math method to another method?

Reply
Thread Tools

Passing math method to another method?

 
 
Neutek
Guest
Posts: n/a
 
      01-17-2007
Cool... I think this works


#does not work when trying to send an entire mathematical expresion as
a param
def sigma(floor, to_do, cap)
x = 0
floor.upto(cap) {|i|
#x += i.send(to_do)
x += to_do[i]
}
return x
end
puts sigma(4, lambda{|x| x ** 2}, 20)

Thanks a million!


Neutek wrote:
> Robert Klemme wrote:
> > ** SPOILER **
> >
> > Don't read on if you first want to experiment yourself.
> >
> >
> > On 17.01.2007 17:48, Neutek wrote:
> > > Robert, I was actually reading the summation wiki and they had a few
> > > code examples in C++/Java.. I thought I'd goof a bit and write
> > > something out in ruby. Of course, I hit the roadblock when trying to
> > > pass math operators(or methods rather) to a method...

> >
> > What is the "summation wiki"? Are you referring to this page?
> > http://en.wikipedia.org/wiki/Sum
> >
> > If yes, here are some more ways:
> >
> > # plain values
> > sum = (m..n).inject(0){|s,x| s + x}
> >
> > # with a function
> > f = lambda {|i| i * 2}
> > sum = (m..n).inject(0){|s,x| s + f[x]}
> >
> > # in a method
> > def sum(m, n, f)
> > (m..n).inject(0){|s,x| s + f[x]}
> > end
> >
> > s = sum( 1, 2, lambda {|x| x * 2} )
> >
> > etc.
> >
> > Advantage of using lambdas is that they are more flexible than method
> > names and can contain arbitrary calculations.
> >
> > Kind regards
> >
> > robert
> >
> >
> > PS: Please don't top post.

>
> What's top post? (sorry, I'm new to google groups --I put my reply at
> the bottom if this is what you meant )
>
> I'll read through your example now but figured what I worked on in the
> interim was worth posting..
>
>
> #works
> def test(a, to_do, b)
> return a.send(to_do, b)
> end
> puts test(2, :**, 3)
>
> #does not work when trying to send an entire mathematical expresion as
> a param
> def sigma(floor, to_do, cap)
> x = 0
> floor.upto(cap) {|i|
> x += i.send(to_do)
> }
> return x
> end
> puts sigma(4, :**2, 20)
>
>
> #does not work.. but another example of what I would expect
>
> def do_it(n, to_do)
> return n.send(to_do)
> end
> puts sigma(3, :+4/2) #should yield 5
>
>
> (Thank you)


 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-18-2007
On 17.01.2007 18:21, Neutek wrote:
> Cool... I think this works
>
>
> #does not work when trying to send an entire mathematical expresion as
> a param
> def sigma(floor, to_do, cap)
> x = 0
> floor.upto(cap) {|i|
> #x += i.send(to_do)
> x += to_do[i]
> }
> return x
> end
> puts sigma(4, lambda{|x| x ** 2}, 20)
>
> Thanks a million!


Basically if you think this further through, what you are trying to do
is already part of Ruby's standard lib. The method is #inject. I think
I used it in one of my postings. Your piece above becomes

puts (4..20).inject(0) {|s,x| s + x ** 2}

or, to more directly translate using #upto

require 'enumerator'
puts 4.to_enum(:upto, 20).inject(0) {|s,x| s + x ** 2}

Cheers

robert
 
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
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
Math.min and Math.max for byte / short Philipp Java 9 07-23-2008 12:37 AM
math.h trig functions questions (and some forgotten high school math) Mark Healey C Programming 7 05-22-2006 10:42 AM
Re: Is still math.h the C++ math library ? AciD_X C++ 4 04-01-2004 07:29 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM



Advertisments