[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Wanted: mapcar() without nils
From: |
Thierry Volpiatto |
Subject: |
Re: Wanted: mapcar() without nils |
Date: |
Sat, 13 Jun 2009 21:05:56 +0200 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.94 (gnu/linux) |
Hi, you can use loop: (easier)
ELISP> (mapcar #'(lambda (x) (when (stringp x) x)) '("a" "b" "c" 1 2 "d" 3))
("a" "b" "c" nil nil "d" nil)
ELISP> (loop for i in '("a" "b" "c" 1 2 "d" 3) when (stringp i) collect i)
("a" "b" "c" "d")
Nordlöw <per.nordlow@gmail.com> writes:
> This is my mockup:
>
> (defun extract-elements (pred seq)
> "Extract a copy of SEQ containing all elements fullfilling
> PRED."
> (delq nil (mapcar `(lambda (elm) (when (,pred elm)) elm) seq)))
> ;; Use: (extract-elements 'symbolp '(a b 1 2)) => '(a b)
> ;; Use: (extract-elements 'numberp '(a b 1 2)) => '(1 2)
>
> But why does my test-cases return '(a b 1 2) all the time? The first
> should return (a b) and second (1 2).
> I have tried edebugging it but because mapcar is a builtin I didn't
> get any help that way.
>
> /Nordlöw
>
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France