Idiom wanted (now hiring!)

J

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)
 
O

Ola Bini

J

Joel VanderWerf

Jonathan said:
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
 
R

Robert Dober

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
 
B

Bertram Scharpf

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
 
N

Nobuyoshi Nakada

Hi,

At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober wrote in [ruby-talk:247783]:
and if a did not exist before?

a does exist at the assignment and initialized as nil.
 
R

Robert Klemme

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
 
R

Robert Dober

Hi,

At Fri, 13 Apr 2007 17:57:47 +0900,
Robert Dober wrote in [ruby-talk:247783]:
and if a did not exist before?

a does exist at the assignment and initialized as nil.
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
 
B

Bob Hutchison

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



 
R

Robert Klemme

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

Kind regards

robert
 
R

Robert Dober

On 13.04.2007 13:07, Robert Dober 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. :)
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
 
R

Robert Klemme

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

*gg* I wanted to leave that for you. :)

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

Kind regards

robert
 
E

Eleanor McHugh

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

I suspect we've all made that mistake at one time or another ;)

Ellie

Eleanor McHugh
Games With Brains
 
R

Robert Dober

I suspect we've all made that mistake at one time or another ;)
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 ;)

R.
 
E

Eleanor McHugh

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 ;)

I know. I was one of them >;o


Ellie

Eleanor McHugh
Games With Brains
 
P

Peña, Botp

From: Robert Dober [mailto:[email protected]]=20
# > On 13.04.2007 08:02, Jonathan wrote:
# > > a =3D func(b) unless func(b).nil? (AKA)
# > > a =3D func(b) if func(b)
# > x =3D func(b)
# > a =3D x if x
# x =3D func b
# a ||=3Dx
irb(main):001:0> a=3D1
=3D> 1
irb(main):002:0> a=3Dfunc()
NoMethodError: undefined method `func' for main:Object
from (irb):2
from :0
irb(main):004:0> a=3Dfunc() rescue a
=3D> 1
irb(main):006:0> b=3Dfunc() rescue b
=3D> nil
 
R

Robert Dober

Robert Dober said:
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.
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

That might be to my liking too if there were not

======================
501/1 > ruby -e '@x=@x;p @x'
nil
--------------------------->
503/3 > ruby -e 'p @x'
nil
======================
504/4 > ruby -e 'x=x;p x'
nil
-------------------------->
505/5 > ruby -e 'p x'
-e:1: undefined local variable or method `x' for main:Object (NameError)
======================

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

Cheers
Robert
 
R

Robert Dober

So you are proposing all undefined locals return nil too? }}:)
No I am too young to die, because that means we need python call syntax
for the implicit receiver, maybe you want some parens too ;).
Beware I am writing this in the best intent.
Ok fooled around enough.

The real problem is the implicit declaration of variables of course.
I feel that it should be hidden which means it should take place after
the RHS evaluation, but in practice it takes place before.

Well I can life with that :)

Cheers
Robert
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top