Elseif?

Martin DeMello wrote:

···

Joel VanderWerf vjoel@path.berkeley.edu wrote:

x = 1.5

case true
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

Sorta like cond in lisp…

Nice!! You should add it to the RubyIdioms wiki page.

But I’ve never actually used it. :slight_smile:

Much nicer that way. Now I will use it.

I’ve updated the RubyIdioms page.

···

nobu.nokada@softhome.net wrote:

Hi,

At Sun, 8 Dec 2002 06:18:52 +0900, > Joel VanderWerf wrote:

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Hi –

···

On Sun, 8 Dec 2002 nobu.nokada@softhome.net wrote:

Hi,

At Sun, 8 Dec 2002 06:18:52 +0900, > Joel VanderWerf wrote:

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

“ts” decoux@moulon.inra.fr wrote in message
news:200212081116.gB8BGgU27230@moulon.inra.fr

(This is based on my >20 year old memory of FORTRAN 77, so I could be
off in the details. I believe “computed GOTO” is deprecated in current
FORTRANs. :slight_smile:

I hope it’s deprecated. I take me 3 days to debug one of my FORTRAN 4
program written with computed GOTO (ACP program) and it’s worst than a
case statement. But I always write stupid things :-))

The ocaml bytecode interpreter on gcc runs 50% faster than the equivalent on
MSVC despite the fact that MSVC is generally optimizing better than gcc. The
reason? gcc supports computed gotos which apparently allows the interpreter
to translate opcodes to addresses of subroutines, thus avoiding an extra
computational step for each instruction.

Mikkel

pigeon% ruby -e ‘case; when 12; puts 24; end’

Another thing that would be neat to see, in my opinion, is a ‘caseall’

···

joe = 50
jane = 45

caseall
when joe > jane
puts “Joe is older than Jane”
when jane > joe
puts “Jane is older than Joe”
when 40 < joe && joe < 60
puts “Joe is middle aged”
when 40 < jane && jane < 60
puts “Jane is middle aged”
else
puts “Joe and Jane are the same age, and neither is middle aged”
end

In this case, it should print
“Joe is older than Jane”
“Joe is middle aged”
“Jane is middle aged”

The ‘else’ clause is only executed if none of the rest match.

I’ve used a construct like that in other languages. While it’s the same as
a bunch of if statements, it is a bit cleaner.

-Greg


Greg Millam
walker at deafcode.com

Parrot is using GCC’s computed gotos, too.

···

On Sun, Dec 08, 2002 at 11:53:04PM +0900, MikkelFJ wrote:

“ts” decoux@moulon.inra.fr wrote in message
news:200212081116.gB8BGgU27230@moulon.inra.fr

(This is based on my >20 year old memory of FORTRAN 77, so I could be
off in the details. I believe “computed GOTO” is deprecated in current
FORTRANs. :slight_smile:

I hope it’s deprecated. I take me 3 days to debug one of my FORTRAN 4
program written with computed GOTO (ACP program) and it’s worst than a
case statement. But I always write stupid things :-))

The ocaml bytecode interpreter on gcc runs 50% faster than the equivalent on
MSVC despite the fact that MSVC is generally optimizing better than gcc. The
reason? gcc supports computed gotos which apparently allows the interpreter
to translate opcodes to addresses of subroutines, thus avoiding an extra
computational step for each instruction.


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Sex, Drugs & Linux Rules
– MaDsen Wikholm, mwikholm@at8.abo.fi

But that’s a different animal. What I want/wanted was an elegant,
simple way to express successive (Boolean) tests as a multiway

(cond ((< x y) -1)
((= x y) 0)
((> x y) 1))

It’s an unfamiliar construct. I don’t doubt that when the switch or

It’s over 30 years old!

– Nikodemus

···

On Sun, 8 Dec 2002, Hal E. Fulton wrote:

Hello Dave,

lisp and clipper are greate languages but… :slight_smile: Charles Wetherell in
“Etudes for Programmers” (1978) describes language Mini which has
operator closest to Ruby case:

select str of
case (“on”): return 1;
case (“off”,“”): return 0;
otherwise: return -1;
end select

it’s the beast SELECT construction i ever seen and i was very pleased
what ruby includes just the same SELECT operator

Tuesday, December 10, 2002, 8:54:18 PM, you wrote:

···

If the latter, it has already existed in another language and looks like
this:

(cond
((expr1) (stmt1))
((expr2) (stmt2))
((expr3) (stmt3))

((exprN) (stmtN))
)

Those who know and remember Clipper (and Fox, and probably dBase) will also
recognise its case statement as being such a facility:

,----

Do Case
Case x >> 0
? “x >> 0”
Case x == 0
? “x == 0”
Case x < 0
? “x < 0”
Otherwise
? “Something wonderful has happened”
EndCase
`----


Best regards,
Bulat mailto:bulatz@integ.ru

One example where I have seen continuous improvement is with Chuck
Moore’s version of Forth (please ignore the broken record ;-).
His latest effort, called ColorForth, is smaller, faster, and
arguably, more elegant and powerful than previous incarnations.
By way of example, imagine a full VLSI CAD system, graphics, simulator,
and all, in around 500 lines of code (under 20k).

The Forth community, which likes ANSI stuff, is headed in a different
direction. And most people find the “continuous improvements” take
away their “favourite features”.

Chuck is not sentimental, from what I can tell. And not attached to
convention (a 24-key keyboard? Huffman encoding, not ASCII? Color
instead of punctuation?). Are these “less good”? Hard to say,
unless you just look at the results.

Regards,

-mark.

···

At 05:23 AM 12/8/2002 +0900, Dave Thomas wrote:

Unfortunately, many folks seem to feel that continuous improvement means
continuous accretion of features. I have a theory that in really, really
good designs, the total amount of stuff stays the same over time. As
improvements are added, less good things are discarded.

That may be true, within limits… I think even a good design
has some elasticity in it. I certainly don’t favor endless
expansion, but neither would I favor dumping something every
time a feature was added.

And no design is so good that it does not ultimately lead to
greater things.

Ruby is my favorite language – for what my opinion is worth,
as I’m not a language fanatic. But I don’t expect it will
always be my favorite language. In 2016, I hope to be
programming in something I like better, if my brain has not
fossillized. And I hope it is something that borrows heavily
from Ruby, and learns from mistakes that we don’t even yet
perceive.

But now I’m just indulging in idle dreaming.

Hal

···

----- Original Message -----
From: “Dave Thomas” dave@pragprog.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Saturday, December 07, 2002 2:23 PM
Subject: Re: (OT) Re: elseif?

On Sat, 2002-12-07 at 13:04, Hal E. Fulton wrote:

But of course, it’s the kai-zen principle at work. Take a hundred
baby steps forward, and suddenly it looks like a giant quantum leap.
That, I think, is why we like Ruby – or part of the reason – or
that’s true for me, anyway.

Unfortunately, many folks seem to feel that continuous improvement means
continuous accretion of features. I have a theory that in really, really
good designs, the total amount of stuff stays the same over time. As
improvements are added, less good things are discarded.

This isn’t easy in a programming language, where throwing stuff away
breaks backward compatibility.

I’d like to learn LISP and Latin too. Maybe if someone coded a LISP
environment whose interface (and code) was entirely in Latin… two birds one
stone???

There’s a perl module that does almost precisely that.

http://www.csse.monash.edu.au/~damian/papers/HTML/Perligata.html

Martin DeMello wrote:

x = 1.5

case true
when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

Sorta like cond in lisp…

Nice!! You should add it to the RubyIdioms wiki page.

But I’ve never actually used it. :slight_smile:

I added it, and included a nasty swipe at Hal :slight_smile:

Gavin

···

From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU

Joel VanderWerf vjoel@path.berkeley.edu wrote:

Hi,

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Much nicer that way. Now I will use it.

And so will I! That is amazing.

I’ve updated the RubyIdioms page.

Hmm, have to check that out… :slight_smile:

Hal

···

----- Original Message -----
From: “Joel VanderWerf” vjoel@PATH.Berkeley.EDU
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Sunday, December 08, 2002 12:04 AM
Subject: Re: elseif?

nobu.nokada@softhome.net wrote:

At Sun, 8 Dec 2002 06:18:52 +0900, > > Joel VanderWerf wrote:

Hi –

Hi –

Hi,

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.

Oh, and in case that wasn’t enough proof:

puts case
when x < 0; “x < 0”
when 0 <= x && x < 1; “x in interval [0,1)”
when 1 <= x && x < 2; “x in interval [1,2)”
when 2 <= x; “x >= 2”
end

David

···

On Sun, 8 Dec 2002 dblack@candle.superlink.net wrote:

On Sun, 8 Dec 2002 nobu.nokada@softhome.net wrote:

At Sun, 8 Dec 2002 06:18:52 +0900, > > Joel VanderWerf wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

;))))))))))))


                        - Martin J. Brown, Jr. -
                        - mjbjr@beaudesign.com -

  Public PGP Key ID: 0xDB184F7B keyserver: http://certserver.pgp.com/
···

On Sun, 8 Dec 2002 10:40:05 +0900 “Gavin Sinclair” gsinclair@soyabean.com.au wrote:

I’d like to learn LISP and Latin too. Maybe if someone coded a LISP
environment whose interface (and code) was entirely in Latin… two birds one
stone???

:slight_smile: OK, I meant in languages known/used by mere mortals
like nyself.

Hal

···

----- Original Message -----
From: “Nikodemus Siivola” tsiivola@cc.hut.fi
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, December 09, 2002 2:21 AM
Subject: Re: (OT) Re: elseif?

On Sun, 8 Dec 2002, Hal E. Fulton wrote:

But that’s a different animal. What I want/wanted was an elegant,
simple way to express successive (Boolean) tests as a multiway

(cond ((< x y) -1)
((= x y) 0)
((> x y) 1))

It’s an unfamiliar construct. I don’t doubt that when the switch or

It’s over 30 years old!

In article
Pine.LNX.4.44.0212080116210.2040-100000@candle.superlink.net,

Hi –

Hi,

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.

Oops, I think some more code just disappeared from my screen!!!

case
when x < 0; puts “x < 0”
when x < 1; puts “x in interval [0,1)”
when x < 2; puts “x in interval [1,2)”
else puts “x >= 2”
end

Ross

···

dblack@candle.superlink.net wrote:

On Sun, 8 Dec 2002 nobu.nokada@softhome.net wrote:

At Sun, 8 Dec 2002 06:18:52 +0900, > > Joel VanderWerf wrote:

In article
rshaw1961-AFB9AF.20591409122002@news-server.bigpond.net.au,

In article
Pine.LNX.4.44.0212080116210.2040-100000@candle.superlink.net,

Hi –

Hi,

x = 1.5

case

when x < 0; puts “x < 0”
when 0 <= x && x < 1; puts “x in interval [0,1)”
when 1 <= x && x < 2; puts “x in interval [1,2)”
when 2 <= x; puts “x >= 2”
end

If no expression after “case”, “when” clause evaled as true
will be executed.

Yet more proof of my theory, which is that as you work on a problem in
Ruby, and it gets better and more idiomatic, code disappears from the
screen.

Oops, I think some more code just disappeared from my screen!!!

case
when x < 0; puts “x < 0”
when x < 1; puts “x in interval [0,1)”
when x < 2; puts “x in interval [1,2)”
else puts “x >= 2”
end

And using David’s suggestion in another post and adding a a twist we
lose even more!!!

puts "x " + case
when x < 0; “< 0”
when x < 1; “in interval [0,1)”
when x < 2; “in interval [1,2)”
else “>= 2”
end

Although I kind of think I might have gone a little too far and lost
some readability in my quest to eliminate duplication. Fore!!!

Ross

···

Ross Shaw rshaw1961@yahoo.com.au wrote:

dblack@candle.superlink.net wrote:

On Sun, 8 Dec 2002 nobu.nokada@softhome.net wrote:

At Sun, 8 Dec 2002 06:18:52 +0900, > > > Joel VanderWerf wrote: