Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Gregory Ewing
Marko Rauhamaa wrote: Ian Kelly : Another point in favor of an explicit tail-call keyword. Then one couldn't make that mistake. How about "return"? How about "goto"? :-) That's not entirely an unserious suggestion -- if you really believe that a "goto with arguments" is a good feature for

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Stefan Behnel
Steven D'Aprano schrieb am 14.07.2015 um 06:54: > On Tuesday 14 July 2015 14:45, Ben Finney wrote: >> The Python reference says of a class ‘__new__’ method:: >> >> object.__new__(cls[, ...]) >> >> Called to create a new instance of class cls. __new__() is a static >> method (special-cas

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Marko Rauhamaa
Ian Kelly : > On Mon, Jul 13, 2015 at 11:25 PM, Chris Angelico wrote: >> (Also, side point: Python can't actually optimize the above function, >> because it actually means "call quicksort, then discard its return >> value and return None". A true tail call has to return the result of >> the recur

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Ian Kelly
On Mon, Jul 13, 2015 at 11:25 PM, Chris Angelico wrote: > (Also, side point: Python can't actually optimize the above function, > because it actually means "call quicksort, then discard its return > value and return None". A true tail call has to return the result of > the recursive call, and Pyth

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Paul Rubin
Chris Angelico writes: > That's a prime example of recursion... but not of recursion that can > be tail-call optimized into iteration. It's an example of forking > recursion, where one call can result in multiple calls (same with tree > traversal); it calls itself to sort the first part and the la

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Marko Rauhamaa
Chris Angelico : >> def quicksort(array, start, end): >> midp = partition(array, start, end) >> if midp <= (start+end)//2: >> quicksort(array, start, midp) >> quicksort(array, midp+1, end) >> else: >> quicksort(array, midp+1, end) >> quicksort(array

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Tue, Jul 14, 2015 at 3:15 PM, Paul Rubin wrote: > Chris Angelico writes: >> I'm not sure why the transition to another state has to be recursive. > > It's not recursive: it's more like a goto with arguments, and a tail > call expresses it nicely. Hmm, maybe, but I'm not sure that the transiti

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Tue, Jul 14, 2015 at 3:15 PM, Paul Rubin wrote: > It's difficult given how subjective the concept of warping is. What's > straightforward to someone else sounds likely to look warped to you and > vice versa. But how does this look: > > def quicksort(array, start, end): > midp = partiti

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Paul Rubin
Chris Angelico writes: > I'm not sure why the transition to another state has to be recursive. It's not recursive: it's more like a goto with arguments, and a tail call expresses it nicely. > Maybe this is something where previous experience makes you more > comfortable with a particular style,

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Ben Finney
Steven D'Aprano writes: > py> class Spam(object): > ... def __new__(cls): > ... print cls > ... > py> Spam.__new__() # implicit first arg? > Traceback (most recent call last): > File "", line 1, in > TypeError: __new__() takes exactly 1 argument (0 given) Thanks, I'm glad I

Re: str.index() and str.find() versus only list.index()

2015-07-13 Thread Chris Angelico
On Tue, Jul 14, 2015 at 2:58 PM, Steven D'Aprano wrote: > On Tuesday 14 July 2015 14:07, Ian Kelly wrote: > >> On Mon, Jul 13, 2015 at 9:23 PM, Steven D'Aprano >> wrote: >>> Correct. But rather than removing it, it would be better to take a leaf >>> out of re.match's book and return None as the s

Re: str.index() and str.find() versus only list.index()

2015-07-13 Thread Steven D'Aprano
On Tuesday 14 July 2015 14:07, Ian Kelly wrote: > On Mon, Jul 13, 2015 at 9:23 PM, Steven D'Aprano > wrote: >> On Tue, 14 Jul 2015 01:12 pm, Ian Kelly wrote: >> >>> On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven >>> wrote: Hi, Quick question: why does str have both index() and f

Re: Foo.__new__ is what species of method?

2015-07-13 Thread Steven D'Aprano
On Tuesday 14 July 2015 14:45, Ben Finney wrote: > Howdy all, > > The Python reference says of a class ‘__new__’ method:: > > object.__new__(cls[, ...]) > > Called to create a new instance of class cls. __new__() is a static > method (special-cased so you need not declare it as such

Foo.__new__ is what species of method?

2015-07-13 Thread Ben Finney
Howdy all, The Python reference says of a class ‘__new__’ method:: object.__new__(cls[, ...]) Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as i

Re: str.index() and str.find() versus only list.index()

2015-07-13 Thread Ian Kelly
On Mon, Jul 13, 2015 at 9:23 PM, Steven D'Aprano wrote: > On Tue, 14 Jul 2015 01:12 pm, Ian Kelly wrote: > >> On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven >> wrote: >>> Hi, >>> >>> Quick question: why does str have both index() and find(), while list >>> only has index()? Is there a reason fo

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Terry Reedy
On 7/13/2015 3:07 PM, Marko Rauhamaa wrote: Or, translated into (non-idiomatic) Python code: def common_prefix_length(bytes_a, bytes_b): def loop(list_a, list_b, common_length): if not list_a: re

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Steven D'Aprano
On Tue, 14 Jul 2015 12:05 am, Chris Angelico wrote: > If you want to make an assertion that iterative > code requires equivalent warping to tail-recursive code, I want to see > an example of it. Is that difficult? Of course it is. If it wasn't difficult, people would post examples instead of gett

Re: str.index() and str.find() versus only list.index()

2015-07-13 Thread Steven D'Aprano
On Tue, 14 Jul 2015 01:12 pm, Ian Kelly wrote: > On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven > wrote: >> Hi, >> >> Quick question: why does str have both index() and find(), while list >> only has index()? Is there a reason for that, or is it just an historical >> accident? > > Historical a

Re: str.index() and str.find() versus only list.index()

2015-07-13 Thread Ian Kelly
On Mon, Jul 13, 2015 at 10:56 AM, Roel Schroeven wrote: > Hi, > > Quick question: why does str have both index() and find(), while list only > has index()? Is there a reason for that, or is it just an historical > accident? Historical accident, I think. If it were to be redone, I doubt that str.f

Re: beginners choice: wx or tk?

2015-07-13 Thread Michael Torrie
On 07/13/2015 08:42 AM, Grant Edwards wrote: > If it didn't have to run on Windows, I'd pick pygtk over wx. I've > never tried qt. PyQt is very nice to work with. In some respects it's not as Pythonic as PyGTK. It feels a lot like transliterated C++ code, which it is. But it's a powerful toolki

Re: A webpy Templetor question

2015-07-13 Thread Chris Angelico
On Tue, Jul 14, 2015 at 9:13 AM, wrote: > #in my application.py: > def checknow(): > ... > return TN_str > > render = web.template.render('templates/',globals={'stat':checknow}) > > #in the template: > $def with(checknow) > ... ... > Test: $stat(checknow) You've effectively taken a refer

A webpy Templetor question

2015-07-13 Thread yongzhi . chen
Hi all, I want to display/update several metrics in a normal page (not in a webpy form). These metrics got updated every minute and were stored in a log file. I prepared a function to open that log file then analyze the last several lines to collect them. I want these metrics got updated in eve

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Terry Reedy
On 7/13/2015 7:22 AM, Jussi Piitulainen wrote: Ian Burnette writes: A post I did not receive, but want to comment on. I've recently been playing around with Clojure, and I really like the way they've overcome the JVM not supporting TRE. The way Clojure solves this problem is to have a built i

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Marko Rauhamaa
Ethan Furman : > I would love to see good functional examples as it is definitely a > weak spot for me. Oh, if you want to go functional, you should look at idiomatic Scheme code: (define (common-prefix-length bytes-a bytes

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Ethan Furman
Antoon, I think Chris is arguing in good faith; certainly asking for examples should be a reasonable request. Even if he is not, we would have better discussions and other participants would learn more if you act like he is. I would love to see good functional examples as it is definitely a

str.index() and str.find() versus only list.index()

2015-07-13 Thread Roel Schroeven
Hi, Quick question: why does str have both index() and find(), while list only has index()? Is there a reason for that, or is it just an historical accident? Best regards, Roel -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. --

A new module for performing tail-call elimination

2015-07-13 Thread Th. Baruchel
Hi, after having spent much time thinking about tail-call elimination in Python (see for instance http://baruchel.github.io/blog/ ), I finally decided to write a module for that. You may find it at: https://github.com/baruchel/tco Tail-call elimination is done for tail-recursion as well as for

Re: beginners choice: wx or tk?

2015-07-13 Thread Grant Edwards
On 2015-07-11, Chris Angelico wrote: > On Sat, Jul 11, 2015 at 7:28 PM, Ulli Horlacher > wrote: >> I want to start a project with python. >> The program must have a (simple) GUI and must run on Linux and Windows. >> The last one as standalone executable, created with pyinstaller. > > Not sure what

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Antoon Pardon
On 07/13/2015 04:05 PM, Chris Angelico wrote: > On Mon, Jul 13, 2015 at 11:55 PM, Antoon Pardon > wrote: >> On 07/13/2015 02:34 PM, Chris Angelico wrote: > Warping your code around a recursive solution > to make it into a perfect tail call usually means making it look a lot > less impr

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Mon, Jul 13, 2015 at 11:55 PM, Antoon Pardon wrote: > On 07/13/2015 02:34 PM, Chris Angelico wrote: >> Warping your code around a recursive solution to make it into a perfect tail call usually means making it look a lot less impressive; for instance, >>> And sometimes your proble

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Mon, Jul 13, 2015 at 11:46 PM, Ian Kelly wrote: > On Mon, Jul 13, 2015 at 6:34 AM, Chris Angelico wrote: >> On Mon, Jul 13, 2015 at 10:00 PM, Antoon Pardon >> wrote: >>> On 07/13/2015 01:28 PM, Chris Angelico wrote: Why is it worth writing your code recursively, only to have it be i

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Antoon Pardon
On 07/13/2015 02:34 PM, Chris Angelico wrote: > >>> Warping your code around a recursive solution >>> to make it into a perfect tail call usually means making it look a lot >>> less impressive; for instance, >> And sometimes your problem is very easily solved by a number of functions >> that tail c

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Ian Kelly
On Mon, Jul 13, 2015 at 6:34 AM, Chris Angelico wrote: > On Mon, Jul 13, 2015 at 10:00 PM, Antoon Pardon > wrote: >> On 07/13/2015 01:28 PM, Chris Angelico wrote: >>> Why is it worth writing your code recursively, only to have it be >>> implemented iteratively? >> >> Because sometimes, it is easi

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Mon, Jul 13, 2015 at 10:00 PM, Antoon Pardon wrote: > On 07/13/2015 01:28 PM, Chris Angelico wrote: >> On Sun, Jul 12, 2015 at 8:20 AM, Ian Burnette wrote: >>> [ About tail recursion ] >>> >> When a function is purely tail-recursive like this, it's trivial to >> convert it at the source code l

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Antoon Pardon
On 07/13/2015 01:28 PM, Chris Angelico wrote: > On Sun, Jul 12, 2015 at 8:20 AM, Ian Burnette wrote: >> [ About tail recursion ] >> > When a function is purely tail-recursive like this, it's trivial to > convert it at the source code level: > > def factorial(n): > acc = 1 > while n > 0: >

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Mon, Jul 13, 2015 at 9:22 PM, Jussi Piitulainen wrote: > That's oddly restricted to self-calls. To get the real thing, "recur" > should replace "return" - I'm tempted to spell it "recurn" - so the > definition would look like this: > > def factorial(n, acc=1): >if n == 0: > return acc

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Chris Angelico
On Sun, Jul 12, 2015 at 8:20 AM, Ian Burnette wrote: > > I've recently been playing around with Clojure, and I really like the way > they've overcome the JVM not supporting TRE. The way Clojure solves this > problem is to have a built in "recur" function that signals to the Clojure > compiler t

Interactive, test-driven coding challenges (algorithms and data structures)

2015-07-13 Thread donne . martin
Repo: https://github.com/donnemartin/interactive-coding-challenges Shortlink: https://bit.ly/git-code Hi Everyone, I created a number of interactive, test-driven coding challenges. I will continue to add to the repo on a regular basis. I'm hoping you find it useful as a fun, hands-on way to l

Re: Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Jussi Piitulainen
Ian Burnette writes: > I've recently been playing around with Clojure, and I really like the > way they've overcome the JVM not supporting TRE. The way Clojure > solves this problem is to have a built in "recur" function that > signals to the Clojure compiler to convert the code to a loop in the >

Possibly Pythonic Tail Call Optimization (TCO/TRE)

2015-07-13 Thread Ian Burnette
Hi there, First, please forgive me if this is the wrong venue to be suggesting this idea-- I'm just following the PEP workflow page . I've recently been playing around with Clojure, and I really like the way they've overcome the JVM not supp