View Single Post
Old 05-05-2014, 12:20 PM   #16
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,467
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by davidfor View Post
chaley: I like it. My vote goes for the first version. The second version is a lot more flexible, but I think the first is much simpler to read and understand. With the second I know I would mess up the arguments and spend hours trying to sort out. And if I didn't put them in order, I would miss a range somewhere.

I sort of like the idea of having it as:

Code:
first_matching_compare(v, val1, lt1, val2, lt2,  ..., ltn, elseval)
That reads to me as: val1 if v<lt1, val2 if lt1<=v<lt2, val3 if lt2<=v<lt3 and so on until elseval if v>=ltn. But, I think that would be inconsistent with the other template functions.
You are right, that is inconsistent with all the other similar functions.

I submitted the changes to Kovid (not yet merged) for a "first_matching_cmp" function. I decided to use "cmp" so that it was consistent with the numeric comparision function "cmp" and not confused with strcmp. If there is demand I can also add a first_matching_strcmp, but I don't see many use cases for it.

The inline doc for the new function is
Code:
_('first_matching_cmp(val, cmp1, result1, cmp2, r2, ..., else_result) -- '
            'compares "val < cmpN" in sequence, returning resultN for '
            'the first comparison that succeeds. Returns else_result '
            'if no comparison succeeds. Example: '
            'first_matching_cmp(10,5,"small",10,"middle",15,"large","giant") '
            'returns "middle". The same example with a first value of 16 '
            'returns "giant".')
EDIT: Kovid committed the change to the "master" branch.

Last edited by chaley; 05-05-2014 at 12:24 PM.
chaley is offline   Reply With Quote