WJ wrote:
> WJ wrote:
>
> > Peter Seibel wrote:
> >
> > > (loop for x in things
> > > when (foo-p x) collect x into foos
> > > when (bar-p x) collect x into bars
> > > when (and (foo-p x) (bar-p x)) collect x into both
> > > finally (return (values foos bars both)))
> > >
> > > Or if the predicate functions FOO-P and BAR-P are sufficiently
> > > expensive that you don't want to compute more often than absolutely
> > > necessary:
> > >
> > > (loop for x in things
> > > for foo-p = (foo-p x)
> > > for bar-p = (bar-p x)
> > > when foo-p collect x into foos
> > > when bar-p collect x into bars
> > > when (and foo-p bar-p) collect x into both
> > > finally (return (values foos bars both)))
MatzLisp (Ruby):
def foo? x; x.even?; end
def bar? x; 0 == x.modulo(3); end
foos,bars,both = [],[],[]
20.times{|x|
foo = foo? x
bar = bar? x
foos << x if foo
bars << x if bar
both << x if foo && bar }
[foos, bars, both]
===>
[[0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[0, 3, 6, 9, 12, 15, 18],
[0, 6, 12, 18]]
--
It is extremely gratifying to ... achieve membership in an elite that is going
to make history.... The only drawback is that the self-appointed shepherds are
likely to find eventually that they were only sheepdogs and herded the flock
toward a destination of which they knew nothing. --- R. P. Oliver