* apply function
@ 2023-08-07 5:45 Heime
2023-08-07 6:12 ` Karan Ahlawat
0 siblings, 1 reply; 4+ messages in thread
From: Heime @ 2023-08-07 5:45 UTC (permalink / raw)
To: Heime via Users list for the GNU Emacs text editor
I have seem code that calls a function in the following way
(apply 'nemboss-estring (pp-to-string object) bfname))
than the usual
(emboss-estring (pp-to-string object) bfname)
What might be the reasons for doing so ?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: apply function
2023-08-07 5:45 apply function Heime
@ 2023-08-07 6:12 ` Karan Ahlawat
2023-08-07 6:42 ` Heime
2023-08-07 18:44 ` Emanuel Berg
0 siblings, 2 replies; 4+ messages in thread
From: Karan Ahlawat @ 2023-08-07 6:12 UTC (permalink / raw)
To: help-gnu-emacs
On 07/08/23 11:15, Heime wrote:
> I have seem code that calls a function in the following way
>
> (apply 'nemboss-estring (pp-to-string object) bfname))
>
> than the usual
>
> (emboss-estring (pp-to-string object) bfname)
>
> What might be the reasons for doing so ?
The most common use case I've seen for this is that apply can take in a
list as the arguments to the function, where a list would not work, and
then spread the elements of the list as individual arguments. So
(+ (list 1 2 3))
doesn't work, but doing
(apply #'+ (list 1 2 3)) ; outputs 6
does work, since it essentially reduces it to (+ 1 2 3)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: apply function
2023-08-07 6:12 ` Karan Ahlawat
@ 2023-08-07 6:42 ` Heime
2023-08-07 18:44 ` Emanuel Berg
1 sibling, 0 replies; 4+ messages in thread
From: Heime @ 2023-08-07 6:42 UTC (permalink / raw)
To: Karan Ahlawat; +Cc: help-gnu-emacs
Sent with Proton Mail secure email.
------- Original Message -------
On Monday, August 7th, 2023 at 6:12 PM, Karan Ahlawat <ahlawatkaran12@gmail.com> wrote:
> On 07/08/23 11:15, Heime wrote:
>
> > I have seem code that calls a function in the following way
> >
> > (apply 'nemboss-estring (pp-to-string object) bfname))
> >
> > than the usual
> >
> > (emboss-estring (pp-to-string object) bfname)
> >
> > What might be the reasons for doing so ?
>
>
> The most common use case I've seen for this is that apply can take in a
> list as the arguments to the function, where a list would not work, and
> then spread the elements of the list as individual arguments. So
>
> (+ (list 1 2 3))
>
> doesn't work, but doing
>
> (apply #'+ (list 1 2 3)) ; outputs 6
>
> does work, since it essentially reduces it to (+ 1 2 3)
I want to print the output from a macro by passing its output to a function.
Would use of apply be more appropriate ?
(show-mcode '(thismacro (* 3 5) (* 5 7)))
Or should I use something else ? Should the expansion of a macro be turned
to a list before passing it to a function ?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: apply function
2023-08-07 6:12 ` Karan Ahlawat
2023-08-07 6:42 ` Heime
@ 2023-08-07 18:44 ` Emanuel Berg
1 sibling, 0 replies; 4+ messages in thread
From: Emanuel Berg @ 2023-08-07 18:44 UTC (permalink / raw)
To: help-gnu-emacs
Karan Ahlawat wrote:
>> What might be the reasons for doing so ?
>
> The most common use case I've seen for this is that apply
> can take in a list as the arguments to the function, where
> a list would not work, and then spread the elements of the
> list as individual arguments. So
>
> (+ (list 1 2 3))
>
> doesn't work, but doing
>
> (apply #'+ (list 1 2 3)) ; outputs 6
>
> does work
The other use case is when one has the function stored in
a variable or provided as an argument, i.e. it is not known
from the static code perspective what function will be
executed. Observe:
(require 'cl-lib)
(defun test-final-line-f (fun pos-list)
(cl-loop for p in pos-list do
(goto-char p)
(apply fun nil) ))
Here is a better example, maybe, where the function is either
`re-search-backward' or `re-search-forward' depending on the
argument REV (as in "reverse").
;;; -*- lexical-binding: t -*-
;;
;; this file:
;; https://dataswamp.org/~incal/emacs-init/regexp.el
(require 'cl-lib)
(require 'dwim)
(defun replace-regexp-1 (re rep &optional beg end rev)
(interactive
`(,(read-string "regexp: ")
,(read-string "replace: ")
,@(use-region t)
,current-prefix-arg))
(or beg (setq beg (point-min)))
(or end (setq end (point-max)))
(pcase-let ((`(,start ,sfun ,stop)
(if rev
(list end #'re-search-backward beg)
(list beg #'re-search-forward end) )))
(save-excursion
(goto-char start)
(let ((c 0))
(while (apply sfun re stop '(t))
(cl-incf c)
(replace-match rep) )
(message "matches replaced: %d" c) ))))
(defalias 'rr #'replace-regexp-1)
(provide 'regexp)
Source:
https://dataswamp.org/~incal/emacs-init/dwim.el
https://dataswamp.org/~incal/emacs-init/measure.el
https://dataswamp.org/~incal/emacs-init/regexp.el
--
underground experts united
https://dataswamp.org/~incal
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-08-07 18:44 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-07 5:45 apply function Heime
2023-08-07 6:12 ` Karan Ahlawat
2023-08-07 6:42 ` Heime
2023-08-07 18:44 ` Emanuel Berg
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).