Re: [OT] language tricks (was: creating menu's)
On Tue, May 08, 2007 at 09:34:35PM -0400, Douglas Allan Tutty wrote: On Tue, May 08, 2007 at 01:22:10PM -0700, Bryan Irvine wrote: I need a fairly simple menu, and have thought about just simple selects but figured now would also be a good time to learn something new as well. It's nothing so complex that I need to go ncurses to do. Just a basic option 1 then option 3 then run some command thing. My front-ends I do in python. It doesn't have a case/select. I just use if/then/elif/ Then there's Fortran with computed gotos; very slick. I forget the syntax but is something like goto (10+choice) 11 ch1() ... 12 ch2() ... 13 ch3() ... It means that only one computation takes place instead of one comparison for each choice until one matches. Just pointing out: if Python can do the job at all, you almost certainly don't need that kind of micro-optimization in Fortran code. Also, this is a menu. Efficiency is not exactly a big goal. However, and this is where I go completely off-topic, while we're at it, you don't need Fortran for this, most languages have equivalent constructs (C): switch(option) { case 1: ... case 2: ... case 3: ... default: /* error! */ ... } or even void (*dispatch[])(void) = { proc_opt1, proc_opt2, proc_opt3 } void proc_opt1(void) { ... } void proc_opt2(void) { ... } void proc_opt3(void) { ... } In languages with higher order-functions, this can be written even more concisely (Scheme): (define dispatch (vector (lambda () ...) (lambda () ...) (lambda () ...))) A suiteable make-menu macro could even make something like (define toplevel-menu (make-menu (opt1 (lambda () ...)) (opt2 (lambda () ...)) (another menu another-menu))) (define another-menu (make-menu (opt3 (lambda () ...)) (opt4 (lambda () ...)) (top toplevel-menu))) do what it looks like it should do. However, all of this is massively overkill. Just use a shell script. Joachim -- TFMotD: mirroring-ports (7) - how to build a mirror for ports distfiles
Re: [OT] language tricks (was: creating menu's)
On Wed, May 09, 2007 at 10:56:57AM +0200, Joachim Schipper wrote: On Tue, May 08, 2007 at 09:34:35PM -0400, Douglas Allan Tutty wrote: On Tue, May 08, 2007 at 01:22:10PM -0700, Bryan Irvine wrote: I need a fairly simple menu, and have thought about just simple selects but figured now would also be a good time to learn something new as well. It's nothing so complex that I need to go ncurses to do. Just a basic option 1 then option 3 then run some command thing. My front-ends I do in python. It doesn't have a case/select. I just use if/then/elif/ Then there's Fortran with computed gotos; very slick. I forget the syntax but is something like goto (10+choice) for each choice until one matches. Just pointing out: if Python can do the job at all, you almost certainly don't need that kind of micro-optimization in Fortran code. Also, this is a menu. Efficiency is not exactly a big goal. I don't do enough programming to want to keep track of multiple languages. If I have to read a program in 10 years I want to know what its trying to do. C has too much punctuation everywhere. So I only program in Python and Fortran. However, and this is where I go completely off-topic, while we're at it, you don't need Fortran for this, most languages have equivalent constructs (C): In languages with higher order-functions, this can be written even more concisely (Scheme): However, all of this is massively overkill. Just use a shell script. Shell is too much like C (punctuation and spacing matter). (sorry if this sounds anti-unix). I use shell if its like a dos bat file, sequential. Once I have to test conditions and branch I switch to python. Then if something takes a long time (or I know it will before hand), I use fortran 77. Unfortunaly, I can't get my head around regex either. Two hours after I'v written it I can't understand it. So I code it in python or fortran. Doug.