* for a given emacs session: insert a subject with an increasing counter @ 2021-08-30 16:01 Uwe Brauer 2021-08-30 16:10 ` Eric S Fraga 0 siblings, 1 reply; 26+ messages in thread From: Uwe Brauer @ 2021-08-30 16:01 UTC (permalink / raw) To: ding Hi Is the following possible for a given emacs session a internal counter is created that is deleted after closing emacs and which would allow to insert to each message in the subject line a counter of the form Subject: message-of <2021-08-30 lun>: 1 Subject: message-of <2021-08-30 lun>: 2 Of course such a function could be used only for certain emails (using say some bbdb functionality). This for example (defvar my-subject-counter 0) (defun my-insert-add-counter () (interactive) (insert (int-to-string (+ my-subject-counter 1)))) Does not work since the counter is not saved when increased. Does anybody know about such a functionality? Regards Uwe Brauer ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-30 16:01 for a given emacs session: insert a subject with an increasing counter Uwe Brauer @ 2021-08-30 16:10 ` Eric S Fraga 2021-08-30 18:46 ` Eric Abrahamsen 2021-08-31 6:50 ` Uwe Brauer 0 siblings, 2 replies; 26+ messages in thread From: Eric S Fraga @ 2021-08-30 16:10 UTC (permalink / raw) To: ding On Monday, 30 Aug 2021 at 18:01, Uwe Brauer wrote: > Does not work since the counter is not saved when increased. Why not save it then? I.e. something like this: (defun my-insert-add-counter () (interactive) (setq my-subject-counter (+ my-subject-counter 1)) (insert (int-to-string my-subject-counter))) [untested] HTH, eric PS by the way, there is an elisp function called 1+ which you might like to use ;-)) -- Eric S Fraga via Emacs 28.0.50 & org 9.4.6 on Debian 11.0 ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-30 16:10 ` Eric S Fraga @ 2021-08-30 18:46 ` Eric Abrahamsen 2021-08-31 6:50 ` Uwe Brauer 1 sibling, 0 replies; 26+ messages in thread From: Eric Abrahamsen @ 2021-08-30 18:46 UTC (permalink / raw) To: ding Eric S Fraga <e.fraga@ucl.ac.uk> writes: > On Monday, 30 Aug 2021 at 18:01, Uwe Brauer wrote: >> Does not work since the counter is not saved when increased. > > Why not save it then? I.e. something like this: > > (defun my-insert-add-counter () > (interactive) > (setq my-subject-counter (+ my-subject-counter 1)) > (insert (int-to-string my-subject-counter))) > > [untested] > > HTH, > eric > > PS by the way, there is an elisp function called 1+ which you might like > to use ;-)) And if you really want to write the absolute minimum code, you can use `cl-incf'! ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-30 16:10 ` Eric S Fraga 2021-08-30 18:46 ` Eric Abrahamsen @ 2021-08-31 6:50 ` Uwe Brauer 2021-08-31 6:58 ` Adam Sjøgren 2021-08-31 7:44 ` Emanuel Berg 1 sibling, 2 replies; 26+ messages in thread From: Uwe Brauer @ 2021-08-31 6:50 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 1097 bytes --] >>> "ESF" == Eric S Fraga <e.fraga@ucl.ac.uk> writes: > On Monday, 30 Aug 2021 at 18:01, Uwe Brauer wrote: >> Does not work since the counter is not saved when increased. > Why not save it then? I.e. something like this: > (defun my-insert-add-counter () > (interactive) > (setq my-subject-counter (+ my-subject-counter 1)) > (insert (int-to-string my-subject-counter))) Thanks very much, recursion, sigh, I should have thought about it. So I cooked up that (defun my-reset-subject-counter () "Reset the counter to ZERO!" (interactive) (setq my-subject-counter 0) (message "Now my-subject counter has been reset to ZERO!")) (defun my-new-insert-subject-counter () "Insert a string of the form [`counter/Total'] " (interactive) (save-excursion (setq my-subject-counter (+ my-subject-counter 1)) (let ((total (read-string "Total number: " ))) (message-carefully-insert-headers (list (cons 'Subject (concat "[" (int-to-string my-subject-counter) "/" total "]"))))))) Which is very useful for my workflow, thanks again. Uwe [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 6:50 ` Uwe Brauer @ 2021-08-31 6:58 ` Adam Sjøgren 2021-08-31 15:42 ` Uwe Brauer 2021-08-31 7:44 ` Emanuel Berg 1 sibling, 1 reply; 26+ messages in thread From: Adam Sjøgren @ 2021-08-31 6:58 UTC (permalink / raw) To: ding Uwe writes: >> (defun my-insert-add-counter () >> (interactive) >> (setq my-subject-counter (+ my-subject-counter 1)) >> (insert (int-to-string my-subject-counter))) > > Thanks very much, recursion, sigh, I should have thought about it. Where is the recursion? It's a global variable, right? Best regards, Adam -- "Tears in waves Adam Sjøgren Lights on fire" asjo@koldfront.dk ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 6:58 ` Adam Sjøgren @ 2021-08-31 15:42 ` Uwe Brauer 2021-08-31 15:54 ` Adam Sjøgren 2021-09-01 3:55 ` for a given emacs session: insert a subject with an increasing counter Bodertz 0 siblings, 2 replies; 26+ messages in thread From: Uwe Brauer @ 2021-08-31 15:42 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 606 bytes --] >>> "AS" == Adam Sjøgren <asjo@koldfront.dk> writes: > Uwe writes: >>> (defun my-insert-add-counter () >>> (interactive) >>> (setq my-subject-counter (+ my-subject-counter 1)) >>> (insert (int-to-string my-subject-counter))) >> >> Thanks very much, recursion, sigh, I should have thought about it. > Where is the recursion? It's a global variable, right? Well the defined quantity my-subject-counter contains in its definition my-subject-counter (setq my-subject-counter 1) Is not recursive, but (setq my-subject-counter (+ my-subject-counter 1)) Looks a bit recursive to me. [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 15:42 ` Uwe Brauer @ 2021-08-31 15:54 ` Adam Sjøgren 2021-09-02 8:15 ` Uwe Brauer 2021-09-04 15:30 ` [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) Uwe Brauer 2021-09-01 3:55 ` for a given emacs session: insert a subject with an increasing counter Bodertz 1 sibling, 2 replies; 26+ messages in thread From: Adam Sjøgren @ 2021-08-31 15:54 UTC (permalink / raw) To: ding Uwe writes: > Well the defined quantity my-subject-counter contains in its > definition my-subject-counter Ah, yes, but setq doesn't define a quantity - it is much less abstract: it assigns a value to a variable. > (setq my-subject-counter 1) > > Is not recursive, but > (setq my-subject-counter (+ my-subject-counter 1)) > > Looks a bit recursive to me. Thanks for explaining, I was genuinely puzzled; you are using a different definition of recursion than most people do, when it comes to programming - where it is usually, implicitly about functions. Unless '+' has started to call itself ;-) Best regards, Adam -- "I went for the fireengines Adam Sjøgren But they were all upside down" asjo@koldfront.dk ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 15:54 ` Adam Sjøgren @ 2021-09-02 8:15 ` Uwe Brauer 2021-09-04 15:30 ` [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) Uwe Brauer 1 sibling, 0 replies; 26+ messages in thread From: Uwe Brauer @ 2021-09-02 8:15 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 811 bytes --] >>> "AS" == Adam Sjøgren <asjo@koldfront.dk> writes: > Uwe writes: >> Well the defined quantity my-subject-counter contains in its >> definition my-subject-counter >> (setq my-subject-counter 1) >> >> Is not recursive, but >> (setq my-subject-counter (+ my-subject-counter 1)) > Thanks for explaining, I was genuinely puzzled; you are using a > different definition of recursion than most people do, when it comes to > programming - where it is usually, implicitly about functions. Well, it is a bit like the definition of GNU GNU = GNU is Not Unix So the defined object GNU is part the object that defines it. That qualifies as recursion, for me, as does GNU = GNU is Not Unix (setq my-subject-counter (+ my-subject-counter 1)) ;-) [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) 2021-08-31 15:54 ` Adam Sjøgren 2021-09-02 8:15 ` Uwe Brauer @ 2021-09-04 15:30 ` Uwe Brauer 2021-09-07 20:04 ` Emanuel Berg 1 sibling, 1 reply; 26+ messages in thread From: Uwe Brauer @ 2021-09-04 15:30 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 1357 bytes --] > Uwe writes: > Ah, yes, but setq doesn't define a quantity - it is much less abstract: > it assigns a value to a variable. > Thanks for explaining, I was genuinely puzzled; you are using a > different definition of recursion than most people do, when it comes to > programming - where it is usually, implicitly about functions. I realised that there was a misunderstanding. I thought you and others suggest just to use (defun my-new-insert-subject-counter-old () "Insert a string of the form [`counter/Total'] " (interactive) (save-excursion (setq my-subject-counter (+ my-subject-counter 1)) (let ((total (read-string "Total number: " ))) (message-carefully-insert-headers (list (cons 'Subject (format "[%s/%s]" my-subject-counter total))))))) Without (defvar my-subject-counter 0) (or something like this.) As a matter of fact I deleted the defvar definition from my code and it still worked[1] but which puzzled me. So I thought (setq my-subject-counter (+ my-subject-counter 1)) defines and sets the variable in a recursive way, but it does not. You need a defvar (or a surrounding let). I only discovered my error misunderstanding when I restarted emacs and the code did not work anymore Sorry for the misunderstanding. Footnotes: [1] because it was already defined [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) 2021-09-04 15:30 ` [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) Uwe Brauer @ 2021-09-07 20:04 ` Emanuel Berg 0 siblings, 0 replies; 26+ messages in thread From: Emanuel Berg @ 2021-09-07 20:04 UTC (permalink / raw) To: ding Uwe Brauer wrote: > I realised that there was a misunderstanding. I thought you > and others suggest just to use I didn't know of the closure thing, it is actually an excellent for this task in particular! Only I still don't know what total is supposed to express, nevermind, take a look: (require 'cl-lib) (require 'message) (let ((subject-counter 0)) (defun insert-subject-counter (total) (interactive "nTotal: ") (save-mark-and-excursion (message-carefully-insert-headers (list (cons 'Subject (format "[%s/%s]" (cl-incf subject-counter) total) )))))) (defalias 'iscr #'insert-subject-counter) > Without (defvar my-subject-counter 0) (or something > like this.) > > As a matter of fact I deleted the defvar definition from my > code and it still worked[1] but which puzzled me. This is a tricky case. No, you don't need the `defvar' to make globals, you can do it like this (defun make-global () (setq global 5) ) ;; (make-global) ;; global ; 5 I don't recommend it but it is possible. So that isn't where the shoe hurts but here > So I thought > (setq my-subject-counter (+ my-subject-counter 1)) > defines and sets the variable in a recursive way, but it > does not. You need a defvar (or a surrounding let). `setq' doesn't get to do that as (+ my-subject-counter 1) uses an "void-value variable symbol" in my-subject-counter, to paraphrase the error message ... (I think you realized that but I say/write it anyway.) Also, check out the `1+' and, which is even better here, `cl-incf' functions! -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 15:42 ` Uwe Brauer 2021-08-31 15:54 ` Adam Sjøgren @ 2021-09-01 3:55 ` Bodertz 2021-09-04 15:25 ` Uwe Brauer 1 sibling, 1 reply; 26+ messages in thread From: Bodertz @ 2021-09-01 3:55 UTC (permalink / raw) To: ding Just for your information, if you don't want a global variable, you can use let: (let ((counter 0)) (defun my-insert-add-counter () (cl-incf counter) (insert (int-to-string counter)))) ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-01 3:55 ` for a given emacs session: insert a subject with an increasing counter Bodertz @ 2021-09-04 15:25 ` Uwe Brauer 2021-09-07 17:12 ` Emanuel Berg 0 siblings, 1 reply; 26+ messages in thread From: Uwe Brauer @ 2021-09-04 15:25 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 372 bytes --] >>> "B" == Bodertz <bodertz@gmail.com> writes: > Just for your information, if you don't want a global variable, you can > use let: > (let ((counter 0)) > (defun my-insert-add-counter () > (cl-incf counter) > (insert (int-to-string counter)))) Right, but it feels a bit counterintuitive: to define a function in a let. Anyhow a question of taste I presume. [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-04 15:25 ` Uwe Brauer @ 2021-09-07 17:12 ` Emanuel Berg 2021-09-07 19:02 ` Eric Abrahamsen 0 siblings, 1 reply; 26+ messages in thread From: Emanuel Berg @ 2021-09-07 17:12 UTC (permalink / raw) To: ding Uwe Brauer wrote: >> Just for your information, if you don't want a global >> variable, you can use let: >> >> (let ((counter 0)) >> (defun my-insert-add-counter () >> (cl-incf counter) >> (insert (int-to-string counter)))) > > Right, but it feels a bit counterintuitive: to define > a function in a let. Very counterintuitive to the point I never thought about it and wouldn't no matter how much Lisp I ever get to do... > Anyhow a question of taste I presume. I don't think they are equivalent, but I'm not the right person to explain/provide examples to illustrate ... not in this case at least. -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 17:12 ` Emanuel Berg @ 2021-09-07 19:02 ` Eric Abrahamsen 2021-09-07 19:25 ` Emanuel Berg 0 siblings, 1 reply; 26+ messages in thread From: Eric Abrahamsen @ 2021-09-07 19:02 UTC (permalink / raw) To: ding Emanuel Berg <moasenwood@zoho.eu> writes: > Uwe Brauer wrote: > >>> Just for your information, if you don't want a global >>> variable, you can use let: >>> >>> (let ((counter 0)) >>> (defun my-insert-add-counter () >>> (cl-incf counter) >>> (insert (int-to-string counter)))) >> >> Right, but it feels a bit counterintuitive: to define >> a function in a let. > > Very counterintuitive to the point I never thought about it > and wouldn't no matter how much Lisp I ever get to do... That's called a "closure", because the function "closes over" the let-bound symbol. Once evaluated, _only_ the function body has access to that symbol: it can treat it like a globally-defined variable, but no one else can see it. When lexical-binding is non-nil, you're always making closures: (setq lexical-binding t) (lambda (arg) (message "%S" arg)) --> (closure (t) (arg) (message "%S" arg)) The (t) is where the closed-over symbols and their current values would be stored, if there were any. It's sort of like the function's own private let-form. Eric ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 19:02 ` Eric Abrahamsen @ 2021-09-07 19:25 ` Emanuel Berg 2021-09-07 20:09 ` Emanuel Berg 0 siblings, 1 reply; 26+ messages in thread From: Emanuel Berg @ 2021-09-07 19:25 UTC (permalink / raw) To: ding Eric Abrahamsen wrote: >>> Right, but it feels a bit counterintuitive: to define >>> a function in a let. >> >> Very counterintuitive to the point I never thought about it >> and wouldn't no matter how much Lisp I ever get to do... > > That's called a "closure", because the function "closes > over" the let-bound symbol. Once evaluated, _only_ the > function body has access to that symbol: it can treat it > like a globally-defined variable, but no one else can see > it. When lexical-binding is non-nil, you're always making > closures: > > (setq lexical-binding t) > (lambda (arg) (message "%S" arg)) --> > (closure (t) (arg) (message "%S" arg)) > > The (t) is where the closed-over symbols and their current > values would be stored, if there were any. It's sort of like > the function's own private let-form. But ... isn't that what you get with lexical/static `let' _in_ the function? -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 19:25 ` Emanuel Berg @ 2021-09-07 20:09 ` Emanuel Berg 2021-09-07 20:35 ` Emanuel Berg 2021-09-07 20:44 ` Eric Abrahamsen 0 siblings, 2 replies; 26+ messages in thread From: Emanuel Berg @ 2021-09-07 20:09 UTC (permalink / raw) To: ding >> That's called a "closure", because the function "closes >> over" the let-bound symbol. Once evaluated, _only_ the >> function body has access to that symbol: it can treat it >> like a globally-defined variable, but no one else can see >> it. When lexical-binding is non-nil, you're always making >> closures: >> >> (setq lexical-binding t) >> (lambda (arg) (message "%S" arg)) --> >> (closure (t) (arg) (message "%S" arg)) >> >> The (t) is where the closed-over symbols and their current >> values would be stored, if there were any. It's sort of >> like the function's own private let-form. > > But ... isn't that what you get with lexical/static `let' > _in_ the function? Ah, now I understand what you mean. With `let' inside, the variable will reset, so it cannot count, but with `let' outside (the closure), as you say "it can treat it like a globally-defined variable". "globally-defined", very good! Oh, no! I didn't know of this (never seen it). I have used global variables to hold the "state" (be a memory between function calls) but I've also done more involved solutions like properties and even self-modifying code :O And all the while, it was this easy ... -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 20:09 ` Emanuel Berg @ 2021-09-07 20:35 ` Emanuel Berg 2021-09-07 20:44 ` Eric Abrahamsen 1 sibling, 0 replies; 26+ messages in thread From: Emanuel Berg @ 2021-09-07 20:35 UTC (permalink / raw) To: ding > Ah, now I understand what you mean. With `let' inside, the > variable will reset, so it cannot count, but with `let' > outside (the closure), as you say "it can treat it like > a globally-defined variable". But now, the byte-compiler will complain Warning: the function ‘insert-subject-counter’ is not known to be defined. so now we need not `defvar' but "deffunc" to make it explicitly global/dynamic :P -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 20:09 ` Emanuel Berg 2021-09-07 20:35 ` Emanuel Berg @ 2021-09-07 20:44 ` Eric Abrahamsen 2021-09-21 3:32 ` Emanuel Berg 1 sibling, 1 reply; 26+ messages in thread From: Eric Abrahamsen @ 2021-09-07 20:44 UTC (permalink / raw) To: ding Emanuel Berg <moasenwood@zoho.eu> writes: >>> That's called a "closure", because the function "closes >>> over" the let-bound symbol. Once evaluated, _only_ the >>> function body has access to that symbol: it can treat it >>> like a globally-defined variable, but no one else can see >>> it. When lexical-binding is non-nil, you're always making >>> closures: >>> >>> (setq lexical-binding t) >>> (lambda (arg) (message "%S" arg)) --> >>> (closure (t) (arg) (message "%S" arg)) >>> >>> The (t) is where the closed-over symbols and their current >>> values would be stored, if there were any. It's sort of >>> like the function's own private let-form. >> >> But ... isn't that what you get with lexical/static `let' >> _in_ the function? > > Ah, now I understand what you mean. With `let' inside, the > variable will reset, so it cannot count, but with `let' > outside (the closure), as you say "it can treat it like > a globally-defined variable". That's right. > "globally-defined", very good! > > Oh, no! > > I didn't know of this (never seen it). I have used global > variables to hold the "state" (be a memory between function > calls) but I've also done more involved solutions like > properties and even self-modifying code :O > > And all the while, it was this easy ... TBH, I've never actually used a closure in anger, probably just because it doesn't occur to me. I'll bet if I went back and looked over the code I've written in the past I could find some situations where I could have used them, but... *shrug* ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-07 20:44 ` Eric Abrahamsen @ 2021-09-21 3:32 ` Emanuel Berg 2021-09-21 19:12 ` Bodertz 0 siblings, 1 reply; 26+ messages in thread From: Emanuel Berg @ 2021-09-21 3:32 UTC (permalink / raw) To: ding Eric Abrahamsen wrote: >> Oh, no! >> >> I didn't know of this (never seen it). I have used global >> variables to hold the "state" (be a memory between function >> calls) but I've also done more involved solutions like >> properties and even self-modifying code :O >> >> And all the while, it was this easy ... > > TBH, I've never actually used a closure in anger, probably > just because it doesn't occur to me. I'll bet if I went back > and looked over the code I've written in the past I could > find some situations where I could have used them, but... > *shrug* I think I heard that you shouldn't use, or that you should avoid, global variables the first time when I was 12. I understood it enough to stick by it and I guess my code was good to me 5-6 days out of 7 at least. However it would have been even better if it had been formulized like this ... When the situation is <something>, while a global variable would solve it, <something else> is preferable ... So, doing now what we were unable to do at age 12, we can formulate one such rule: When the situation is like that, that a function needs a state or local memory, which cannot be reset from one function call to the other, use a closure! It is easy to add rules ... When the value has the nature of an user option, a global variable is fine ... But what about when two (or more) functions need access to the same variable, how should that be done? Local functions (`cl-labels'), parameters, evaluation - don't know, is there a short answer? - or can one have several functions in a/one closure `let'? Aaanyway, I think the best way to approach this is to grep the source for `defvar' (or baseline `setq'), prune the options, and then ... (Unbelievable that I never read this in a single computer book of all the tons I've read. Unbelievable people who write them. Or?) -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-21 3:32 ` Emanuel Berg @ 2021-09-21 19:12 ` Bodertz 2021-09-21 21:56 ` Emanuel Berg 0 siblings, 1 reply; 26+ messages in thread From: Bodertz @ 2021-09-21 19:12 UTC (permalink / raw) To: ding Emanuel Berg <moasenwood@zoho.eu> writes: > or can one have several functions in a/one closure `let'? You can. A simple example: (let ((counter 0)) (defun decrement-counter () (cl-decf counter)) (defun increment-counter () (cl-incf counter)) (defun reset-counter () (setq counter 0))) > (Unbelievable that I never read this in a single computer book > of all the tons I've read. Unbelievable people who write > them. Or?) There is a book called 'Let Over Lambda' which is named after this concept: the 'let' is over the 'lambda' (or 'defun', in this case). I haven't read it, but it's how I heard of this idea. ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-09-21 19:12 ` Bodertz @ 2021-09-21 21:56 ` Emanuel Berg 0 siblings, 0 replies; 26+ messages in thread From: Emanuel Berg @ 2021-09-21 21:56 UTC (permalink / raw) To: ding Bodertz wrote: >> or can one have several functions in a/one closure `let'? > > You can. A simple example: > > (let ((counter 0)) > (defun decrement-counter () > (cl-decf counter)) > > (defun increment-counter () > (cl-incf counter)) > > (defun reset-counter () > (setq counter 0))) Sweet! That example may be simple but it is exemplary nonetheless! Did you ever consider writing a book on computing? :) >> (Unbelievable that I never read this in a single computer >> book of all the tons I've read. Unbelievable people who >> write them. Or?) > > There is a book called 'Let Over Lambda' which is named > after this concept: the 'let' is over the 'lambda' (or > 'defun', in this case). I haven't read it, but it's how > I heard of this idea. Interesting title, I'd love to read it ... I did Lisp at the university ("Advanced Functional Programming", 5 ECTS in 2013-02-01) - that course also included Erlang and Haskell - Erlang because of modularity/concurrency I guess, and Haskell because of rigidity/modernity - again I guess. I don't consider Lisp functional, or rather, it is functional and everything else - or it can be, at least. It is multi-paradigm but even that doesn't feel right because when Lisp came (1958, USA; CL 1984, Elisp 1985) at that time there were no programming paradigms. Which BTW IMO are, at the best, thought models to enhance our understanding, not a bunch of rules to follow. Anyway, at that course there were some material, but I lost it somewhere between the Second and Third Impact. Other than that, I've read the Emacs 18 manual and three books on Lisp, namely @book{land-of-lisp, author = {Conrad Barski}, isbn = 1593272812, publisher = {No Starch}, title = {Land of Lisp}, year = {2010} } @book{lispcraft, author = {Robert Wilensky}, isbn = 0393954420, publisher = {Norton}, title = {LISPcraft}, year = {1984} } (the third one doesn't seem to be in the register, anyway it was in Swedish so of less use even to "us" these days) Conrad Barski's book is very good! (Searched, but didn't find his e-mail; feel free to CC this to him.) https://dataswamp.org/~incal/books/books.bib -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 6:50 ` Uwe Brauer 2021-08-31 6:58 ` Adam Sjøgren @ 2021-08-31 7:44 ` Emanuel Berg 2021-08-31 7:56 ` Emanuel Berg 2021-08-31 16:01 ` Uwe Brauer 1 sibling, 2 replies; 26+ messages in thread From: Emanuel Berg @ 2021-08-31 7:44 UTC (permalink / raw) To: ding Uwe Brauer wrote: > Thanks very much, recursion, sigh, I should have thought > about it. Indeed not recursion, a global/special/dynamic variable, so (defvar my-subject-counter) (setq my-subject-counter 0) > (setq my-subject-counter 0) > (message "Now my-subject counter has been reset to ZERO!")) (message "... %s" my-subject-counter) > (setq my-subject-counter (+ my-subject-counter 1)) (require 'cl-lib) (defun ... (cl-incf my-subject-counter) ... ) > (read-string "Total number: " ) Hm ... don't you want to automate that as well? > (concat "[" (int-to-string my-subject-counter) "/" total "]") (format "[%s/%s]" my-subject-counter total) -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 7:44 ` Emanuel Berg @ 2021-08-31 7:56 ` Emanuel Berg 2021-08-31 16:01 ` Uwe Brauer 1 sibling, 0 replies; 26+ messages in thread From: Emanuel Berg @ 2021-08-31 7:56 UTC (permalink / raw) To: ding >> (read-string "Total number: " ) > > Hm ... don't you want to automate that as well? If you don't, (defun my-new-insert-subject-counter (total) (interactive "nTotal number: ") [...] -- underground experts united https://dataswamp.org/~incal ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 7:44 ` Emanuel Berg 2021-08-31 7:56 ` Emanuel Berg @ 2021-08-31 16:01 ` Uwe Brauer 2021-08-31 16:29 ` Eric Abrahamsen 1 sibling, 1 reply; 26+ messages in thread From: Uwe Brauer @ 2021-08-31 16:01 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 766 bytes --] >>> "EB" == Emanuel Berg <moasenwood@zoho.eu> writes: > Uwe Brauer wrote: Thanks for your advice: I have now (defun my-reset-subject-counter () "Reset the counter to ZERO!" (interactive) (setq my-subject-counter 0) (message "Counter value is NOW: %s" my-subject-counter)) (defun my-new-insert-subject-counter (total) "Insert a string of the form [`counter/Total'] " (interactive "nTotal number: ") (save-excursion (setq my-subject-counter (+ my-subject-counter 1)) (message-carefully-insert-headers (list (cons 'Subject (format "[%s/%s]" my-subject-counter total)))))) But this part I don't understand > (require 'cl-lib) > (defun ... > (cl-incf my-subject-counter) ... ) What do you suggest? Uwe [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 16:01 ` Uwe Brauer @ 2021-08-31 16:29 ` Eric Abrahamsen 2021-08-31 19:06 ` Uwe Brauer 0 siblings, 1 reply; 26+ messages in thread From: Eric Abrahamsen @ 2021-08-31 16:29 UTC (permalink / raw) To: ding Uwe Brauer <oub@mat.ucm.es> writes: >>>> "EB" == Emanuel Berg <moasenwood@zoho.eu> writes: > >> Uwe Brauer wrote: > > Thanks for your advice: > > I have now > > (defun my-reset-subject-counter () > "Reset the counter to ZERO!" > (interactive) > (setq my-subject-counter 0) > (message "Counter value is NOW: %s" my-subject-counter)) > > (defun my-new-insert-subject-counter (total) > "Insert a string of the form [`counter/Total'] " > (interactive "nTotal number: ") > (save-excursion > (setq my-subject-counter (+ my-subject-counter 1)) > (message-carefully-insert-headers (list (cons 'Subject (format "[%s/%s]" my-subject-counter total)))))) > > But this part I don't understand > > >> (require 'cl-lib) > >> (defun ... >> (cl-incf my-subject-counter) ... ) The line above "increments" an integer variable "in place", in other words that line is the exact equivalent of: (setq my-subject-counter (+ my-subject-counter 1)) In effect, the `cl-incf' "contains" the `setq' inside it. ^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: for a given emacs session: insert a subject with an increasing counter 2021-08-31 16:29 ` Eric Abrahamsen @ 2021-08-31 19:06 ` Uwe Brauer 0 siblings, 0 replies; 26+ messages in thread From: Uwe Brauer @ 2021-08-31 19:06 UTC (permalink / raw) To: ding [-- Attachment #1: Type: text/plain, Size: 284 bytes --] > Uwe Brauer <oub@mat.ucm.es> writes: > The line above "increments" an integer variable "in place", in other > words that line is the exact equivalent of: > (setq my-subject-counter (+ my-subject-counter 1)) > In effect, the `cl-incf' "contains" the `setq' inside it. Ok, thanks [-- Attachment #2: smime.p7s --] [-- Type: application/pkcs7-signature, Size: 5673 bytes --] ^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2021-09-21 21:56 UTC | newest] Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-08-30 16:01 for a given emacs session: insert a subject with an increasing counter Uwe Brauer 2021-08-30 16:10 ` Eric S Fraga 2021-08-30 18:46 ` Eric Abrahamsen 2021-08-31 6:50 ` Uwe Brauer 2021-08-31 6:58 ` Adam Sjøgren 2021-08-31 15:42 ` Uwe Brauer 2021-08-31 15:54 ` Adam Sjøgren 2021-09-02 8:15 ` Uwe Brauer 2021-09-04 15:30 ` [misunderstanding] (was: for a given emacs session: insert a subject with an increasing counter) Uwe Brauer 2021-09-07 20:04 ` Emanuel Berg 2021-09-01 3:55 ` for a given emacs session: insert a subject with an increasing counter Bodertz 2021-09-04 15:25 ` Uwe Brauer 2021-09-07 17:12 ` Emanuel Berg 2021-09-07 19:02 ` Eric Abrahamsen 2021-09-07 19:25 ` Emanuel Berg 2021-09-07 20:09 ` Emanuel Berg 2021-09-07 20:35 ` Emanuel Berg 2021-09-07 20:44 ` Eric Abrahamsen 2021-09-21 3:32 ` Emanuel Berg 2021-09-21 19:12 ` Bodertz 2021-09-21 21:56 ` Emanuel Berg 2021-08-31 7:44 ` Emanuel Berg 2021-08-31 7:56 ` Emanuel Berg 2021-08-31 16:01 ` Uwe Brauer 2021-08-31 16:29 ` Eric Abrahamsen 2021-08-31 19:06 ` Uwe Brauer
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 NNTP newsgroup(s).