Idiom wanted (now hiring!)

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)

a = x unless (x = func(b)).nil?

a = func(b) if func(b)

a = x if (x = func(b)).


Ola B. (http://ola-bini.blogspot.com)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http://www.ki.se)
OLogix Consulting (http://www.ologix.com)

“Yields falsehood when quined” yields falsehood when quined.

Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

http://rubylution.ping.de/articles/2007/03/01/the-opposite-of-blank-in-rails

On 4/13/07, Joel VanderWerf [email protected] wrote:

Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
these two are not equivalent, just imagine func(b) returning false
a = func(b) if func(b)

a = func(b) || a
and if a did not exist before?
Well it still works, as I expected given the poster of the message, but
I really
feel this is not consistent behavior

@a = f(b) || @a
of course that nicely corresponds to the auto nilification of
undefined instance vars, like it or hate it.

But for local variables?

Cheers
Robert

Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

a = func(b) || a

Hi,

At Fri, 13 Apr 2007 17:57:47 +0900,
Robert D. wrote in [ruby-talk:247783]:

a = func(b) || a
and if a did not exist before?

a does exist at the assignment and initialized as nil.

Hi,

Am Freitag, 13. Apr 2007, 15:02:18 +0900 schrieb Jonathan:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

As far as I see only the second statement makes a difference
when the return value is `false’. So,

a = func(b) || nil

would do!?

Bertram

On 13.04.2007 08:02, Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

Just to throw something obvious (which has not been mentioned so far)
into the mix:

x = func(b)
a = x if x

:slight_smile:

robert

On 4/13/07, Robert K. [email protected] wrote:

a = x if x
x = func b
a ||=x

How many combinations might we come up with ;)?

:slight_smile:

    robert

Robert

On 4/13/07, Nobuyoshi N. [email protected] wrote:

Nobu Nakada

Thx Nobu, that is as a matter of fact a “logical” and “functional”
explanation, but my question was rather conceptional.
Do make it clear I will be a little bit blunt, forgive me.
I do not like that behavior!
And you ?
Cheers
Robert

On 13-Apr-07, at 2:02 AM, Jonathan wrote:

Is there a cool way to do this without calling the function twice?:

a = func(b) unless func(b).nil? (AKA)
a = func(b) if func(b)

In lisp there are libraries of utilities that provide what is called
‘anaphoric’ versions of control structures etc. They are
simultaneously widely used and frowned upon. Paul Graham is one of
the primary sources of code for this.

Ruby can’t really do what lisp does, but here is a hacky
approximation and some test cases. You’ll see why they are frowned
upon in the test cases (lisp doesn’t do any better than ruby at
avoiding errors).

Actually, what follows is the output of ‘irb --prompt-mode xmp’ so,
should you want to actually use this stuff, you’ll have to clean up
the output. I don’t think I recommend using it in Ruby by the way.

Cheers,
Bob

module Anaphoric
def ifa(v)
if v then
@it = v
yield
end
end

def ifa2(v)
if v then
yield(v)
end
end
end
==>nil

include Anaphoric
==>Object

ifa (2 + 3 + 4) do
puts “1 – it #{@it}”
end
1 – it 9
==>nil

ifa2 (2 + 3 + 4) do | it |
puts “2 – it #{it}”
end
2 – it 9
==>nil

ifa (2 + 3 + 4) do
puts “3a – it #{@it}”
ifa (99 + 2 + 3 + 4) do
puts “3b – it #{@it}”
end
puts “3c – it #{@it}”
end
3a – it 9
3b – it 108
3c – it 108
==>nil

ifa2 (2 + 3 + 4) do | it |
puts “4a – it #{it}”
ifa2 (99 + 2 + 3 + 4) do | it |
puts “4b – it #{it}”
end
puts “4c – it #{it}”
end
4a – it 9
4b – it 108
4c – it 108
==>nil

ifa2 (2 + 3 + 4) do | it |
puts “5a – it #{it}”
ifa2 (99 + 2 + 3 + 4) do | it2 |
puts “5b – it #{it2}”
end
puts “5c – it #{it}”
end
5a – it 9
5b – it 108
5c – it 9
==>nil


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


Bob H. – tumblelog at <http://
www.recursive.ca/so/>
Recursive Design Inc. – http://www.recursive.ca/
xampl for Ruby – http://rubyforge.org/projects/xampl/

On 13.04.2007 13:07, Robert D. wrote:

x = func(b)
a = x if x
x = func b
a ||=x

How many combinations might we come up with ;)?

Certainly a lot more. However, my main point was to simply store the
result of func(b) in another variable and reuse that. Sometimes the
cool solutions are not without issues (see the other postings) and a
pragmatic solution can be better at times. :slight_smile:

Kind regards

robert

On 13.04.2007 14:29, Robert D. wrote:

a = x if x
is kind of not
a ||=x
Sorry Folks

gg I wanted to leave that for you. :slight_smile:

Seriously, I was more focused on clarifying my intentions which I felt I
did not make clear very well. :slight_smile:

Kind regards

robert

On 4/13/07, Robert K. [email protected] wrote:

On 13.04.2007 13:07, Robert D. wrote:

Certainly a lot more. However, my main point was to simply store the
result of func(b) in another variable and reuse that. Sometimes the
cool solutions are not without issues (see the other postings) and a
pragmatic solution can be better at times. :slight_smile:
Sure no argument here.
I just screwed up, I am touched that you have not even seen my serious
blunder

a = x if x
is kind of not
a ||=x
Sorry Folks
Robert

On 4/13/07, Eleanor McHugh [email protected] wrote:

a = x if x
is kind of not
a ||=x

I suspect we’ve all made that mistake at one time or another :wink:
Really nice of you to say so, but to be honest, 99.99999% of the
people having made that mistake wisely refused to send it to the ML :wink:

R.

On 13 Apr 2007, at 13:29, Robert D. wrote:

is kind of not
a ||=x

I suspect we’ve all made that mistake at one time or another :wink:

Ellie

Eleanor McHugh
Games With Brains

raise ArgumentError unless @reality.responds_to? :reason

On 13 Apr 2007, at 14:37, Robert D. wrote:

On 4/13/07, Eleanor McHugh [email protected] wrote:

On 13 Apr 2007, at 13:29, Robert D. wrote:

a = x if x
is kind of not
a ||=x

I suspect we’ve all made that mistake at one time or another :wink:
Really nice of you to say so, but to be honest, 99.99999% of the
people having made that mistake wisely refused to send it to the ML :wink:

I know. I was one of them >;o

Ellie

Eleanor McHugh
Games With Brains

raise ArgumentError unless @reality.responds_to? :reason

From: Robert D. [mailto:[email protected]]

On 4/13/07, Robert K. [email protected] wrote:

> On 13.04.2007 08:02, Jonathan wrote:

> > a = func(b) unless func(b).nil? (AKA)

> > a = func(b) if func(b)

> x = func(b)

> a = x if x

x = func b

a ||=x

irb
irb(main):001:0> a=1
=> 1
irb(main):002:0> a=func()
NoMethodError: undefined method `func’ for main:Object
from (irb):2
from :0
irb(main):004:0> a=func() rescue a
=> 1
irb(main):006:0> b=func() rescue b
=> nil

“Robert D.” [email protected] writes:


Nobu Nakada

Thx Nobu, that is as a matter of fact a “logical” and “functional”
explanation, but my question was rather conceptional.
Do make it clear I will be a little bit blunt, forgive me.
I do not like that behavior!
And you ?

It’s very philosophical:

$ ruby -e ‘thing = thing; p thing’
nil

“Robert D.” [email protected] writes:

You see it is not the isolated semantics of x=x, but the context of
other semantics that disturbs me (well disturbs me just a tiny little
bit ;).

So you are proposing all undefined locals return nil too? }}:slight_smile: