[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: mapcar-if

William James

6/15/2015 9:41:00 PM

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)

--
Therefore, a dying empire and its fearful elite is always willing to resort to
massive violence against the citizens of the empire in order to turn them into
slaves. www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm
2 Answers

William James

10/29/2015 8:12:00 PM

0

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

William James

2/8/2016 11:53:00 PM

0

WJ wrote:

> 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]

Shorter:

[:b,2,3,:d,:e,8].flat_map{|x| x.is_a?(Numeric) ? x*x : []}


Another way:

[:b,2,3,:d,:e,8].grep->(x){x.is_a? Numeric}{|n| n*n}

--
[A]n unholy alliance of leftists, capitalists, and Zionist supremacists has
schemed to promote immigration and miscegenation with the deliberate aim of
breeding us out of existence in our own homelands.... [T]he real aim stays the
same: the biggest genocide in human history.... --- Nick Griffin
(https://www.youtube.com/watch?v=K...)