[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Idiom wanted (now hiring!

Jonathan

4/13/2007 6:02:00 AM

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)

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

28 Answers

Ola Bini

4/13/2007 6:30:00 AM

0

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 Bini (http://ola-bini.bl...)
JvYAML, RbYAML, JRuby and Jatha contributor
System Developer, Karolinska Institutet (http:/...)
OLogix Consulting (http://www....)

"Yields falsehood when quined" yields falsehood when quined.



Joel VanderWerf

4/13/2007 6:45:00 AM

0

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

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Florian Frank

4/13/2007 7:47:00 AM

0

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

--
Florian Frank

Robert Dober

4/13/2007 8:58:00 AM

0

On 4/13/07, Joel VanderWerf <vjoel@path.berkeley.edu> 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
>
> --
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
>
>


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Bertram Scharpf

4/13/2007 9:20:00 AM

0

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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Nobuyoshi Nakada

4/13/2007 9:23:00 AM

0

Hi,

At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober 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.

--
Nobu Nakada

Robert Klemme

4/13/2007 10:28:00 AM

0

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

:-)

robert

Robert Dober

4/13/2007 11:06:00 AM

0

On 4/13/07, Nobuyoshi Nakada <nobu@ruby-lang.org> wrote:
> Hi,
>
> At Fri, 13 Apr 2007 17:57:47 +0900,
> Robert Dober 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.
>
> --
> 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

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Dober

4/13/2007 11:07:00 AM

0

On 4/13/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> 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
x = func b
a ||=x

How many combinations might we come up with ;)?
>
> :-)
>
> robert
>
>

Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Bob Hutchison

4/13/2007 12:14:00 PM

0


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

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