# Choose parameter to solve for

**URL:** <https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290>\
**Category:** Ruby\
**Created:** [February 3, 2009, 7:10am UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290 "2009-02-03T07:10:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 7:10am UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/1 "2009-02-03T07:10:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 7:52am UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/2 "2009-02-03T07:52:59Z")

</div>

What do you mean?

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

On 03/02/2009, at 5:10 PM, Jason L.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 7:22am UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/3 "2009-02-03T07:22:32Z")

</div>

> ```
> if @x.nil?
> @x = var
> 
> ```

This could be rewritten as

```
    @x ||= var

```

> ```
> elsif @y.nil?
> 
> ```

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

```
    @y ||= var
    @z ||= var

```

instead – depending on what you want of course.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 12:52pm UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/4 "2009-02-03T12:52:12Z")

</div>

> This could be rewritten as
> 
> ```
> @x ||= var
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 2:46pm UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/5 "2009-02-03T14:46:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/6 "2009-02-03T15:09:07Z")

</div>

Jason L. 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 🙂

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/7 "2009-02-03T12:29:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leveille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a88e57/32.png) [@leveille](https://www.ruby-forum.com/u/leveille)\
**Post date:** [February 3, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/choose-parameter-to-solve-for/159290/8 "2009-02-03T14:05:04Z")

</div>

Jason L. 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?
