choose parameter to solve for

  • Thread starter Jason Lillywhite
  • Start date
J

Jason Lillywhite

I'm looking for a clean way to write a function that allows me to choose
which parameter to solve for assuming all others are known. Is it good
design to do something like this: ?

class Foo
def initialize(x, y, z)
@x, @y, @z = x, y, z
end

def function(var)
if @x.nil?
@x = var
elsif @y.nil?
@y = var
else
@z = var
end
@x + @y + @z
end

end

z = Foo.new(1, 2, nil)
z.function(3)
=> 6
??
 
T

Tom Link

=A0 =A0 if @x.nil?
=A0 =A0 =A0 @x =3D var

This could be rewritten as

@x ||=3D var
=A0 =A0 elsif @y.nil?

What if @x and @y are nil? You might want to use

@y ||=3D var
@z ||=3D var

instead -- depending on what you want of course.
 
B

Brian Candler

Jason said:
I'm looking for a clean way to write a function that allows me to choose
which parameter to solve for assuming all others are known. Is it good
design to do something like this: ?

class Foo
def initialize(x, y, z)
@x, @y, @z = x, y, z
end

def function(var)
if @x.nil?
@x = var
elsif @y.nil?
@y = var
else
@z = var
end
@x + @y + @z
end

end

z = Foo.new(1, 2, nil)
z.function(3)
=> 6

Hmm, is this some sort of currying?

adder = lambda { |x,y,z| x+y+z }
z = lambda { |z| adder[1,2,z] }
z[3]

You could perhaps factor this out into some sort of fix_args method.

Ruby 1.9 has Proc#curry, but I think this only fixes from the left.
 
B

Brian Candler

Jason said:
class Foo
def initialize(x, y, z)
@x, @y, @z = x, y, z
end

def function(var)
if @x.nil?
@x = var
elsif @y.nil?
@y = var
else
@z = var
end
@x + @y + @z
end

end

z = Foo.new(1, 2, nil)
z.function(3)
=> 6

Also, if this is really the pattern you want (an object holding default
values for each of the args), then you probably don't want to modify the
instance variables between invocations. So here is a more general
version:

class Foo
def initialize(*args)
@args = args
end
def function(*more)
# for each nil argument, pick one from 'more'
calculate(*@args.collect { |x| x || more.shift })
end
def calculate(x,y,z)
x + y + z
end
end
z = Foo.new(1,2,nil)
z.function(3)
q = Foo.new(nil,nil,6)
q.function(4,5)

But I still think some flavour of currying is what you want. Is it
important that arbitrary parameters are fixed, rather than just the
left-most ones?
 
J

Jason Lillywhite

adder = lambda { |x,y,z| x+y+z }
z = lambda { |z| adder[1,2,z] }
z[3]
This is what I want: write my equation one time then use that equation
in many different ways - solve for any one variable of my choice without
re-writing the equation. Thank you so much!
But I still think some flavour of currying is what you want. Is it
important that arbitrary parameters are fixed, rather than just the
left-most ones?

It is important that arbitrary parameters are fixed as long as only one
parameter is unknown.

Thank you.
 
B

Brian Candler

Jason said:
adder = lambda { |x,y,z| x+y+z }
z = lambda { |z| adder[1,2,z] }
z[3]
This is what I want: write my equation one time then use that equation
in many different ways - solve for any one variable of my choice without
re-writing the equation. Thank you so much!
But I still think some flavour of currying is what you want. Is it
important that arbitrary parameters are fixed, rather than just the
left-most ones?

It is important that arbitrary parameters are fixed as long as only one
parameter is unknown.

If the logic is to be hard-coded then maybe

f2 = lambda { |y| adder[1,y,3] }

is good enough for you. Otherwise, writing a currying function which
fixes arbitrary parameters is left as an exercise :)
 

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,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top