Passing math method to another method?

N

Neutek

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

James Edward Gray II

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

Hope that helps.

James Edward Gray II
 
X

Xavier Noria

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

Rob Biedenharn

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:
=> 27


Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
Skype: rob.biedenharn
 
R

Robert Klemme

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
 
K

Kalman Noel

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
 
N

Neutek

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 said:
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
 
R

Robert Klemme

** SPOILER **

Don't read on if you first want to experiment yourself.


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

Neutek

Robert said:
** SPOILER **

Don't read on if you first want to experiment yourself.


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)²
 
N

Neutek

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
}
return x
end
puts sigma(4, lambda{|x| x ** 2}, 20)

Thanks a million!

Robert said:
** SPOILER **

Don't read on if you first want to experiment yourself.


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)²
 
R

Robert Klemme

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,248
Latest member
MagdalenaB

Latest Threads

Top