all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Wanted: mapcar() without nils
@ 2009-06-13 12:19 Nordlöw
  2009-06-13 12:47 ` Lennart Borgman
       [not found] ` <mailman.562.1244897231.2239.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 8+ messages in thread
From: Nordlöw @ 2009-06-13 12:19 UTC (permalink / raw
  To: help-gnu-emacs

How do I apply a function for each element in a sequence a return the
non-nil result only? I have used mapcar() but it returns the nils
aswell. Do I have to filter out the non-nils explicitly or is there a
builtin function that does all this?

/Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
  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>
  1 sibling, 0 replies; 8+ messages in thread
From: Lennart Borgman @ 2009-06-13 12:47 UTC (permalink / raw
  To: Nordlöw; +Cc: help-gnu-emacs

(delq nil ...)

On Sat, Jun 13, 2009 at 2:19 PM, Nordlöw<per.nordlow@gmail.com> wrote:
> How do I apply a function for each element in a sequence a return the
> non-nil result only? I have used mapcar() but it returns the nils
> aswell. Do I have to filter out the non-nils explicitly or is there a
> builtin function that does all this?
>
> /Nordlöw
>




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
       [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
                       ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Nordlöw @ 2009-06-13 18:24 UTC (permalink / raw
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
  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>
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Thierry Volpiatto @ 2009-06-13 19:05 UTC (permalink / raw
  To: help-gnu-emacs

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





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
       [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
  0 siblings, 1 reply; 8+ messages in thread
From: Nordlöw @ 2009-06-13 19:33 UTC (permalink / raw
  To: help-gnu-emacs

On Jun 13, 9:05 pm, Thierry Volpiatto <thierry.volpia...@gmail.com>
wrote:
> 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.nord...@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

I know the meaning of backquotes and commas to specify what should be
evaluated. But what meaning does '#' have in your example?:
> ELISP> (mapcar #'(lambda (x) (when (stringp x) x)) '("a" "b" "c" 1 2 "d" 3))

/Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
  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:43     ` Lennart Borgman
  2009-06-13 20:02     ` Drew Adams
  3 siblings, 0 replies; 8+ messages in thread
From: Lennart Borgman @ 2009-06-13 19:43 UTC (permalink / raw
  To: Nordlöw; +Cc: help-gnu-emacs

You misplaced a closing paren in the lambda

On Sat, Jun 13, 2009 at 8:24 PM, Nordlöw<per.nordlow@gmail.com> wrote:
> (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)))

 (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)




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Wanted: mapcar() without nils
  2009-06-13 19:33       ` Nordlöw
@ 2009-06-13 19:45         ` Nordlöw
  0 siblings, 0 replies; 8+ messages in thread
From: Nordlöw @ 2009-06-13 19:45 UTC (permalink / raw
  To: help-gnu-emacs

Oopps, my mistake got a parenthesis wrong...

Now it works:

(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)

Thanks,
Nordlöw


^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Wanted: mapcar() without nils
  2009-06-13 18:24   ` Nordlöw
                       ` (2 preceding siblings ...)
  2009-06-13 19:43     ` Lennart Borgman
@ 2009-06-13 20:02     ` Drew Adams
  3 siblings, 0 replies; 8+ messages in thread
From: Drew Adams @ 2009-06-13 20:02 UTC (permalink / raw
  To: 'Nordlöw', help-gnu-emacs

> (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.





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2009-06-13 20:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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.