REBOL Technologies

Nit PICKing

Carl Sassenrath, CTO
REBOL Technologies
2-Jul-2008 18:30 GMT

Article #0367
Main page || Index || Prior Article [0366] || Next Article [0368] || 14 Comments || Send feedback

Here's a commonly repeating pattern: you need two pick one of two values based on a condition.

For, example, say you need to pick one of two strings. Sure, you can write:

str: either cond ["this"]["that"]

Of course, that line seems inefficient because you're evaluating one of two blocks. You're forcing the interpreter to nest (and nesting is for the birds unless you're really computing something.)

Instead, I often write the more optimal expression:

str: pick ["this" "that"] cond

The pick action is fast, but cond better be TRUE or FALSE (or 1 or 2) or boom - throw exception.

Yet even further, if the values are actually variables the above becomes a bit longer...

str: get pick [this that] cond

Being a greedy block-miser, I must say I'm dissatisfied by any of these methods. (Insatisfait, as I am learning to say.) So, these days I've started writing:

str: pick1 cond this that

Of course, since pick1 is a user-defined function, it's not really more efficient in evaluation, it just looks better.

All of this is really just nit picking... but it's funny how little nits eat away at you during long coding sessions. But, dreaming... if pick1 were a native... now maybe we have something there. (Would take maybe 10 minutes to add it.)

Of course, then we'd need a good name for it, and you know what that means...

What do you think? Is this food for thought or nits for picking?

14 Comments

Updated 22-Nov-2017   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code