Subject: bug#27584: 26.0.50; alist-get: Add optional arg TESTFN

From: Tino Calancha <[email-masked]>
Date: Sat, 8 Jul 2017 16:02:12 +0900 (JST)

On Sat, 8 Jul 2017, Eli Zaretskii wrote:

>> From: Nicolas Petton <[email-masked]>
>> Date: Fri, 07 Jul 2017 21:47:03 +0200
>> Cc: [email-masked], Tino Calancha <[email-masked]>
>>
>> +      if (NILP (testfn))
>> +	{
>> +	  if (CONSP (car)
>> +	      && (EQ (XCAR (car), key) || !NILP (Fequal (XCAR (car), key))))
>> +	    return car;
>> +	}
>> +      else if (CONSP (car) && (!NILP (call2 (testfn, (XCAR (car)), key))))
>> +	{
>> +	  return car;
>> +	}
>
> No need for braces when there's only one line to enclose.
> Also, no need for parentheses around "!NILP (...)".
>
> Bonus points for simplifying the code by determining TESTFN up front,
> then having only one of the above two clauses.
Do you mean something like this?

{
   Lisp_Object tail = list;
   Lisp_Object fn = NILP (testfn) ? Qequal : testfn;
     FOR_EACH_TAIL (tail)
       {
         Lisp_Object car = XCAR (tail);
         if (CONSP (car) && !NILP (call2 (fn, (XCAR (car)), key)))
           return car;
       }

   CHECK_LIST_END (tail, list);
   return Qnil;
}

;; This is shorter but now the default case, because the call2, is less 
;; efficient than just using Fequal, right?