[TIP] Sorting by ValueList and synchronized sorting

Richard Gaskin ambassador at fourthworld.com
Tue Jun 17 20:53:35 EDT 2008


J. Landman Gay wrote:

> Richard Gaskin wrote: 
>> It's very handy, but a cognitive hurdle for me at first.  I'd never seen 
>> anything like it before.
> 
> You probably blocked it. :) 
> <http://search.gmane.org/?query=custom+sort&group=gmane.comp.ide.revolution.user>

Wow.  So many posts I've overlooked.  Must have had stuff in beta during 
those threads. :)

Glad I took the time today, thought.  You _can_ teach an old dog new 
tricks. :)


> Speaking of mental gymnastics, here's my favorite. I use it a lot. I 
> didn't write it, it was written by Tony Root from HyperPro back in the 
> day, and turned into a function by Paul Foraker:
> 
> function whichOne var,fld1,fld2  -- from a handler by Tony Root
> -- Handles a case where you need to return one value if your key is 
> empty, another if not.
> 
>    return (item offset(char 1 of (var = empty),"tf") of quote & fld1 
> &","& fld2 & quote)
> end whichOne
> 
> Paul says:
> 
> "There's a great scripter working at Digital Pictures named Tony Root, 
> who wrote a one-line boolean evaluator that I corrupted into a function. 
> This handles the case where you need to return one value if your key is 
> empty, another if not. For example, if the social security number field 
> (var) is empty, then return "Please fill in your social security number" 
> (fld1); otherwise, return "Now please fill in the form on the next 
> page." (fld2). The elegant part (which Tony wrote) is getting the offset 
> of char 1 of an expression that resolves to true or false, of "tf". Wish 
> I'd thought of that. "
> 
> You don't have to use it for just empty/not empty. With some 
> adjustments, you can use it to choose between any two values.

In other languages that's called iff.  Paul's/Tony's implementation is 
very clever, but I believe it benchmarks a bit faster (and is easier to 
read) when written as an if-else without the itemoffset and concatenation:

function whichOne pVal, pTrue, pFalse
    if pVal is empty then return pTrue
    else return pFalse
end whichOne


A more classic iff would evaluate any expression to true or false - this 
one should handle empties as false as well:

function iff pExp, pTrue, pFalse
   if pExp is true then return pTrue
   else return pFalse
end iff

-- 
  Richard Gaskin
  Managing Editor, revJournal
  _______________________________________________________
  Rev tips, tutorials and more: http://www.revJournal.com



More information about the use-livecode mailing list