Genii Weblog
Limits to cleverness
Mon 20 Dec 2004, 09:01 AM
Tweetby Ben Langhinrichs
Copyright © 2004 Genii Software Ltd.
What has been said:
254.1. Damien Katz (12/20/2004 10:21 AM)
This is so cool! Finally, years after I created all that stuff I'm seeing examples of what it can accomplish. I've never actually had occasion to use anything of the new stuff, so this is the closest I've come to seeing it in action.
Which ones faster? I don't know, I'd guess the first probably has a slight performance advantage, but it's so hard to read that it just isn't worth it.
Also, for readability I would use more temporary variables instead of nesting the expressions so deeply. There is practically no overhead using temp vars and it might make the formulas easier to understand.
Thanks for this post Ben, really cool stuff (to me anyway)!
254.2. Julian Robichaux (12/21/2004 03:00 PM)
Well, I won't call this a "better" solution, but I've been playing around a bit, and here's what I came up with (the formatting will probably end up getting shot, so I apologize in advance):
---------------------------------
Speaker := "Jean Claude van Damme" : "Catherine Zeta Smith" : "Tim Burton" : "Bruce Willis" : "Prince" : "Oscar de la Hoya";
REM {Add a tilde as a placeholder before words that begin with lowercase letters};
letters := "a b c d e f g h i j k l m n o p q r s t u v w x y z";
letterList := " " + @Explode(letters);
letterReplaceList := " ~" + @Explode(letters);
sParsed := @ReplaceSubstring(Speaker; letterList; letterReplaceList);
REM {If a name has any tildes, split at the first one; otherwise split at the last word};
slist := @Transform(sParsed; "S";
@If(@Contains(S; "~"); @Right(S; "~") + ", " + @Left(S; "~");
@Contains(S; " "); @RightBack(S; " ") + ", " + @LeftBack(S; " ");
S));
REM {Remove any extra tildes (for example, "John ~van ~der Schlumdt")};
slist := @Trim(@ReplaceSubstring(slist; "~"; ""));
REM {Sort the list};
sorted := @Sort(slist);
@Prompt([Ok]; "Sort By Last"; @Implode(sorted; @Char(10)));
REM {Change it back to firstname lastname format};
sorted_by_last := @Trim(@Word(sorted; ","; 2) + " " + @Word(sorted; ","; 1));
@Prompt([Ok]; "Sort By Last"; @Implode(sorted_by_last; @Char(10)));
---------------------------------
I guess if you wanted to cram it all together, you could also call it like:
---------------------------------
Speaker := "Jean Claude van Damme" : "Catherine Zeta Smith" : "Tim Burton" : "Bruce Willis" : "Prince" : "Oscar de la Hoya";
letters := "a b c d e f g h i j k l m n o p q r s t u v w x y z";
sorted := @Sort(@Trim(@ReplaceSubstring(@Transform(@ReplaceSubstring(Speaker; " " + @Explode(letters); " ~" + @Explode(letters)); "S"; @If(@Contains(S; "~"); @Right(S; "~") + ", " + @Left(S; "~"); @Contains(S; " "); @RightBack(S; " ") + ", " + @LeftBack(S; " "); S)); "~"; "")));
sorted_by_last := @Trim(@Word(sorted; ","; 2) + " " + @Word(sorted; ","; 1));
@Prompt([Ok]; "Sort By Last"; @Implode(sorted_by_last; @Char(10)));
---------------------------------
Same cat, different way to skin it. I suspect your method might be a little more efficient.
By the way, what are you using to color-code your Formula language stuff in HTML?
- Julian
254.3. Feri (05/12/2006 05:24 AM)
simpler one:
tmp := "Joe Litton":"Cindy Lou Who":"Mary Jane van der Welten":"Tom Duff":"Ben Langhinrichs":"Harry Belafonte"; @Right( @Sort( (@RightBack( tmp; " " ) + " " + @LeftBack( tmp; " " )) + "|" + tmp ); "|" )