From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: John Mastro Newsgroups: gmane.emacs.help Subject: Re: How to delete all nil properties from a plist? Date: Wed, 5 Aug 2015 18:05:17 -0700 Message-ID: References: <87oaiq3buh.fsf@mbork.pl> <87mvya3bij.fsf@mbork.pl> <871tfmv5d5.fsf@kuiper.lan.informatimago.com> <87si81u09e.fsf@kuiper.lan.informatimago.com> <87mvy5nwid.fsf@kuiper.lan.informatimago.com> <87pp31s1go.fsf@nl106-137-147.student.uu.se> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1438823153 21188 80.91.229.3 (6 Aug 2015 01:05:53 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 6 Aug 2015 01:05:53 +0000 (UTC) To: "help-gnu-emacs@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Aug 06 03:05:51 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZN9d1-0004wG-7U for geh-help-gnu-emacs@m.gmane.org; Thu, 06 Aug 2015 03:05:51 +0200 Original-Received: from localhost ([::1]:42959 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZN9d0-0004lu-89 for geh-help-gnu-emacs@m.gmane.org; Wed, 05 Aug 2015 21:05:50 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:51197) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZN9cp-0004ln-AJ for help-gnu-emacs@gnu.org; Wed, 05 Aug 2015 21:05:40 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZN9co-0005B4-99 for help-gnu-emacs@gnu.org; Wed, 05 Aug 2015 21:05:39 -0400 Original-Received: from mail-ob0-x232.google.com ([2607:f8b0:4003:c01::232]:33627) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZN9cn-0005As-QF for help-gnu-emacs@gnu.org; Wed, 05 Aug 2015 21:05:38 -0400 Original-Received: by obdeg2 with SMTP id eg2so44927023obd.0 for ; Wed, 05 Aug 2015 18:05:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=QglbfU/9YKA8iSo2pqa2UrnMMb4+jLLEBA16usm1yVQ=; b=E7E3JcBNjEP+hp4tM0OElENzyAWLWxivXuB38f7RO5KIKpuzFqXiJfIa5A9zYeeZPk J3SAuqaPnJ0dZlsyJFLNiGa3lPM0oSLLs8skYEsNxC8q+Q5760FfRsMrB1y+ywnlFCwF 3Xxhnbh4i7wrCSyK1cNmuk7Z33yz/JAKJCN4T++I3ECpM8Zm+g8pyJZmc7fFV0UrIBp+ M1eisBE/AQm0pNmb4HbxU8UCyG5U1sOR/HYHPLaVwh33SA5zYOqF3rr2llfiNlkyaChD rZxjOyt0M6tnMFCAAdhvPxKfzFctJYSM67dTf3ylPvDKLYIFThp7Gq/GVvTGZvWVjCTT wWWQ== X-Received: by 10.182.236.66 with SMTP id us2mr10425074obc.5.1438823137353; Wed, 05 Aug 2015 18:05:37 -0700 (PDT) Original-Received: by 10.76.168.70 with HTTP; Wed, 5 Aug 2015 18:05:17 -0700 (PDT) In-Reply-To: <87pp31s1go.fsf@nl106-137-147.student.uu.se> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4003:c01::232 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 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-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:106279 Archived-At: > This only uses `cons', `cdr', and `nreverse', so it is > linear, right? ("linear" only from going thru the > input list.) > > (require 'cl) > > (defun plist-drop-nil-props (l) > (let ((new) > (nil-prop) > (prop) ) > (cl-loop for x in l > do (if (setq prop (not prop)) > (if x (setq new (cons x new)) > (setq nil-prop t)) > (if x > (if nil-prop (setq nil-prop nil) > (setq new (cons x new)) ) > (setq new (cdr new) )))) > (nreverse new) )) I took a swing at this too for the fun of it. Here's what I ended up with: (defun plist-drop-nil-props (plist) (let* ((result (list 'head)) (last result)) (cl-loop for (key val) on plist by #'cddr do (when-let (new (and val (list key val))) (setcdr last new) (setq last (cdr new)))) (cdr result))) The `when-let' macro is new in Emacs 25 but obviously it can be trivially replaced with `let' and `when' (or `-when-let' from dash.el). -- john