Hope this banter is not totally inane... but:
Two solutions strike me as most intuitive, being trained in Java (and
agreeing with Horst). First, writing the equivalent java code is not
very difficult, but it does point out the 'tupling' as H calls it.
So the first solution, is to write Java code that gets compiled into the
most similar byte code! Make a temp. Do the ops. The machine does...
The other solution I come to, is to accomplish the same idea in process
that the python code is doing, which, aptly named, is 'tupling'.
So, why not create a Tuple class, define swap as a method of that class?
Then, you can define BOTH a and b in one line, then call swap on that:
abTuple = new MyTyple("foo","bar");
abTuple.swap(); //or abTuple = MyTuple.swap(abTuple); if you like!
So, what is the problem we are really looking at?
There are so many negative sentiments regarding Java,
but I think it has its benefits, and is worth knowing
what it is good for... right tool / right job, after all.
Regards to you silly hackers,
Ben B
P.S. strongly typed == worlds apart.
On Thu, 19 Jun 2003 21:49:21 -0700 (PDT)
Horst <[EMAIL PROTECTED]> wrote:
| On Thu, 19 Jun 2003, Larry Price wrote:
|
| > Neil popped this one on the clinic,
| >
| > can you write a function in java that does the equivalent of this
| > python script
| >
| > -------snip--------
| > #!/usr/bin/python
| >
| > def swap(a,b):
| > return b,a
| >
| > a = 'foo'
| > b = 'bar'
| > print a,b
| > a,b = swap(a,b)
| > print a,b
| > ------snap---------
| >
| > turns out to be surprisingly difficult ;-)
| >
|
| Cool :-)
|
| The convenience python offers here doesn't really come from the swap
| function, but from the transparent 'tupling' in-and-out -- at both
| sides of the assignment operator, and at the return............. Horst
| (No, I won't show the vector in-and-out version in Java, neither in
| C++ ,
| for that matter :-)
|
| #!/usr/bin/python
| def swap(a,b): return b,a
| a, b = 'foo', 'bar'
| print a,b
| a,b = swap(a,b)
| print a,b
| print "return type of swap():", type(swap(a, b))
|
|
_______________________________________________
EuG-LUG mailing list
[EMAIL PROTECTED]
http://mailman.efn.org/cgi-bin/listinfo/eug-lug