From: Robert Munyer <robertm@not-for-mail.invalid>
To: help-gnu-emacs@gnu.org
Subject: Re: replace element in list
Date: Fri, 23 Nov 2018 12:05:16 +0000 (UTC) [thread overview]
Message-ID: <pt8qcm$1e0u$1@gioia.aioe.org> (raw)
In-Reply-To: mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org
> edgar wrote:
>> (defun my-list-replace (obj orig new)
>> "Replaces an element in a list with something else"
[...]
>> (my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi")))
>> ;; (("a" . "b") ("hi"))
If nothing in the list matches your "orig" item, your function will
replace the _first_ item. Did you intend that?
(my-list-replace '(("a" . "b") ("c" "d")) '("e" "f") (list '("hi")))
;; (("hi") ("c" "d"))
Stefan Monnier wrote:
> - if you do it by modifying the list in place, it means you're using
> nasty side-effects, which are better avoided when possible
> (especially with lists).
Good point.
> - if you want to do it without side-effects, your operation will
> inevitably be algorithmically inefficient because a list is not
> designed for that.
If he doesn't want to run it very frequently nor on very long lists,
moderate inefficiency is OK.
Edgar, here is one that avoids the nasty side-effects that Stefan
mentioned. It isn't especially efficient, but it is simple and clear.
Warning: it behaves the same as your original version _only_ if there
is exactly one matching item.
(defun my-list-replace-2 (l old-item new-items)
(apply 'append
(mapcar (lambda (x)
(cond ((equal x old-item) new-items)
(t (list x))))
l)))
--
Robert Munyer
E-mail: (reverse (append '(com dot munyer at) (list (* 91837 99713))))
next prev parent reply other threads:[~2018-11-23 12:05 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-11-22 4:42 replace element in list edgar
2018-11-22 5:18 ` Eric Abrahamsen
2018-11-22 5:39 ` Drew Adams
2018-11-22 6:04 ` Eric Abrahamsen
2018-11-22 13:19 ` Stefan Monnier
[not found] ` <mailman.4442.1542892802.1284.help-gnu-emacs@gnu.org>
2018-11-23 12:05 ` Robert Munyer [this message]
2018-11-24 3:01 ` edgar
2018-11-24 5:27 ` Drew Adams
2018-11-24 7:22 ` edgar
-- strict thread matches above, loose matches on Subject: below --
2019-09-02 9:44 Replace " Andreas Röhler
2019-09-02 10:37 ` tomas
2019-09-02 10:41 ` tomas
2019-09-02 10:46 ` tomas
2019-09-02 11:03 ` Michael Heerdegen
2019-09-02 11:05 ` tomas
2019-09-02 13:38 ` Andreas Röhler
2019-09-02 13:29 ` Stefan Monnier
2019-09-02 13:43 ` tomas
2019-09-02 14:00 ` Andreas Röhler
2019-09-02 14:57 ` Stefan Monnier
2019-09-02 20:29 ` Andreas Röhler
2019-09-02 21:32 ` Stefan Monnier
2019-09-03 5:37 ` Andreas Röhler
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='pt8qcm$1e0u$1@gioia.aioe.org' \
--to=robertm@not-for-mail.invalid \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).