And how do you iterate over the first n-1 elements of a list with n elements? This is what my code does:
i = 0 while i < len(term.args) - 1: ____mark_term(term.args[i]) ____i += 1 term = term.args[i] You can try yourself: % python3 >>> foo = ["a", "b", "c"] >>> i = 0 >>> while i < len(foo) - 1: ... print("mark_term", foo[i]) ... i += 1 ... mark_term a mark_term b >>> foo = foo[i] >>> foo 'c' alister schrieb am Mittwoch, 15. September 2021 um 20:41:12 UTC+2: > On Wed, 15 Sep 2021 11:31:48 -0700, Mostowski Collapse wrote: > > > There is a further problem with this: > > > >> for i,term in enumerate(term.args): > >> ____mark_term(term.args[i]) > > > > It should read: > > > > for i,help in enumerate(term.args): > > ____mark_term(help) > > > > But then i isn't need. > even Better (i had only skimmed the code as I was certain I would find > this, it is probably the No. 1 thing new python programmers get wrong > if your example is correct the it can be simplified even further to > > for help in term.args: > mark_term(help) > > & if help does not get used after this loop then a comprehension is even > better > _ == [mark_term(help) for help in term.args] > > > the underscore character is python convention for an unneeded place- > holder variable. > > > > Mostowski Collapse schrieb am Mittwoch, 15. September 2021 um 20:22:50 > > UTC+2: > >> Do you mean, replace this: > >> i = 0 while i < len(term.args) - 1: > >> ____mark_term(term.args[i]) > >> ____i += 1 term = term.args[i] > >> > >> By this: > >> > >> for i,term in enumerate(term.args): > >> ____mark_term(term.args[i]) > >> > >> This wouldn't be correct anymore. The recursive call is only for the > >> arguments except for the last one one. > >> alister schrieb am Mittwoch, 15. September 2021 um 20:17:23 UTC+2: > >> > On Wed, 15 Sep 2021 18:23:10 +0200, Mostowski Collapse wrote: > >> > > >> > > I really wonder why my Python implementation is a factor 40 slower > >> > > than my JavaScript implementation. > >> > > Structurally its the same code. > >> > > > >> > > You can check yourself: > >> > > > >> > > Python Version: > >> > > https://github.com/jburse/dogelog-moon/blob/main/devel/runtimepy/ > >> > machine.py > >> > > > >> > > JavaScript Version: > >> > > https://github.com/jburse/dogelog-moon/blob/main/devel/runtime/ > >> > machine.js > >> > > > >> > > Its the same while, if-then-else, etc.. its the same classes > >> > > Variable, Compound etc.. Maybe I could speed it up by some details. > >> > > For example to create an array of length n, I use in Python: > >> > > > >> > > temp = [NotImplemented] * code[pos] > >> > > pos += 1 > >> > > > >> > > Whereas in JavaScript I use, also in exec_build2(): > >> > > > >> > > temp = new Array(code[pos++]); > >> > > > >> > > So I hear Guido doesn't like ++. So in Python I use += > >> > > and a separate statement as a workaround. But otherwise, > >> > > what about the creation of an array, > >> > > > >> > > is the the idiom [_] * _ slow? I am assuming its compiled away. Or > >> > > does it really first create an array of size 1 and then enlarge it? > >> > > > >> > > Julio Di Egidio wrote: > >> > <sniped due to top posting> > >> > > >> > this is probably a string contender > >> > > >> > i = 0 while i < len(term.args) - 1: > >> > mark_term(term.args[i]) > >> > i += 1 term = term.args[i] > >> > > >> > try replacing with something more pythonic > >> > > >> > for index,term in enumerate(term.args): > >> > mark_term(term.args[i]) > >> > > >> > > >> > & possibly go all the way to changing it into a comprehension > >> > > >> > there are other similar anti patterns throughout this code. > >> > > >> > any time you are manually keeping a counter as an index into a > >> > list,tupple other iterable YOU ARE DOING IT WRONG! > >> > > >> > Do not write javascript in python, write python > >> > > >> > > >> > > >> > -- > >> > Two percent of zero is almost nothing. > >> > > >> > > >> > > >> > > >> > -- > >> > Whoever dies with the most toys wins. > -- > Pie are not square. Pie are round. Cornbread are square. -- https://mail.python.org/mailman/listinfo/python-list