[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Passing math method to another method?

x1

1/17/2007 2:06:00 PM

I'm trying to figure out how to pass methods such as:
+, -, **, ^
to a method and evaluate.

For example,

def test(a, b, to_do)
return a.send(to_do(b))
end


puts test(1, 2, "+") #should return 3
puts test(3, 3, "^") #should return 0
puts test(3, 3, "**") #should return 27

any help would be appreciated.

11 Answers

James Gray

1/17/2007 2:17:00 PM

0

On Jan 17, 2007, at 8:10 AM, Neutek wrote:

> I'm trying to figure out how to pass methods such as:
> +, -, **, ^
> to a method and evaluate.
>
> For example,
>
> def test(a, b, to_do)
> return a.send(to_do(b))
> end
>
>
> puts test(1, 2, "+") #should return 3
> puts test(3, 3, "^") #should return 0
> puts test(3, 3, "**") #should return 27
>
> any help would be appreciated.

>> def test(a, b, meth)
>> a.send(meth, b)
>> end
=> nil
>> test(1, 2, :+)
=> 3
>> test(3, 3, :^)
=> 0
>> test(3, 3, :**)
=> 27

Hope that helps.

James Edward Gray II

Xavier Noria

1/17/2007 2:18:00 PM

0

On Jan 17, 2007, at 3:10 PM, Neutek wrote:

> I'm trying to figure out how to pass methods such as:
> +, -, **, ^
> to a method and evaluate.
>
> For example,
>
> def test(a, b, to_do)
> return a.send(to_do(b))
> end
>
>
> puts test(1, 2, "+") #should return 3
> puts test(3, 3, "^") #should return 0
> puts test(3, 3, "**") #should return 27

a.send(to_do, b)



Rob Biedenharn

1/17/2007 2:18:00 PM

0


On Jan 17, 2007, at 9:10 AM, Neutek wrote:

> I'm trying to figure out how to pass methods such as:
> +, -, **, ^
> to a method and evaluate.
>
> For example,
>
> def test(a, b, to_do)
> return a.send(to_do(b))
> end
>
>
> puts test(1, 2, "+") #should return 3
> puts test(3, 3, "^") #should return 0
> puts test(3, 3, "**") #should return 27
>
> any help would be appreciated.

You're very close:

>> def test(a, b, to_do)
>> a.send(to_do, b)
>> end
=> nil
>> test(1, 2, "+")
=> 3
>> test(3, 3, '^')
=> 0
>> test(3, 3, '**')
=> 27


Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com
Skype: rob.biedenharn



Robert Klemme

1/17/2007 2:23:00 PM

0

On 17.01.2007 15:06, Neutek wrote:
> I'm trying to figure out how to pass methods such as:
> +, -, **, ^
> to a method and evaluate.
>
> For example,
>
> def test(a, b, to_do)
> return a.send(to_do(b))
> end
>
>
> puts test(1, 2, "+") #should return 3
> puts test(3, 3, "^") #should return 0
> puts test(3, 3, "**") #should return 27
>
> any help would be appreciated.

Several ways to do it

irb(main):001:0> def test(a,b,op) a.send(op,b) end
=> nil
irb(main):002:0> test 1,2,:"+"
=> 3
irb(main):003:0> def test(a,b,op) a.send(op.to_sym,b) end
=> nil
irb(main):004:0> test 1,2,"+"
=> 3
irb(main):005:0> def test(a,b,op) op[a,b] end
=> nil
irb(main):006:0> test 1,2,lambda {|x,y| x+y}
=> 3

The last sample shows the more functional approach.

What are you trying to achieve?

Kind regards

robert

Kalman Noel

1/17/2007 4:48:00 PM

0

Robert Klemme:
> irb(main):003:0> def test(a,b,op) a.send(op.to_sym,b) end
^^^^^^
> => nil
> irb(main):004:0> test 1,2,"+"
> => 3

Any specific reason for to_sym's appearing there?

Kalman

x1

1/17/2007 4:49:00 PM

0

Wow. Thanks for the input guys.

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


I got this far ;-)

def sigma(floor, to_do, cap)

end
puts sigma(4, ^2, 20)

I will fiddle with your suggestions and continue with my sigma :-)

Thanks again for all your help


Robert Klemme wrote:
> On 17.01.2007 15:06, Neutek wrote:
> > I'm trying to figure out how to pass methods such as:
> > +, -, **, ^
> > to a method and evaluate.
> >
> > For example,
> >
> > def test(a, b, to_do)
> > return a.send(to_do(b))
> > end
> >
> >
> > puts test(1, 2, "+") #should return 3
> > puts test(3, 3, "^") #should return 0
> > puts test(3, 3, "**") #should return 27
> >
> > any help would be appreciated.
>
> Several ways to do it
>
> irb(main):001:0> def test(a,b,op) a.send(op,b) end
> => nil
> irb(main):002:0> test 1,2,:"+"
> => 3
> irb(main):003:0> def test(a,b,op) a.send(op.to_sym,b) end
> => nil
> irb(main):004:0> test 1,2,"+"
> => 3
> irb(main):005:0> def test(a,b,op) op[a,b] end
> => nil
> irb(main):006:0> test 1,2,lambda {|x,y| x+y}
> => 3
>
> The last sample shows the more functional approach.
>
> What are you trying to achieve?
>
> Kind regards
>
> robert

Robert Klemme

1/17/2007 5:11:00 PM

0


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

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.

Robert Klemme

1/17/2007 5:12:00 PM

0

On 17.01.2007 17:47, Kalman Noel wrote:
> Robert Klemme:
>> irb(main):003:0> def test(a,b,op) a.send(op.to_sym,b) end
> ^^^^^^
>> => nil
>> irb(main):004:0> test 1,2,"+"
>> => 3
>
> Any specific reason for to_sym's appearing there?

Yes, my ignorance. :-)

robert

x1

1/17/2007 5:14:00 PM

0


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.or...
>
> 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)?

x1

1/17/2007 5:21:00 PM

0

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.or...
> >
> > 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)²