Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Friday, 4 March 2011

Trying out itertools in processing.py

Continuing my explorations of the differences between python / ruby / java(processing) I have recently created a context sensitive LSystem grammar library in ruby-processing. I'm not sure I could have done it in python or java for that matter (it is not that I don't think it can be done, because I intend to have a good go at repeating the exercise in processing.py and then java, but it should now be easier, having done it once). It is just that the semantic load seem to be much less in ruby. Take one simple thing in ruby you want to do something x number of times, it could not be simpler:-
x.times do
...
and you are done!!!!!
Well that is how I found itertools for python, it is interesting so I will play with it but it certainly does not lessen the semantic load (in fact it is mainly a distraction). But I'm not happy with the for i in range(0, x): loop when I have no intention of using i. However it turns out, that apart from the semantically nice 'repeat' , the syntax of the itertools is even crazier, for efficiency it is suggested the following form is used:-
for _ in itertools.repeat(None, x):
Where the count is not tracked?
One neat trick (in python) I learnt was that you can put import statements in a function (and so avoid namespace clashes) here is an extract from my modified grammar.py:-

55 def repeat(rpx, axiom, rules):
56     """
57     Repeat rule substitution in a recursive fashion rpx times
58     """ 
59     production = axiom
60     from itertools import repeat
61     for _ in repeat(None, rpx):
62         production = produce(production, rules)
63     return production

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2