From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: edgar@openmail.cc Newsgroups: gmane.emacs.help Subject: replace element in list Date: Thu, 22 Nov 2018 04:42:06 +0000 Message-ID: <2087821187f90948d25d93c7ea475e66@openmail.cc> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: blaine.gmane.org 1542862081 24765 195.159.176.226 (22 Nov 2018 04:48:01 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 22 Nov 2018 04:48:01 +0000 (UTC) User-Agent: Roundcube Webmail/1.2.4 To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Nov 22 05:47:56 2018 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1gPgu8-0006Fd-U5 for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Nov 2018 05:47:53 +0100 Original-Received: from localhost ([::1]:43988 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPgwF-0007yG-D2 for geh-help-gnu-emacs@m.gmane.org; Wed, 21 Nov 2018 23:50:03 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:34837) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gPgvf-0007xv-8H for help-gnu-emacs@gnu.org; Wed, 21 Nov 2018 23:49:31 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gPgp3-0000Vd-QD for help-gnu-emacs@gnu.org; Wed, 21 Nov 2018 23:42:41 -0500 Original-Received: from onethreetwo.vfemail.net ([199.16.11.132]:58384 helo=vfemail.net) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gPgp3-0000UF-J3 for help-gnu-emacs@gnu.org; Wed, 21 Nov 2018 23:42:37 -0500 Original-Received: (qmail 48322 invoked by uid 89); 22 Nov 2018 04:42:23 -0000 Original-Received: from localhost (HELO freequeue.vfemail.net) (127.0.0.1) by localhost with (DHE-RSA-AES256-SHA encrypted) SMTP; 22 Nov 2018 04:42:23 -0000 Original-Received: (qmail 48305 invoked by uid 89); 22 Nov 2018 04:42:06 -0000 Original-Received: by simscan 1.4.0 ppid: 48303, pid: 48304, t: 0.0023s scanners:none Original-Received: from unknown (HELO smtp102-2.vfemail.net) (172.16.100.62) by FreeQueue with SMTP; 22 Nov 2018 04:42:06 -0000 Original-Received: (qmail 14192 invoked by uid 89); 22 Nov 2018 04:41:36 -0000 Original-Received: by simscan 1.4.0 ppid: 14185, pid: 14188, t: 0.0294s scanners:none Original-Received: from unknown (HELO www.vfemail.net) (ZWRnYXJAb3Blbm1haWwuY2M=@172.16.100.137) by 172.16.100.62 with ESMTPA; 22 Nov 2018 04:41:36 -0000 Original-Received: from akQ+dFuw4YSre/MrPhYHJNKngCYIjXr4jjHIGw1qHBq1jXiXgtc2GQ== (vL8ykrrR67caRuYDr35Bqvj+N5bQ+utr) via pl/IMPp6amMb9BbWkEHW2TSZC/Z/i9Zj by www.vfemail.net with HTTP (HTTP/1.1 POST); Wed, 21 Nov 2018 22:42:06 -0600 X-Sender: edgar@openmail.cc X-detected-operating-system: by eggs.gnu.org: FreeBSD 8.x [fuzzy] X-Received-From: 199.16.11.132 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:118726 Archived-At: Hello, I want to share and know if there is a better way (more efficient or clearer) to replace something within a list (I don't know LISP). My code is like this, and it works for the given example. (defun my-list-replace (obj orig new) "Replaces an element in a list with something else" (let* ( ;; Position of the thing we need to remove (pos (cl-position orig obj :test 'equal)) ;; If pos is nil, reset to zero (pos (if pos pos 0)) ;; The length of the original object (objlen (length obj)) ;; The elements before the element to remove (head (butlast obj (- objlen pos))) ;; The elements after the element to remove (trail (nthcdr (+ 1 pos) obj))) ;; Join (1) the sub-list before the element to be replaced ;; with (2) the new element and (3) the rest of the list (append head (append new trail)) )) ;; Example simple (my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") '(":)")) ;; (("a" . "b") ":)") (my-list-replace '(("a" . "b") ("c" "d")) '("c" "d") (list '("hi"))) ;; (("a" . "b") ("hi")) ;; Example long (setq org-latex-default-packages-alist (my-list-replace org-latex-default-packages-alist '("T1" "fontenc" t ("pdflatex")) (list '("" "unicode-math" t ("xelatex" "xetex")) '("" "lmodern" t ("pdflatex")) '("QX" "fontenc" t ("pdflatex"))))) ------------------------------------------------- ONLY AT VFEmail! - Use our Metadata Mitigator to keep your email out of the NSA's hands! $24.95 ONETIME Lifetime accounts with Privacy Features! 15GB disk! No bandwidth quotas! Commercial and Bulk Mail Options!