* Exiting from mapc
@ 2024-12-31 19:35 Heime via Users list for the GNU Emacs text editor
2024-12-31 20:13 ` [External] : " Drew Adams
2025-01-02 10:53 ` Jean Louis
0 siblings, 2 replies; 17+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-12-31 19:35 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor
How can I exit when encountering 'safg or 'nogo
(defun amazones-falkotr-kntlr (actm-seqr)
(mapc
(lambda (actm)
;; Evaluate conditions and executes corresponding actions
;; based on matching clauses.
(cond
;; CASE
((eq actm 'safg)
(message "SAFG amazones-falkotr-kntlr"))
;; CASE
((eq actm 'armg)
(message "ARMG amazones-falkotr-kntlr")
;; Load falkotr.el from `load-path'.
(add-to-list 'load-path (amazones-fpln-waypt "FLKTR" 'sec))
(require 'falkotr))
;; CASE
((eq actm 'nogo)
(message "NOGO amazones-falkotr-kntlr"))
;; CASE
((eq actm 'go)
(message "GO amazones-falkotr-kntlr")
(falkotr-launch-kntlr '(armg go)))
;; CASE
(t
;; Unrecognised symbol, log error with offending symbol.
(message "NOGO amazones-falkotr-kntlr")
(message " └── ACTM Unrecognised: %s" actm))))
actm-seqr))
Sent with Proton Mail secure email.
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
2024-12-31 19:35 Exiting from mapc Heime via Users list for the GNU Emacs text editor
@ 2024-12-31 20:13 ` Drew Adams
2024-12-31 20:20 ` Heime via Users list for the GNU Emacs text editor
` (2 more replies)
2025-01-02 10:53 ` Jean Louis
1 sibling, 3 replies; 17+ messages in thread
From: Drew Adams @ 2024-12-31 20:13 UTC (permalink / raw)
To: Heime, Heime via Users list for the GNU Emacs text editor
> How can I exit when encountering 'safg or 'nogo
As usual, wrap with a `catch', and `throw' to it whenever you like.
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
2024-12-31 20:13 ` [External] : " Drew Adams
@ 2024-12-31 20:20 ` Heime via Users list for the GNU Emacs text editor
2024-12-31 22:30 ` Drew Adams
2025-01-01 1:53 ` Björn Bidar
[not found] ` <87wmffwb8w.fsf@>
2 siblings, 1 reply; 17+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2024-12-31 20:20 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime via Users list for the GNU Emacs text editor
Sent with Proton Mail secure email.
On Wednesday, January 1st, 2025 at 8:13 AM, Drew Adams <drew.adams@oracle.com> wrote:
> > How can I exit when encountering 'safg or 'nogo
>
>
> As usual, wrap with a `catch', and` throw' to it whenever you like.
Does the catch statement belong before mapc?
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
2024-12-31 20:20 ` Heime via Users list for the GNU Emacs text editor
@ 2024-12-31 22:30 ` Drew Adams
2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
0 siblings, 1 reply; 17+ messages in thread
From: Drew Adams @ 2024-12-31 22:30 UTC (permalink / raw)
To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor
> > > How can I exit when encountering 'safg or 'nogo
> >
> > As usual, wrap with a `catch', and` throw' to it whenever you like.
>
> Does the catch statement belong before mapc?
Yes - "wrap".
(defun amazones-falkotr-kntlr (actm-seqr)
(catch 'RTFM
(mapc (lambda (...)
<Do whatever, and `throw' any value,
from anywhere here, to 'RTFM>)
actm-seqr))))
1. `C-h f catch'
2. https://www.gnu.org/software/emacs/manual/html_node/elisp/Catch-and-Throw.html
Learning to use `catch' and `throw' with code
such as iteration is basic. And catch/throw is
a building block for implementing other iteration
etc. constructs.
The Elisp code that Emacs provides so freely to
you is a great place to learn about Elisp. Grep
for `catch' there...
Here's a simple example:
(defmacro while-no-input (&rest body)
"..."
(declare (debug t) (indent 0))
(let ((catch-sym (make-symbol "input")))
`(with-local-quit
(catch ',catch-sym
(let ((throw-on-input ',catch-sym))
(or (input-pending-p)
(progn ,@body)))))))
Here's another:
(defun get-window-with-predicate (predicate &optional minibuf all-frames default)
"..."
(catch 'found
(dolist (window (window-list-1
(next-window nil minibuf all-frames)
minibuf all-frames))
(when (funcall predicate window)
(throw 'found window)))
default))
The Elisp manual makes great bedtime reading.
Time better spent than asking help-gnu-emacs,
perhaps.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2024-12-31 20:13 ` [External] : " Drew Adams
2024-12-31 20:20 ` Heime via Users list for the GNU Emacs text editor
@ 2025-01-01 1:53 ` Björn Bidar
[not found] ` <87wmffwb8w.fsf@>
2 siblings, 0 replies; 17+ messages in thread
From: Björn Bidar @ 2025-01-01 1:53 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime, Heime via Users list for the GNU Emacs text editor
Drew Adams <drew.adams@oracle.com> writes:
>> How can I exit when encountering 'safg or 'nogo
>
> As usual, wrap with a `catch', and `throw' to it whenever you like.
Should dolist be better in this context anyway?
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
[not found] ` <87wmffwb8w.fsf@>
@ 2025-01-01 2:02 ` Drew Adams
2025-01-01 19:40 ` Björn Bidar
[not found] ` <874j2iwcff.fsf@>
0 siblings, 2 replies; 17+ messages in thread
From: Drew Adams @ 2025-01-01 2:02 UTC (permalink / raw)
To: Björn Bidar
Cc: Heime, Heime via Users list for the GNU Emacs text editor
> >> How can I exit when encountering 'safg or 'nogo
> >
> > As usual, wrap with a `catch', and `throw' to it whenever you like.
>
> Should dolist be better in this context anyway?
Of course, but it doesn't answer his question. ;-)
If a plain iteration, with no intermediate escape
to throw a value, is appropriate for what you want
to do, then by all means use it.
IOW, don't go catching and throwing willy nilly.
Like `GO TO'.
`dolist' and `mapc' etc. don't let you escape;
you must go through the whole list. Catch and
throw are available for when you need to get out
and return or do something directly, and not
waste time running down the rest of the list for
nothing.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-01 2:02 ` Drew Adams
@ 2025-01-01 19:40 ` Björn Bidar
[not found] ` <874j2iwcff.fsf@>
1 sibling, 0 replies; 17+ messages in thread
From: Björn Bidar @ 2025-01-01 19:40 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime, Heime via Users list for the GNU Emacs text editor
Drew Adams <drew.adams@oracle.com> writes:
>> >> How can I exit when encountering 'safg or 'nogo
>> >
>> > As usual, wrap with a `catch', and `throw' to it whenever you like.
>>
>> Should dolist be better in this context anyway?
>
> Of course, but it doesn't answer his question. ;-)
Sure I'm just saying this as an added comment. macp can be expensive
unless you invoke a handler for function anyway or use a mapc style
filter function such as seq-filter.
> If a plain iteration, with no intermediate escape
> to throw a value, is appropriate for what you want
> to do, then by all means use it.
>
> IOW, don't go catching and throwing willy nilly.
> Like `GO TO'.
>
> `dolist' and `mapc' etc. don't let you escape;
> you must go through the whole list. Catch and
> throw are available for when you need to get out
> and return or do something directly, and not
> waste time running down the rest of the list for
> nothing.
Another approach is to use second status variable when using while.
Are there instances where a throw and catch does not work in while loop?
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
[not found] ` <874j2iwcff.fsf@>
@ 2025-01-01 20:00 ` Drew Adams
2025-01-01 21:09 ` Björn Bidar
[not found] ` <87a5cautqb.fsf@>
0 siblings, 2 replies; 17+ messages in thread
From: Drew Adams @ 2025-01-01 20:00 UTC (permalink / raw)
To: Björn Bidar
Cc: Heime, Heime via Users list for the GNU Emacs text editor
> Are there instances where a throw and catch
> does not work in while loop?
Dunno what you have in mind by "does not work",
but no.
throw unilaterally transfers control to the
innermost catch that has the same TAG as the
throw.
But if there's no catch with that TAG then an
error is raised. (Does that qualify for you
as not working?)
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-01 20:00 ` Drew Adams
@ 2025-01-01 21:09 ` Björn Bidar
[not found] ` <87a5cautqb.fsf@>
1 sibling, 0 replies; 17+ messages in thread
From: Björn Bidar @ 2025-01-01 21:09 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime, Heime via Users list for the GNU Emacs text editor
Drew Adams <drew.adams@oracle.com> writes:
> But if there's no catch with that TAG then an
> error is raised. (Does that qualify for you
> as not working?)
By not working I meant that using throw and success in a C-like break
and that not working.
I wouldn't call your example not working but it is something to take
into account when coming from other languages.
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
[not found] ` <87a5cautqb.fsf@>
@ 2025-01-01 21:59 ` Drew Adams
0 siblings, 0 replies; 17+ messages in thread
From: Drew Adams @ 2025-01-01 21:59 UTC (permalink / raw)
To: Björn Bidar
Cc: Heime, Heime via Users list for the GNU Emacs text editor
> > But if there's no catch with that TAG then an
> > error is raised. (Does that qualify for you
> > as not working?)
>
> By not working I meant that using throw and success in a C-like break
> and that not working.
Dunno the answer; sorry. I've been on a C break
for about 40 years now, and plan to stay on it. ;-)
> I wouldn't call your example not working but it is something to take
> into account when coming from other languages.
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
2024-12-31 22:30 ` Drew Adams
@ 2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
2025-01-02 0:31 ` Joel Reicher
` (2 more replies)
0 siblings, 3 replies; 17+ messages in thread
From: Heime via Users list for the GNU Emacs text editor @ 2025-01-01 23:14 UTC (permalink / raw)
To: Drew Adams; +Cc: Heime via Users list for the GNU Emacs text editor
Sent with Proton Mail secure email.
On Wednesday, January 1st, 2025 at 10:30 AM, Drew Adams <drew.adams@oracle.com> wrote:
> > > > How can I exit when encountering 'safg or 'nogo
> > >
> > > As usual, wrap with a `catch', and` throw' to it whenever you like.
> >
> > Does the catch statement belong before mapc?
>
>
> Yes - "wrap".
>
>
> (defun amazones-falkotr-kntlr (actm-seqr)
> (catch 'RTFM
> (mapc (lambda (...)
> <Do whatever, and `throw' any value,
> from anywhere here, to 'RTFM>)
>
> actm-seqr))))
>
> 1. `C-h f catch' 2. https://www.gnu.org/software/emacs/manual/html_node/elisp/Catch-and-Throw.html Learning to use` catch' and `throw' with code such as iteration is basic. And catch/throw is a building block for implementing other iteration etc. constructs. The Elisp code that Emacs provides so freely to you is a great place to learn about Elisp. Grep for` catch' there...
>
> Here's a simple example:
>
> (defmacro while-no-input (&rest body)
> "..."
> (declare (debug t) (indent 0))
> (let ((catch-sym (make-symbol "input")))
> `(with-local-quit
> (catch ',catch-sym
> (let ((throw-on-input ',catch-sym))
> (or (input-pending-p)
> (progn ,@body)))))))
>
> Here's another:
>
> (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
> "..."
> (catch 'found
> (dolist (window (window-list-1
> (next-window nil minibuf all-frames)
> minibuf all-frames))
> (when (funcall predicate window)
> (throw 'found window)))
> default))
How can one get the result of the throw? With a let?
> The Elisp manual makes great bedtime reading.
> Time better spent than asking help-gnu-emacs,
> perhaps.
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
@ 2025-01-02 0:31 ` Joel Reicher
2025-01-02 10:33 ` Jean Louis
2025-01-02 0:33 ` Drew Adams
2025-01-02 2:21 ` Eduardo Ochs
2 siblings, 1 reply; 17+ messages in thread
From: Joel Reicher @ 2025-01-02 0:31 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor; +Cc: Drew Adams, Heime
Heime via Users list for the GNU Emacs text editor
<help-gnu-emacs@gnu.org> writes:
> How can one get the result of the throw? With a let?
Why are you asking on this list when it should be possible to get
this information from (info "(elisp) Catch and Throw")?
Regards,
- Joel
^ permalink raw reply [flat|nested] 17+ messages in thread
* RE: [External] : Exiting from mapc
2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
2025-01-02 0:31 ` Joel Reicher
@ 2025-01-02 0:33 ` Drew Adams
2025-01-02 2:21 ` Eduardo Ochs
2 siblings, 0 replies; 17+ messages in thread
From: Drew Adams @ 2025-01-02 0:33 UTC (permalink / raw)
To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor
> How can one get the result of the throw? With a let?
>
> > The Elisp manual makes great bedtime reading.
> > Time better spent than asking help-gnu-emacs,
> > perhaps.
Whatever you might mean by "get the result of the
throw", the answer's quoted just below your question.
You can't hope to progress if you don't read the
help that so many people have provided, in the
form of `C-h f' and the Elisp manual. I led you
right to the water, but I can't make you drink,
no matter how thirsty you might be.
Anyway...
throw sends its result to catch, which returns
it as the value of the catch call. Or as `C-h f'
tells you:
"Throw to the catch for TAG and return VALUE from it."
^^^^^^^^^^^^^^^^^^^^
"it" = "the catch".
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
2025-01-02 0:31 ` Joel Reicher
2025-01-02 0:33 ` Drew Adams
@ 2025-01-02 2:21 ` Eduardo Ochs
2 siblings, 0 replies; 17+ messages in thread
From: Eduardo Ochs @ 2025-01-02 2:21 UTC (permalink / raw)
To: Heime; +Cc: Drew Adams, Heime via Users list for the GNU Emacs text editor
On Wed, 1 Jan 2025 at 20:15, Heime via Users list for the GNU Emacs
text editor <help-gnu-emacs@gnu.org> wrote:
>
> How can one get the result of the throw? With a let?
;; See: (info "(elisp)Catch and Throw")
(catch 'a (throw 'a 'b))
Cheers,
Eduardo
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-02 0:31 ` Joel Reicher
@ 2025-01-02 10:33 ` Jean Louis
2025-01-03 0:23 ` Joel Reicher
0 siblings, 1 reply; 17+ messages in thread
From: Jean Louis @ 2025-01-02 10:33 UTC (permalink / raw)
To: Joel Reicher
Cc: Heime via Users list for the GNU Emacs text editor, Drew Adams,
Heime
* Joel Reicher <joel.reicher@gmail.com> [2025-01-02 03:33]:
> Heime via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
> writes:
>
> > How can one get the result of the throw? With a let?
>
> Why are you asking on this list when it should be possible to get this
> information from (info "(elisp) Catch and Throw")?
Sometimes it is helpful to provide info, and too often people have
misunderstanding and need explanation. Emacs is full of terms,
concepts, definitions which cause misunderstandings. If that would not
be so, I would be very happy, though it is.
You must know that there are different types of readers, different
audience. Emacs Lisp manual is written in technical manner, it wasn't
written in manner of a tutorial.
Imagine people in need of tutorial reading just this sentence:
11.7.1 Explicit Nonlocal Exits: ‘catch’ and ‘throw’
Even if not a beginner, experienced programmer could mix those terms
with something else.
Unspoken of the rest of the article:
"Most control constructs affect only the flow of control within the
construct itself. " -- what level of programming person must already
have to undersand only this first sentence? Reader must already have
basic programming knowledge, control structure understanding, scope
awareness, etc.
If person is not on the level to easily understand just the first
sentence, mind gets blocked, misunderstood words create blanks in the
mind, and people stop using it, and it is good thing that Heime or
other people do not give up, but ask for answers here.
--
Jean Louis
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: Exiting from mapc
2024-12-31 19:35 Exiting from mapc Heime via Users list for the GNU Emacs text editor
2024-12-31 20:13 ` [External] : " Drew Adams
@ 2025-01-02 10:53 ` Jean Louis
1 sibling, 0 replies; 17+ messages in thread
From: Jean Louis @ 2025-01-02 10:53 UTC (permalink / raw)
To: Heime; +Cc: Heime via Users list for the GNU Emacs text editor
* Heime via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2024-12-31 22:37]:
>
> How can I exit when encountering 'safg or 'nogo
>
> (defun amazones-falkotr-kntlr (actm-seqr)
>
> (mapc
>
> (lambda (actm)
> ;; Evaluate conditions and executes corresponding actions
> ;; based on matching clauses.
>
> (cond
>
> ;; CASE
> ((eq actm 'safg)
> (message "SAFG amazones-falkotr-kntlr"))
To answer the message "How can I exit when encountering 'safg or
'nogo', yes, you can exit.
Here is what happens if there is no number 1 in the list here below:
(let ((list (catch 'no-go
(mapc (lambda (item)
(if (= item 1)
(throw 'no-go nil)
(message "Processing item: %S" item)))
'(2 3 4 5 6)))))
list) ➜ (2 3 4 5 6)
But if there is number one, `throw' exits:
(let ((list (catch 'no-go
(mapc (lambda (item)
(if (= item 1)
(throw 'no-go nil)
(message "Processing item: %S" item)))
'(2 1 3 4 5 6)))))
list)
Processing item: 2
and finished there!
So yes, you can exit from mapc if you enclose it with `catch' this way.
--
Jean Louis
^ permalink raw reply [flat|nested] 17+ messages in thread
* Re: [External] : Exiting from mapc
2025-01-02 10:33 ` Jean Louis
@ 2025-01-03 0:23 ` Joel Reicher
0 siblings, 0 replies; 17+ messages in thread
From: Joel Reicher @ 2025-01-03 0:23 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor; +Cc: Drew Adams, Heime
Jean Louis <bugs@gnu.support> writes:
> * Joel Reicher <joel.reicher@gmail.com> [2025-01-02 03:33]:
>> Heime via Users list for the GNU Emacs text editor
>> <help-gnu-emacs@gnu.org> writes:
>>
>>> How can one get the result of the throw? With a let?
>>
>> Why are you asking on this list when it should be possible to
>> get this information from (info "(elisp) Catch and Throw")?
>
> Sometimes it is helpful to provide info, and too often people
> have misunderstanding and need explanation. Emacs is full of
> terms, concepts, definitions which cause misunderstandings. If
> that would not be so, I would be very happy, though it is.
>
> You must know that there are different types of readers,
> different audience. Emacs Lisp manual is written in technical
> manner, it wasn't written in manner of a tutorial.
If the problem is in understanding the manual, then my very strong
opinion is that the question should be asked in terms of the
manual.
This does three things:
1) Demonstrates good faith on the part of the enquirer that they
have attempted to read the manual
2) Helps phrase the question in common terms with a minimum of
unstated assumptions
3) Potentially leads to improvements in the manual
Regards,
- Joel
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2025-01-03 0:23 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-31 19:35 Exiting from mapc Heime via Users list for the GNU Emacs text editor
2024-12-31 20:13 ` [External] : " Drew Adams
2024-12-31 20:20 ` Heime via Users list for the GNU Emacs text editor
2024-12-31 22:30 ` Drew Adams
2025-01-01 23:14 ` Heime via Users list for the GNU Emacs text editor
2025-01-02 0:31 ` Joel Reicher
2025-01-02 10:33 ` Jean Louis
2025-01-03 0:23 ` Joel Reicher
2025-01-02 0:33 ` Drew Adams
2025-01-02 2:21 ` Eduardo Ochs
2025-01-01 1:53 ` Björn Bidar
[not found] ` <87wmffwb8w.fsf@>
2025-01-01 2:02 ` Drew Adams
2025-01-01 19:40 ` Björn Bidar
[not found] ` <874j2iwcff.fsf@>
2025-01-01 20:00 ` Drew Adams
2025-01-01 21:09 ` Björn Bidar
[not found] ` <87a5cautqb.fsf@>
2025-01-01 21:59 ` Drew Adams
2025-01-02 10:53 ` Jean Louis
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).