Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

el(se)if constriction

 [1/7] from: peoyli:algonet:se at: 27-Oct-2000 16:21


Here's a function I wrote that I find useable (or, is there a better way of doing this ?): elif: func [ {Do the block that a test condition is true for} elif-block [block!] {A block of conditions and blocks to be evaluated on a match} /default def_case {A block to evaluate if no conditions were true} ][ either found? find reduce elif-block true [ do second find reduce elif-block true ][ either default [ do def_case ][ none ] ] ] -- Attached file included as plaintext by Listar -- -- File: elif-examples.r REBOL [] ; Example 1: a simple match, check one word for an exact value (which ; also could be done with the switch function ; Result: "third match" ; line1: "third match" elif [ line1 = "first match" [ print "first match" ] line1 = "second match" [ print "second match" ] line1 = "third match" [ print "third match" ] line1 = "fourth match" [ print "fourth match" ] ] ; Example 2: check two words for the first value that matches ; Result: "just another test" ; line1: "nothing" line2: "just another test" elif [ line1 = "first match" [ print "first match" ] line1 = "second match" [ print "second match" ] line1 = "third match" [ print "third match" ] line2 = "just another test" [ print "just another test" ] line1 = "fourth match" [ print "fourth match" ] line1 = "nothing" [ print "should never get here" ] ] ; Example 3: check two words, but also use a default for no match ; Result: "no match" ; line1: "nothing" line2: "some more tests" elif/default [ line1 = "first match" [ print "first match" ] line1 = "second match" [ print "second match" ] line1 = "third match" [ print "third match" ] line1 = "fourth match" [ print "fourth match" ] line2 = "just another test" [ print "just another test" ] ][ print "no match" ] ; Example 4: check two words, which the second of them is checked with ; the parse function. Supply a default if nothing matches ; Result: "more" ; line1: "nothing" line2: "some more tests" elif/default [ line1 = "first match" [ print "first match" ] line1 = "second match" [ print "second match" ] line1 = "third match" [ print "third match" ] line1 = "fourth match" [ print "fourth match" ] parse line2 [to "more" copy result to "tests" to end] [ print result ] line2 = "just another test" [ print "just another test" ] ][ print "no match" ]

 [2/7] from: al:bri:xtra at: 28-Oct-2000 8:03


> ...is there a better way of doing this?
You could try this: Ifs: function [ "Multiple ifs" Ifs [block!] /Default Case [block!] ][Block][ while [not empty? Ifs] [ Block: do/next Ifs if first Block [return do first second Block] Ifs: next second Block ] if Default [return do Case] none ] Example: ifs/default [ parse Request/1 [thru "POST " to end] [ append Request first Connection ; Get the form data. Post Connection Request ] parse Request/1 [thru "GET " copy Filename to " HTTP" to end] [ Filename: to-file replace/all Filename " " " " Get Connection Web Filename ] ][ Forbidden Connection ] Example: ifs/default [ not found? find Absolute Web [ Forbidden Connection ] not exists? Absolute [ Not-Found Connection Web Filename ] dir? Absolute [ ifs/default [ exists? Index: join Absolute %index.htm [ Get-File Connection Index ] exists? Index: join Absolute %Index.html [ Get-File Connection Index ] ][ Get-Directory Connection Web Filename ] ] ][ Get-File Connection Absolute ] Andrew Martin ICQ: 26227169 http://members.nbci.com/AndrewMartin/

 [3/7] from: lmecir:geocities at: 27-Oct-2000 21:54


Hi, you can try http://www.rebol.org/general/pif.r Regards Ladislav

 [4/7] from: g:santilli:tiscalinet:it at: 28-Oct-2000 15:34


Hello P-O! On 27-Ott-00, you wrote: PY> Here's a function I wrote that I find useable (or, is there a PY> better way of doing this ?): I usually do it this way: any [ if condition1 [ code1 true ; needed only if code might return false or none ] if condition2 [ code2 true ] ... (default code) ] It is readable and evaluates the conditions in order; it is probably faster than writing your own, too. Regards, Gabriele. -- Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/

 [5/7] from: peoyli:algonet:se at: 28-Oct-2000 18:23


Andrew, Ladislav & Gabriele, Thanks for your answers to this one. I have tested all of these together with my 'elif' construction, and found out that the difference in speed between these four alternatives are small, and varying depending on how many conditions to test for. Some results Data set: 70 files, with identical (but different) data to be parsed (tv program tables), about 20-50 entries in each of the files. The conditions are using parse to extract the start time, title & description, and title only (if no description was available). 3 tests 9 tests (6 extra of the most advanced) elif (/me) 0:51 1:13 - reduce is probably slowing it down pif (Ladislav) 0:50 1:11 ifs (Andrew) 0:56 1:11 any/if (Gabriele) 1:06 1:01 - No idea why 9 tests is faster than 3 so, the any/if combination is both the slowest and the fastest one (and I did not mix up the test times between the 3 and 9 tests). /PeO

 [6/7] from: lmecir:geocities at: 28-Oct-2000 23:12


Hi PeO, if you like to do some tests, try the examples you can find in http://www.rebol.org/general/pif.r You will find out, that there are some differences. Regards Ladislav

 [7/7] from: joel:neely:fedex at: 28-Oct-2000 17:47


Hi, P-O (or is it PeO?) Thanks for the numbers! It's really helpful to me to have this kind of comparison of approaches to a task. [rebol-bounce--rebol--com] wrote:
> Some results > Data set: 70 files, with identical (but different) data to be parsed
<<quoted lines omitted: 8>>
> so, the any/if combination is both the slowest and the fastest one > (and I did not mix up the test times between the 3 and 9 tests).
My habit is to rerun a benchmark test a few times (3-5) and average the results. That tends to eliminate transient events that may affect a specific timing, such as O/S flushing its buffers, REBOL doing garbage collection, and the effect of other processes running (if you're in an environment that supports multitasking). -jn-

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted