Re: "foreach" loops in IDL [message #64789] |
Mon, 19 January 2009 02:31  |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
rtk schrieb:
> On Jan 16, 11:30 am, alaniwiuse...@googlemail.com wrote:
>> In case anyone's interested, I've produced a little hack that lets you
>> have "foreach" loops in IDL
>
> Neat hack!
>
> Along the same lines, I have a set of DLMs here that add things like
> foreach, map, filter, reduce, etc. to IDL along with lambda functions
> and lists:
>
> http://www.ittvis.com/info/hof/
>
> For example, with the foreach DLM, your example becomes:
>
> foreach, lambda('x: print, x', /P), myarray
>
> There is also a pure IDL version for functions called 'each()' which
> is quite useful.
>
> Map, filter and reduce work pretty much as they do in Python (with
> extensions). There are also compose, apply, cart, accumulate, and
> group higher-order functions. With cart() it is possible to do the
> equivalent of Python list comprehensions.
>
> If you try it and have any questions let me know.
>
> Ron
> oneelkruns@hotmail.com
>
Some more hacks and we can call it python
cheers
Reimar
/me lives in both worlds already
|
|
|
|
Re: "foreach" loops in IDL [message #64795 is a reply to message #64794] |
Fri, 16 January 2009 12:43   |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On Jan 16, 3:34 pm, Paolo <pgri...@gmail.com> wrote:
> I was referring to foreach.pro , not to what you posted.
> Sorry for the confusion.
>
> I guess that I have the same loathing for common blocks that
> David has for heap_gc ... I guess anybody is entitled his
> quirks ;-)
>
> Ciao,
> Paolo
>
>
>
> rtk wrote:
>> On Jan 16, 12:37 pm, Paolo <pgri...@gmail.com> wrote:
>>> My opinion is that something like that make the
>>> code more difficult to understand and prevent
>>> utilization in two different programs running at the
>>> same time in the same session because of the
>>> common blocks.
>
>> It is unclear which set of extensions you are referring to, but if you
>> mean the ones I mentioned I encourage you to take a second look at
>> lambda.pro. There will be no problem between programs because of the
>> common block. Also, the extensions are meant mostly for command line
>> use.
>
>> As for being hard to read and understand, that is just a matter of
>> experience and opinion. Functional languages do pretty well with
>> constructs like these and vastly more sophisticated ones.
>
>> Lastly, as always, if you don't like something, don't use it :)
>
>> Ron- Hide quoted text -
>
> - Show quoted text -
I would have to agree with Paolo here. The for-loop syntax is simple
and clear. The @do @done really obfuscates the code.
|
|
|
Re: "foreach" loops in IDL [message #64797 is a reply to message #64795] |
Fri, 16 January 2009 12:34   |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
I was referring to foreach.pro , not to what you posted.
Sorry for the confusion.
I guess that I have the same loathing for common blocks that
David has for heap_gc ... I guess anybody is entitled his
quirks ;-)
Ciao,
Paolo
rtk wrote:
> On Jan 16, 12:37 pm, Paolo <pgri...@gmail.com> wrote:
>> My opinion is that something like that make the
>> code more difficult to understand and prevent
>> utilization in two different programs running at the
>> same time in the same session because of the
>> common blocks.
>
> It is unclear which set of extensions you are referring to, but if you
> mean the ones I mentioned I encourage you to take a second look at
> lambda.pro. There will be no problem between programs because of the
> common block. Also, the extensions are meant mostly for command line
> use.
>
> As for being hard to read and understand, that is just a matter of
> experience and opinion. Functional languages do pretty well with
> constructs like these and vastly more sophisticated ones.
>
> Lastly, as always, if you don't like something, don't use it :)
>
> Ron
|
|
|
|
Re: "foreach" loops in IDL [message #64803 is a reply to message #64798] |
Fri, 16 January 2009 11:37   |
pgrigis
Messages: 436 Registered: September 2007
|
Senior Member |
|
|
My opinion is that something like that make the
code more difficult to understand and prevent
utilization in two different programs running at the
same time in the same session because of the
common blocks.
Therefore, I don't like the idea very much.
Ciao,
Paolo
alaniwiuse...@googlemail.com wrote:
> In case anyone's interested, I've produced a little hack that lets you
> have "foreach" loops in IDL, i.e. loop over the contents of an array.
> It would be nice if the syntax were to let you have something like:
>
> for myval in myarray do begin
> print, myval
> endfor
>
> Unfortunately it doesn't, unless I've missed something somewhere. And
> it's just a little fiddly to have to loop over an array index every
> time, e.g.:
>
> for i=0, n_elements(myarray) - 1 do begin
> myval = myarray[i]
> print, myval
> endfor
>
> The hack lets you use the following syntax:
>
> foreach, 'myval', myarray
> @do
> print, myval
> @done
>
> and nested loops are supported.
>
> If anyone is interested in having this, grab this a (tiny) download,
> that contains a few short files to put somewhere in your IDL_PATH:
>
> http://home.badc.rl.ac.uk/iwi/idl-foreach.tar.gz
>
> Regards,
> Alan
>
> P.S. Please note that I do not read this mailbox. Do a web search for
> "Alan Iwi" if you want my email address.
|
|
|
Re: "foreach" loops in IDL [message #64805 is a reply to message #64803] |
Fri, 16 January 2009 10:54   |
rtk
Messages: 22 Registered: September 2008
|
Junior Member |
|
|
On Jan 16, 11:30 am, alaniwiuse...@googlemail.com wrote:
> In case anyone's interested, I've produced a little hack that lets you
> have "foreach" loops in IDL
Neat hack!
Along the same lines, I have a set of DLMs here that add things like
foreach, map, filter, reduce, etc. to IDL along with lambda functions
and lists:
http://www.ittvis.com/info/hof/
For example, with the foreach DLM, your example becomes:
foreach, lambda('x: print, x', /P), myarray
There is also a pure IDL version for functions called 'each()' which
is quite useful.
Map, filter and reduce work pretty much as they do in Python (with
extensions). There are also compose, apply, cart, accumulate, and
group higher-order functions. With cart() it is possible to do the
equivalent of Python list comprehensions.
If you try it and have any questions let me know.
Ron
oneelkruns@hotmail.com
|
|
|
Re: "foreach" loops in IDL [message #64869 is a reply to message #64795] |
Tue, 20 January 2009 13:11   |
JD Smith
Messages: 850 Registered: December 1999
|
Senior Member |
|
|
On Jan 16, 3:43 pm, Vince Hradil <vincehra...@gmail.com> wrote:
> I would have to agree with Paolo here. The for-loop syntax is simple
> and clear. The @do @done really obfuscates the code.
That's because it's an unsupported add-on. If IDL included foreach at
the language level, which would be clearer:
for i=0,n_elements(x)-1 do begin
elem=x[i]
print, my_function(elem)
end
or
foreach elem in x
print, my_function(elem)
end
When treating a vector as a list, requiring an extra loop variable is
pure syntactic overhead. Not to mention that the loop variable could
overflow, could get changed in the body of the loop or, most commonly,
risks nested sub-loops accidentally re-using the same loop variable.
None of these happens with a foreach construct.
JD
|
|
|
Re: "foreach" loops in IDL [message #64871 is a reply to message #64869] |
Sat, 31 January 2009 04:20  |
alaniwiusenet
Messages: 2 Registered: January 2009
|
Junior Member |
|
|
On 20 Jan, 21:11, JD Smith <jdtsmith.nos...@yahoo.com> wrote:
>
> That's because it's an unsupported add-on. If IDL included foreach at
> the language level, which would be clearer: [...]
Exactly.
Of course the hack is a bit ugly. It is more intended for use in
quick-and-dirty scripts than anything you intend to distribute etc.
My original post was also partly intended as a way to express a desire
for inclusion of "foreach" at the language level. Anyone at ITTVIS
reading this? If so, please also allow empty lists while you're at
it. Thanks. :-)
Alan
====
Again please note that I do not read this mailbox. Do a web search
for "Alan Iwi" if you want my email address.
|
|
|