help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Break from a dolist


From: Cecil Westerhof
Subject: Re: Break from a dolist
Date: Sun, 09 Sep 2012 21:47:58 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)

Op zondag 9 sep 2012 16:13 CEST schreef Jambunathan K.:

>>> Thanks. That makes for better code. I now have the following code,
>>> that I am going to call from dcbl-gnus-get-mailing-list-address. Much
>>> more clear code and I have now a generic function that I can call from
>>> other places also.
>>>
>>> (defun dcbl-get-tuple-value (tuple index)
>>> "Get value from tuple indexed by index (if it exist);"
>>> (when index
>>> (catch 'loop
>>> (dolist (item tuple)
>>> (when (string-match (car item) index)
>>> (throw 'loop (cdr item)))))))
>>
>>
>> `dolist' (like all cl looping mechanisms) runs within an implicit anonymous
>> local return.  So you can simply use
>>
>> (defun dcbl-get-tuple-value (tuple index)
>> "Get value from tuple indexed by index (if it exist);"
>> (when index
>> (dolist (item tuple)
>> (when (string-match (car item) index)
>> (return (cdr item))))))
>>
>
> If you look at what the dolist does it is very similar to 
>
> (assoc-default    )
>
> probably with an non-default test param.
>
> So a dolist is not even needed :-).

In this case it can, but it is not the same. In my function I work
with a regular expression. For example the newsgroup is:
    nnimap+gmail:bedrijf/ontwikkeling/scala
and the 'key' is:
    :bedrijf/ontwikkeling/scala$


I solved it in the following way:

    (defun dcbl-get-tuple-value (tuple index)
      "Get value from tuple indexed by index (if it exist);"
      (assoc-default index tuple))

    (defun dcbl-gnus-get-mailing-list-address ()
      "Get gnus-newsgroup-name mailing list address (if it has one);"
      (string-match "[^:]*$" gnus-newsgroup-name)
      (dcbl-get-tuple-value dcbl-gnus-mailing-lists (match-string 0 
gnus-newsgroup-name)))

And removed the : and the $ from the keys.

On other places I can not do it like this, but here it resulted it
very nice code.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


reply via email to

[Prev in Thread] Current Thread [Next in Thread]