What to do after Python?

Sheila King sheila at spamcop.net
Mon Feb 19 00:54:19 EST 2001


On 18 Feb 2001 18:28:53 -0800, Johann Hibschman <johann at physics.berkeley.edu>
wrote in comp.lang.python in article <mtelwvbe3e.fsf at astron.berkeley.edu>:

:Sheila King writes:
:
:> On Sun, 18 Feb 2001 12:42:31 -0800, Erik Max Francis <max at alcyone.com> wrote
:> in comp.lang.python in article <3A9033B7.D5ADA989 at alcyone.com>:
:
:> I must say, that I am shocked at the number of apparent *groans* over C++
:> language, in this thread.
:
:Really?  Any language that encourages
:
:vector<double>::iterator end = vec->end();
:vector<double> out(vec->size);
:for(vector<double>::const_iterator i = vec->begin(),
:    vector<double>::iterator j = out->begin();
:    i != end;
:    ++i, ++j) {
:  *j = (*i)*(*i);
:}

I really don't understand that code, probably because I don't teach the STL. I
can see that you're creating some new vector of doubles, called out, the same
size as the vector vec. And for some reason, you're using two iterators to do
your for-loop, though I can't see why. We use only two templated classes in
the course I teach (remember, I'm only teaching a one semester course spread
out over a year), and we don't do them that way. The templates I'm familiar
with are the apvector, which I believe is quite similar to the STL vector
class, and the apmatrix, which is similar to the STL matrix class.

To take the contents of one vector, and square each entry and put it in
another vector, I would probably do this:

apvector<double> out( vec.length());
for ( int i= 0; i < vec.length(); i++)
    out[i]= vec[i]*vec[i]

I think that the code I list above is equivalent to yours?

:as the right way to square a vector is, well, not quite the way I'd
:want to do that.  Personally, I prefer something like
:
:(map-into (make-vector (length vec))
:          #'(lambda (x) (* x x))
:          vec)

My Python isn't that good, either. I haven't learned the lambda stuff, yet.
Still, I never was claiming any sort of comparison between Python and C++.
Matter of fact, I thought I said (in another message) that I really LIKE and
sort of prefer Python. The question was, what is a good second language to
learn. I don't see why C++ would be a bad choice, although I never argued in
favor of it, either.

:The whole STL thing seems incredibly verbose, and there's no way
:that's really needed.  I wouldn't want to try to teach that to people.
:I guess that's just my preference.

Well, I don't know about the STL in great detail. I've not been using nor
teaching it. I suppose, I should eventually learn it. Well, when the need
comes along. However, my spouse codes in C++ for a living (works for/worked
for Rockwell/JPL/Boeing) and he thinks your example code is too verbose, and
suggested this:

vector<double> in;
vector<double> out;
:
:
:
for (int i=0; i<in.size(); i++) 
 out[i]=in[i]*in[i];

instead, which is very similar to my code, which I suggest.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/





More information about the Python-list mailing list