all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: 'Nordlöw' <per.nordlow@gmail.com>, help-gnu-emacs@gnu.org
Subject: RE: Wanted: mapcar() without nils
Date: Sat, 13 Jun 2009 13:02:54 -0700	[thread overview]
Message-ID: <41D3998CCF604FD8ABAFBC9B152DC594@us.oracle.com> (raw)
In-Reply-To: <65652541-cb7d-4c93-8baf-cd963990e57e@g1g2000yqh.googlegroups.com>

> (defun extract-elements (pred seq)
>   (delq nil (mapcar `(lambda (elm)
>                        (when (,pred elm)) elm)
>                     seq)))
> 
> But why does my test-cases return '(a b 1 2) all the time?

The body of your lambda is this: (when X) Y
The body is an implicit progn. It returns the value of the last sexp, which is
Y.

You meant (when X Y), not (when X) Y.

Better yet, you meant (and X Y), since you are interested in the value returned
- you are not interested only in some side effect.

(defun extract-elements (pred seq)
  (delq nil (mapcar (lambda (elm)
                      (and (funcall pred elm) elm))
                    seq)))

Or instead of constructing a list and then filtering it, construct it with only
the elements you want. IOW, promote the filter to be inside the iteration/map.

(defun extract-elements (pred seq)
  (let ((r  ()))
    (dolist (e seq) (when (funcall pred e) (push e r)))
    (nreverse r)))

That's not better or worse - just a different style (in this case).

You can debug problems like this yourself, by using `M-x debug-on-entry
extract-elements'.

Or try evaluating parts of your code using your concrete test args:
(mapcar (lambda (elm) (when (symbolp elm)) elm) '(a b 1 2))
(progn (when (symbolp 1)) 1)

Doing that, you see right away that the problem is a misplaced paren, not
something particular to `delq' or `mapcar' or backquote syntax or... IOW,
simplify the problem.





      parent reply	other threads:[~2009-06-13 20:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-13 12:19 Wanted: mapcar() without nils Nordlöw
2009-06-13 12:47 ` Lennart Borgman
     [not found] ` <mailman.562.1244897231.2239.help-gnu-emacs@gnu.org>
2009-06-13 18:24   ` Nordlöw
2009-06-13 19:05     ` Thierry Volpiatto
     [not found]     ` <mailman.581.1244920367.2239.help-gnu-emacs@gnu.org>
2009-06-13 19:33       ` Nordlöw
2009-06-13 19:45         ` Nordlöw
2009-06-13 19:43     ` Lennart Borgman
2009-06-13 20:02     ` Drew Adams [this message]

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=41D3998CCF604FD8ABAFBC9B152DC594@us.oracle.com \
    --to=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=per.nordlow@gmail.com \
    /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.