Re: ADA Popularity Discussion Request

From: jayessay (nospam_at_foo.com)
Date: 08/29/04


Date: 29 Aug 2004 12:14:39 -0400


Kevin Cline <kevin.cline@gmail.com> wrote:
> Hyman Rosen <hyrosen@mail.com> wrote in message
> news:<1093634083.931713@master.nyc.kbcfp.com>...
>>
>> No fair picking an easy one. Suppose you have a vector of complex
>> numbers and want to set the real part of each to zero. That's far
>> from a one liner in C++ even though it's conceptually trivial.
>
> With boost::lambda, it would be a one liner:
> for_each(v.begin(), v.end(), _1.r = 0);

Why not just use the real thing (instead of a pale emasculated hack)

(map 'vec (lambda (c) (complex 0 (imagpart c))) v)

Actually it's simple to be more flexible:

(defun zero-realpart (vec &optional (type (type-of vec)))
  (map type (lambda (c) (complex 0 (imagpart c))) vec))

(let ((vc1 (vector #c(1 2) #c(2 3) #c(1/3 4.5))))
  (zero-realpart vc1))

==> #(#c(0 2) #c(0 3) #c(0.0 4.5))

(let ((vc1 (list #c(1 2) #c(2 3) #c(1/3 4.5))))
  (zero-realpart vc1))

==> (#c(0 2) #c(0 3) #c(0.0 4.5))

(let ((vc1 (list #c(1 2) #c(2 3) #c(1/3 4.5))))
  (zero-realpart vc1 'vector))

==> #(#c(0 2) #c(0 3) #c(0.0 4.5))

It wouldn't take much to generalize this to be much more flexible and
generic while still generating optimal code.

/Jon

-- 
'jay' - a n t h o n y at romeo/november/charley com