alternatives to ? : contruct

  • Thread starter John-Mason P. Shackelford
  • Start date
J

John-Mason P. Shackelford

As an alternative to:

a =3D y =3D=3D z ? b : c=20

we can say:

a =3D (b if y =3D=3D x) || c

Can anyone think of others?

I'd really like to be able to say:

a =3D b if y=3D=3Dz otherwise c

but the precedence of _or_ is not low enough for this to work. Would
changing the precedence of _or_ to be lower than expression modifiers
brake much? I believe _or_ is presently just above the expression
modifiers today. Then again perhaps that would be confusing. Perhaps
an _otherwise_ operator would do the trick.

This is trivial really, but ruby specializes in niceties which many
might deem trivial but which all add up to an amazingly productive
environment.

--=20
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
(e-mail address removed)
http://pearsonedmeasurement.com
 
M

Martin DeMello

John-Mason P. Shackelford said:
As an alternative to:

a = y == z ? b : c

we can say:

a = (b if y == x) || c

Can anyone think of others?

a = if y == z then b else c end

martin
 
E

ES

Le 11/5/2005 said:
As an alternative to:

a =3D y =3D=3D z ? b : c=20

we can say:

a =3D (b if y =3D=3D x) || c

Can anyone think of others?

I'd really like to be able to say:

a =3D b if y=3D=3Dz otherwise c

but the precedence of _or_ is not low enough for this to work. Would
changing the precedence of _or_ to be lower than expression modifiers
brake much? I believe _or_ is presently just above the expression
modifiers today. Then again perhaps that would be confusing. Perhaps
an _otherwise_ operator would do the trick.

Is this not called the 'else' operator? :)
This is trivial really, but ruby specializes in niceties which many
might deem trivial but which all add up to an amazingly productive
environment.

John-Mason Shackelford

E
 
G

Gyoung-Yoon Noh

I like this one:

a =3D if condition then x else y end

and same way:

a =3D case condition when branch_x : value_x when branch_y : value_y
else other_value end


As an alternative to:
=20
a =3D y =3D=3D z ? b : c
=20
we can say:
=20
a =3D (b if y =3D=3D x) || c
=20
Can anyone think of others?
=20
I'd really like to be able to say:
=20
a =3D b if y=3D=3Dz otherwise c
=20
but the precedence of _or_ is not low enough for this to work. Would
changing the precedence of _or_ to be lower than expression modifiers
brake much? I believe _or_ is presently just above the expression
modifiers today. Then again perhaps that would be confusing. Perhaps
an _otherwise_ operator would do the trick.
=20
This is trivial really, but ruby specializes in niceties which many
might deem trivial but which all add up to an amazingly productive
environment.
=20
--
John-Mason Shackelford
=20
Software Developer
Pearson Educational Measurement
=20
2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
(e-mail address removed)
http://pearsonedmeasurement.com
=20
=20


--=20
http://nohmad.sub-port.net
 
J

Joel VanderWerf

John-Mason P. Shackelford wrote:
...
I'd really like to be able to say:

a = b if y==z otherwise c

Just kidding:

class Object
def incase(cond)
if cond
self
else
yield
end
end
end

a = 3.incase(1==2) {"c"}
p a
a = 3.incase(1==1) {"c"}
p a
 
J

John-Mason P. Shackelford

I don't believe one can use _else_ with an expression modifier. If we
try to use an if block in this case, things get ugly fast:

a =3D if y =3D=3D x; b; else; c; end

Of course I may be missing something. I need to read more code than
just my own :s

--=20
John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
(e-mail address removed)
http://pearsonedmeasurement.com
 
J

James Edward Gray II

I don't believe one can use _else_ with an expression modifier. If we
try to use an if block in this case, things get ugly fast:

a = if y == x; b; else; c; end

Of course I may be missing something. I need to read more code than
just my own :s

The if ... then ... else ... end statements people have been posting
are perfectly legal Ruby. Fire up irb an give them a spin. It's
basically just collapsing an if statement to one line, then capturing
the proper return (since statements return things in Ruby). The
"then" is just a pretty replacement for the ; here.

Hope that helps.

James Edward Gray II
 
J

Jason Foreman

On May 11, 2005, at 2:37 PM, John-Mason P. Shackelford wrote:
=20
=20
The if ... then ... else ... end statements people have been posting
are perfectly legal Ruby. Fire up irb an give them a spin. It's
basically just collapsing an if statement to one line, then capturing
the proper return (since statements return things in Ruby). The
"then" is just a pretty replacement for the ; here.
=20
Hope that helps.
=20
James Edward Gray II
=20
=20

I'd prefer it with less ;'s as well:

a =3D if (y=3D=3Dz): b else c end

which works perfectly fine:

irb(main):025:0> a =3D if (1=3D=3D2): 'foo' else 'bar' end
=3D> "bar"

I really don't like the idea of having an else/otherwise/whatever
operator that allows the results of the statement to be the first and
last pieces, and the operators and conditions all squished in the
middle. B if A else C doesn't read well to me, and would get
confusing I think, whereas if A then B else C is very natural.


Jason
 
M

Martin DeMello

Joel VanderWerf said:
Just kidding:

class Object
def incase(cond)
if cond
self
else
yield
end
end
end

a = 3.incase(1==2) {"c"}
p a
a = 3.incase(1==1) {"c"}
p a

That's only lazy in its second argument :) Maybe

def only(&block)
block
end

class Proc
def if(arg)
arg ? self.call : arg
end
end

class Object
def then
self ? yield : self
end

def else
self ? self : yield
end

alias :eek:therwise :else
end

p (1==1).then { 5 }.else { 1/0 }

p only {1/0}.if(1 == 2).otherwise { 4 }

martin
 
G

gwtmp01

I'd prefer it with less ;'s as well:

a = if (y==z): b else c end

Is this really better than:

a = (y==z) ? b : c

?

The tertiary operator ?: can be used if you want
something terse and "if then else end" if you want something
more verbose. I'm not sure I see the need for yet another
variation.


Gary Wright
 
J

Jason Foreman

=20
Is this really better than:
=20
a =3D (y=3D=3Dz) ? b : c
=20
?
=20
The tertiary operator ?: can be used if you want
something terse and "if then else end" if you want something
more verbose. I'm not sure I see the need for yet another
variation.
=20
Gary Wright
=20
=20

I agree, I don't want another operator, when the existing ones are
more than sufficient. I personally try never to use the tertiary
operator anyway, because when I come back to read it later its not
always immediately obvious to me, but "if else end" always is obvious.

Jason
 
L

Logan Capaldo

As an alternative to:
=20
a =3D y =3D=3D z ? b : c
=20
we can say:
=20
a =3D (b if y =3D=3D x) || c
=20
Can anyone think of others?
=20
I'd really like to be able to say:
=20
a =3D b if y=3D=3Dz otherwise c
=20
but the precedence of _or_ is not low enough for this to work. Would
changing the precedence of _or_ to be lower than expression modifiers
brake much? I believe _or_ is presently just above the expression
modifiers today. Then again perhaps that would be confusing. Perhaps
an _otherwise_ operator would do the trick.
=20
This is trivial really, but ruby specializes in niceties which many
might deem trivial but which all add up to an amazingly productive
environment.
=20
--
John-Mason Shackelford
=20
Software Developer
Pearson Educational Measurement
=20
2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
(e-mail address removed)
http://pearsonedmeasurement.com
=20
=20

Heres one way:

x =3D=3D y and a =3D b or c
 
D

David A. Black

Hi --

Heres one way:

x == y and a = b or c

In the case where x != y, that expression will just return c, without
assigning it to a. (I learned this the hard way either here or on IRC
recently :)


David
 
L

Logan Capaldo

Hi --
=20
On Thu, 12 May 2005, Logan Capaldo wrote:
=20
=20
In the case where x !=3D y, that expression will just return c, without
assigning it to a. (I learned this the hard way either here or on IRC
recently :)
=20
David
=20
--
David A. Black
(e-mail address removed)
=20
=20

Yeah, that was the the point.

If (x =3D=3D y) then a =3D b else c end
is equivalent to x =3D=3D y and a =3D b or c
 
L

Logan Capaldo

=20
Yeah, that was the the point.
=20
If (x =3D=3D y) then a =3D b else c end
is equivalent to x =3D=3D y and a =3D b or c
=20

if you wanted to assign to a the result of the expression just like a
?: you'd do
a =3D (y =3D=3D x and b) or c

unfortunately you have to use the parens in this case

you could also do y =3D=3D x and a =3D b or a =3D c
 
M

Mark Hubbart

=20
Is this really better than:
=20
a =3D (y=3D=3Dz) ? b : c
=20
?

A little, for readability. I like using actual words as operators
whenever possible. This is, of course, excepting symbolic operators
like + and -, which I learned back in first grade, and carry as much
meaning as actual words.
The tertiary operator ?: can be used if you want
something terse and "if then else end" if you want something
more verbose. I'm not sure I see the need for yet another
variation.

First of all, it's the ternary operator :) As for the "if then else
end" statement, the dangling "end" at the... uh, *end*, is a bit of an
eyesore. It's what makes me shy away from using inline conditionals
like that.

I've thought a couple times that it would be nice to have something
like "otherwise" available. I wonder if it would be too difficult for
the parser to parse these two snippets the same:

----
a =3D if (y=3D=3Dz)
b
else
c
end
----
a =3D if (y=3D=3Dz) then b else c
----

That is, if else is on the same line as the code to execute if true,
with no semicolon, the "end" can be dropped. Or this, instead:

----
a =3D b if y=3D=3Dz else c
----

Can the parser definitively detect that the if...else is being used as
a statement modifier in this case?

Just some thoughts off the top of my head, here. I could easily waffle
on them if a good case is made against it.

cheers,
Mark
 
D

David A. Black

Hi --

Yeah, that was the the point.

If (x == y) then a = b else c end
is equivalent to x == y and a = b or c

But it's not equivalent to a = y == z ? b : c


David
 
G

gwtmp01

First of all, it's the ternary operator :)
Yep. My bad.

Since Ruby already has two version of this, a new
third version, would be the tertiary operator. :)

Gary Wright
 
J

John-Mason P. Shackelford

Jason,
I really don't like the idea of having an else/otherwise/whatever
operator that allows the results of the statement to be the first and
last pieces, and the operators and conditions all squished in the
middle. B if A else C doesn't read well to me, and would get
confusing I think, whereas if A then B else C is very natural.

A year ago I would have agreed with you whole heartedly, but I've
come to believe that some thoughts are better expressed with an
expression modifier rather than an if block. When I am trying to
emphasize an assignment, rather than a branch in execution flow,
conditional expressions fit the bill nicely where the subordination of
an if block (inline or not) distracts the reader from the flow of the
code. The syntax below clearly expresses that my main intent with this
line of code is the assignment a =3D b and if the reader cares to know
more detail about that assignment he can see that in the ordinary case
the assignment is performed when a given condition is true, though
some provision is made if it isn't.

I find myself wanting this as code evolves:

1. a =3D b=20
2. a =3D b if y=3D=3Dz
3. if y=3D=3Dz then a else b end =20
- or -
3. y=3D=3Dz ? a : b

Here at step three my main intent (the assignment) is now subordinated
to the condition, which in this case is of secondary concern. While in
some contexts emphasizing the branch will be most clear, in others it
would distract the reader from more important aspects of the code.

Like any language feature this syntax could be abused, but I submit
that expression modifiers exist not to save keystrokes, but to convey
a thought different from that communicated by an if block. The
expressive power of a richer expression modifier would help me to code
what I mean--which is exactly what we want out of a programming
language.


John-Mason Shackelford

Software Developer
Pearson Educational Measurement

2510 North Dodge St.
Iowa City, IA 52245
ph. 319-354-9200x6214
(e-mail address removed)
http://pearsonedmeasurement.com
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top