all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: replace element in list
Date: Wed, 21 Nov 2018 22:04:35 -0800	[thread overview]
Message-ID: <87va4py7f0.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 45a3b5b6-2985-4eab-bb2c-2ad9b4459018@default

Drew Adams <drew.adams@oracle.com> writes:

>> > (defun my-list-replace (obj orig new)
>> >     "Replaces an element in a list with something else"
>> >     (let* ((pos (cl-position orig obj :test 'equal))
>> >            (pos (if pos pos 0))
>> >            (objlen (length obj))
>> >            (head (butlast obj (- objlen pos)))
>> >            (trail (nthcdr (+ 1 pos) obj)))
>> >       (append head (append new trail))))
>> 
>> (let ((orig '(("a" . "b") ("c" "d")))
>>       (obj  '("c" "d"))
>>       (new '(":)")))
>>   (setf (nth (cl-position obj orig :test #'equal) orig) new)
>>   orig)
>
> (defun toto (xs old new)
>   (let ((ms  (member old xs)))
>     (unless ms (error "%S is not in %S" old xs))
>     (setcar ms new)
>     xs))

Old school, and proper :)




  reply	other threads:[~2018-11-22  6:04 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 [this message]
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
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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87va4py7f0.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.