William James
10/29/2015 8:12:00 PM
WJ wrote:
> Kaz Kylheku wrote:
>
> > In article <3C5D9C11.C16B9319@pacbell.net>, Steven M. Haflich wrote:
> > >
> > > Kaz Kylheku wrote:
> > > >
> > >> In article <3c5c2e63.522412171@nntp.interaccess.com>, Thaddeus L Olczyk wrote
> > :
> > >> >Is there a function, call it mapcar-if,
> > >> >which acts like mapcar with one tiny exception?
> > >> >If the return value of the lambda expression is nil,
> > >> >then it does not get inserted into the list returned by mapcar.
> > > >
> > >> Yes, mapcan. RTFM.
> > > >
> > >> Under mapcan, the function is expected to return a list.
> > >> The returned lists are destructively catenated into one big
> > >> list that is returned. Any NIL outputs from the function disappear
> > >> in the catenation.
> > >
> > > This suggestion to use mapcan doesn't quite satisfy the original
> > > request, since (as you observe) it requires rewriting the function
> > > argument to return a list.
> >
> > A rewrite is not absolutely necessary, which is why I didn't observe it
> > that way. Thanks to closures and higher order functions, we can create
> > an adapter object on the fly to change a function's apparent
> > interface:
> >
> > (defun non-nil-value-to-list (f)
> > #'(lambda (x)
> > (let ((y (funcall f x)))
> > (if y (list y)))))
> >
> > (mapcan (non-nil-value-to-list #'whatever-func) whatever-list)
>
> Gauche Scheme:
>
> (filter-map
> (^x (and (number? x) (* x x)))
> '(b 2 3 d e 8))
>
> ===>
> (4 9 64)
MatzLisp (Ruby):
[:b,2,3,:d,:e,8].flat_map{|x| x.is_a?(Numeric) ? [x*x] : []}
==>[4, 9, 64]
--
The report card by the American Society of Civil Engineers showed the national
infrastructure a single grade above failure, a step from declining to the point
where everyday things simply stop working the way people expect them to. ---
washingtonpost.com/local/trafficandcommuting/us-infrastructure-gets-d-in-annual-report/2013/03/19/c48cb010-900b-11e2-9cfd-36d6c9b5d7ad_story.html