[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

choose parameter to solve for

Jason

2/3/2009 6:11:00 AM

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
??
--
Posted via http://www.ruby-....

7 Answers

ThoML

2/3/2009 6:22:00 AM

0

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

Julian Leviston

2/3/2009 6:48:00 AM

0

What do you mean?

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

On 03/02/2009, at 5:10 PM, Jason Lillywhite
<jason.lillywhite@gmail.com> wrote:

> 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
> ??
> --
> Posted via http://www.ruby-....
>

Brian Candler

2/3/2009 11:29:00 AM

0

Jason Lillywhite wrote:
> 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.

--
Posted via http://www.ruby-....

ThoML

2/3/2009 11:51:00 AM

0

> This could be rewritten as
>
> =A0 =A0 =A0 =A0 @x ||=3D var

I totally misread the code. Sorry. Brian seems to be on the right
track.

Brian Candler

2/3/2009 1:05:00 PM

0

Jason Lillywhite wrote:
> 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?
--
Posted via http://www.ruby-....

Jason

2/3/2009 1:46:00 PM

0

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.
--
Posted via http://www.ruby-....

Brian Candler

2/3/2009 2:09:00 PM

0

Jason Lillywhite wrote:
> 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 :-)
--
Posted via http://www.ruby-....