Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.lisp
Re: looking for a more elegant solution to a simple problem
William James
2/23/2016 9:41:00 PM
Pascal Costanza wrote:
> > I was wondering if there was a more concise way of doing something using
> > built-in functions: Given a list lst and a function fn, I want to
> > construct a list which contains the results of applying F to memmbers of
> > L and only keeps the ones which return a true value. It is much simpler
> > in code:
> >
> > (member-if identity (map #'fn lst))
>
> I know about three ways to do that:
>
> (mapcan (lambda (elem)
> (when (fn elem)
> (list (fn elem))))
> list)
>
> (loop for elem in list
> when (fn elem) collect (fn elem))
>
> or check out the excellent collecting macros at
>
http://www.tfeb.org/lisp/hax.html#...
>
> You might want to make sure that fn is only applied once to each
> element. You can do that with when-let in Lispworks. Or use the
> following idiom:
>
> (let ((var (fn elem)))
> (when var (list var)))
>
> Or the following macro:
>
> (defmacro whenlet ((var exp) &body body)
> `(let ((,var ,exp))
> (when ,var ,@body)))
MatzLisp (Ruby):
def func x
5 == x ? nil : x*x
end
(0..12).map{|x| func(x)}.compact
==>[0, 1, 4, 9, 16, 36, 49, 64, 81, 100, 121, 144]
--
Europe is not going to be the monolithic societies that they once were in the
last century.... They are now going into a multicultural mode. Jews will be
resented because of our leading role. --- Barbara Spectre
http://archive.org/download/DavidDuke_videos/HowZionistsDivideAndConquer-fjjsz...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: looking for a more elegant solution to a simple problem
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password