unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Break from a dolist
@ 2012-09-09  8:56 Cecil Westerhof
  2012-09-09  9:25 ` Jambunathan K
       [not found] ` <mailman.8449.1347182656.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Cecil Westerhof @ 2012-09-09  8:56 UTC (permalink / raw)
  To: help-gnu-emacs

I have the following code to fetch the email address belonging to a
mailing list. I use it to automatically fill the to address when
posting to a newsgroup.

    (defun dcbl-gnus-get-mailing-list-address ()
      "Get gnus-newsgroup-name mailing list address (if it has one);"
      (let ((to-address nil))
        (when gnus-newsgroup-name
          (dolist (item dcbl-gnus-mailing-lists)
            (when (string-match (car item) gnus-newsgroup-name)
              (setq to-address (cdr item)))))
        to-address))

But when the address is found, it would be better to leave the loop.
Is this possible?

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Break from a dolist
  2012-09-09  8:56 Break from a dolist Cecil Westerhof
@ 2012-09-09  9:25 ` Jambunathan K
       [not found] ` <mailman.8449.1347182656.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Jambunathan K @ 2012-09-09  9:25 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: help-gnu-emacs

Cecil Westerhof <Cecil@decebal.nl> writes:

> I have the following code to fetch the email address belonging to a
> mailing list. I use it to automatically fill the to address when
> posting to a newsgroup.
>
>     (defun dcbl-gnus-get-mailing-list-address ()
>       "Get gnus-newsgroup-name mailing list address (if it has one);"
>       (let ((to-address nil))
>         (when gnus-newsgroup-name
>           (dolist (item dcbl-gnus-mailing-lists)
>             (when (string-match (car item) gnus-newsgroup-name)
>               (setq to-address (cdr item)))))
>         to-address))
>
> But when the address is found, it would be better to leave the loop.
> Is this possible?


    (info "(elisp) Examples of Catch")

-- 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Break from a dolist
       [not found] ` <mailman.8449.1347182656.855.help-gnu-emacs@gnu.org>
@ 2012-09-09 12:49   ` Cecil Westerhof
  2012-09-09 13:52     ` Le Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Cecil Westerhof @ 2012-09-09 12:49 UTC (permalink / raw)
  To: help-gnu-emacs

Op zondag 9 sep 2012 11:25 CEST schreef Jambunathan K.:

>> I have the following code to fetch the email address belonging to a
>> mailing list. I use it to automatically fill the to address when
>> posting to a newsgroup.
>>
>> (defun dcbl-gnus-get-mailing-list-address ()
>> "Get gnus-newsgroup-name mailing list address (if it has one);"
>> (let ((to-address nil))
>> (when gnus-newsgroup-name
>> (dolist (item dcbl-gnus-mailing-lists)
>> (when (string-match (car item) gnus-newsgroup-name)
>> (setq to-address (cdr item)))))
>> to-address))
>>
>> But when the address is found, it would be better to leave the loop.
>> Is this possible?
>
>
> (info "(elisp) Examples of Catch")

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)))))))

The proof is in:
    (dcbl-get-tuple-value dcbl-gnus-mailing-lists "nnimap+gmail:INBOX\\.Info\\.bbdb")
and that does what it should.

There is one problem. First my 'tuple' was like:

    (setq dcbl-gnus-mailing-lists '(
                                    (":INBOX\\.Info\\.bbdb$"          . "bbdb-info@lists.sourceforge.net")
                                    .
                                    .
                                    .
                                    (":bedrijf/organisatie/org-mode$" . "emacs-orgmode@gnu.org")
                                    ))

But now it is like:

    (setq dcbl-gnus-mailing-lists '(
                                    (":INBOX\\\\.Info\\\\.bbdb$"      . "bbdb-info@lists.sourceforge.net")
                                    .
                                    .
                                    .
                                    (":bedrijf/organisatie/org-mode$" . "emacs-orgmode@gnu.org")
                                    ))

The backslashes for escaping the . have gone from two to four. Why is this?

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Break from a dolist
  2012-09-09 12:49   ` Cecil Westerhof
@ 2012-09-09 13:52     ` Le Wang
  2012-09-09 14:13       ` Jambunathan K
       [not found]       ` <mailman.8455.1347199946.855.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Le Wang @ 2012-09-09 13:52 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: help-gnu-emacs

On Sun, Sep 9, 2012 at 8:49 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>
> 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))))))


--
Le



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Break from a dolist
  2012-09-09 13:52     ` Le Wang
@ 2012-09-09 14:13       ` Jambunathan K
       [not found]       ` <mailman.8455.1347199946.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Jambunathan K @ 2012-09-09 14:13 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: help-gnu-emacs

Le Wang <l26wang@gmail.com> writes:

> On Sun, Sep 9, 2012 at 8:49 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>>
>> 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 :-).

> --
> Le
>
>

-- 



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Break from a dolist
       [not found]       ` <mailman.8455.1347199946.855.help-gnu-emacs@gnu.org>
@ 2012-09-09 19:47         ` Cecil Westerhof
  0 siblings, 0 replies; 6+ messages in thread
From: Cecil Westerhof @ 2012-09-09 19:47 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2012-09-09 19:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-09  8:56 Break from a dolist Cecil Westerhof
2012-09-09  9:25 ` Jambunathan K
     [not found] ` <mailman.8449.1347182656.855.help-gnu-emacs@gnu.org>
2012-09-09 12:49   ` Cecil Westerhof
2012-09-09 13:52     ` Le Wang
2012-09-09 14:13       ` Jambunathan K
     [not found]       ` <mailman.8455.1347199946.855.help-gnu-emacs@gnu.org>
2012-09-09 19:47         ` Cecil Westerhof

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).