Re: Write this accumuator in a functional style

2017-07-16 Thread Pavol Lisy
On 7/14/17, Steve D'Aprano wrote: > On Fri, 14 Jul 2017 09:06 am, Ned Batchelder wrote: > >> Steve's summary is qualitatively right, but a little off on the >> quantitative >> details. Lists don't resize to 2*N, they resize to ~1.125*N: >> >> new_allocated = (size_t)newsize + (newsize >> 3) +

Re: Write this accumuator in a functional style

2017-07-14 Thread Steve D'Aprano
On Fri, 14 Jul 2017 09:06 am, Ned Batchelder wrote: > Steve's summary is qualitatively right, but a little off on the quantitative > details. Lists don't resize to 2*N, they resize to ~1.125*N: > > new_allocated = (size_t)newsize + (newsize >> 3) + (newsize < 9 ? 3 : 6); > > (https://github

Re: Write this accumuator in a functional style

2017-07-14 Thread Paul Rubin
Rustom Mody writes: > Yeah I know append method is supposedly O(1). It's amortized O(1). -- https://mail.python.org/mailman/listinfo/python-list

Re: Write this accumuator in a functional style

2017-07-13 Thread Ned Batchelder
On Thursday, July 13, 2017 at 10:59:52 AM UTC-4, Pavol Lisy wrote: > On 7/13/17, Steve D'Aprano wrote: > > > [1] Actually, CPython's lists initially quadruple the size of the array, up > > to a > > certain point, and then switch to doubling. This ensures that small lists > > have > > even fewer e

Re: Write this accumuator in a functional style

2017-07-13 Thread Chris Angelico
On Fri, Jul 14, 2017 at 2:26 AM, Steve D'Aprano wrote: > On Fri, 14 Jul 2017 01:09 am, Rustom Mody wrote: > >> Couple that with the fact that space-time are not unrelated on any modern VM >> based OS + cache based hw. Doubly so for "managed" languages where gc buys >> space for time. > > I don't u

Re: Write this accumuator in a functional style

2017-07-13 Thread Pavol Lisy
On 7/13/17, Steve D'Aprano wrote: > On Fri, 14 Jul 2017 12:59 am, Pavol Lisy wrote: > >> On 7/13/17, Steve D'Aprano wrote: >> >>> [1] Actually, CPython's lists initially quadruple the size of the array, >>> up >>> to a >>> certain point, and then switch to doubling. This ensures that small lists

Re: Write this accumuator in a functional style

2017-07-13 Thread Steve D'Aprano
On Fri, 14 Jul 2017 12:59 am, Pavol Lisy wrote: > On 7/13/17, Steve D'Aprano wrote: > >> [1] Actually, CPython's lists initially quadruple the size of the array, up >> to a >> certain point, and then switch to doubling. This ensures that small lists >> have >> even fewer expensive resizes, at th

Re: Write this accumuator in a functional style

2017-07-13 Thread Steve D'Aprano
On Fri, 14 Jul 2017 01:09 am, Rustom Mody wrote: > Couple that with the fact that space-time are not unrelated on any modern VM > based OS + cache based hw. Doubly so for "managed" languages where gc buys > space for time. I don't understand that comment. Space/time have *never* been unrelated.

Re: Write this accumuator in a functional style

2017-07-13 Thread Rhodri James
On 13/07/17 16:09, Rustom Mody wrote: Pavol Lisy wrote: IMHO problem is doubling size for huge lists. Or waste big memory for huge frozensets. I mean resize it to 2*N if its size is just N+1. Couple that with the fact that space-time are not unrelated on any modern VM based OS + cache base

Re: Write this accumuator in a functional style

2017-07-13 Thread Rustom Mody
Pavol Lisy wrote: > IMHO problem is doubling size for huge lists. > Or waste big memory for huge frozensets. I mean resize it to 2*N if > its size is just N+1. Couple that with the fact that space-time are not unrelated on any modern VM based OS + cache based hw. Doubly so for "managed" langua

Re: Write this accumuator in a functional style

2017-07-13 Thread Pavol Lisy
On 7/13/17, Steve D'Aprano wrote: > [1] Actually, CPython's lists initially quadruple the size of the array, up > to a > certain point, and then switch to doubling. This ensures that small lists > have > even fewer expensive resizes, at the cost of wasting a bit more memory, but > its > only a sm

Re: Write this accumuator in a functional style

2017-07-13 Thread Steve D'Aprano
On Thu, 13 Jul 2017 10:10 pm, Rustom Mody wrote: > Yeah I know append method is supposedly O(1). > I find that surprising... > More so when the article > https://wiki.python.org/moin/TimeComplexity > talks of average case Vs amortized-worst case(!) Whatever does that mean? "Average case" refers

Re: Write this accumuator in a functional style

2017-07-13 Thread Rustom Mody
Marko wrote: > Simple, yes, but is the worst case > insertion/deletion time still within > O(log n)? Good point; and needs to be applied to Steven's append-using OP as well Yeah I know append method is supposedly O(1). I find that surprising... More so when the article https://wiki.python.or

Re: Write this accumuator in a functional style

2017-07-13 Thread Marko Rauhamaa
Paul Rubin : > Chris Angelico writes: >> Maybe I'm completely on the wrong track here, but the last time I >> implemented a self-balancing tree, it usually involved a fair amount >> of mutation. > > AVL trees are fairly simple to implement without mutation. Red-black > trees are traditionally im

Re: Write this accumuator in a functional style

2017-07-13 Thread Paul Rubin
Chris Angelico writes: > Maybe I'm completely on the wrong track here, but the last time I > implemented a self-balancing tree, it usually involved a fair amount > of mutation. AVL trees are fairly simple to implement without mutation. Red-black trees are traditionally implemented with mutation,

Re: Write this accumuator in a functional style

2017-07-13 Thread Chris Angelico
On Thu, Jul 13, 2017 at 6:15 PM, Paul Rubin wrote: > Chris Angelico writes: >> some point it'll need to be rebalanced, which could at worst case >> be O(n). > > No, you use a structure like an AVL tree or red-black tree, so it's > within a constant factor of balanced after each insertion. You re

Re: Write this accumuator in a functional style

2017-07-13 Thread Paul Rubin
Chris Angelico writes: > some point it'll need to be rebalanced, which could at worst case > be O(n). No, you use a structure like an AVL tree or red-black tree, so it's within a constant factor of balanced after each insertion. You rewrite O(log n) of the nodes, and juggle around a constant nu

Re: Write this accumuator in a functional style

2017-07-12 Thread Chris Angelico
On Wed, Jul 12, 2017 at 7:52 PM, Paul Rubin wrote: > Not so easily in Python since the built-in list and dict types are > designed for mutation update. In Haskell, the list type is a linked > list and the dictionary type is a balanced tree. So, you can make a new > list consisting of a new item

Re: Write this accumuator in a functional style

2017-07-12 Thread Paul Rubin
Steven D'Aprano writes: > for parrot in parrots: > accumulator[parrot.colour].append(parrot) > > That's pretty compact and understandable, but it require mutating a bunch > of pre-allocated lists inside an accumulator. Can we re-write this in a > functional style? Not so easily in Python si

Re: Write this accumuator in a functional style

2017-07-12 Thread Steven D'Aprano
On Wed, 12 Jul 2017 17:47:50 +1200, Gregory Ewing wrote: > Steve D'Aprano wrote: >> - Greg's dict comprehension version requires N+1 passes through the >> data, >> one to convert to a list, and 1 per each possible key. > > Just to be clear, my solution was a response to the requirement that it

Re: Write this accumuator in a functional style

2017-07-12 Thread Rustom Mody
On Tuesday, July 11, 2017 at 4:11:50 PM UTC+5:30, Alain Ketterlin wrote: > Steven D'Aprano writes: > > > I have a colleague who is allergic to mutating data structures. Yeah, I > > know, he needs to just HTFU but I thought I'd humour him. > > > > Suppose I have an iterator that yields named tuple

Re: Write this accumuator in a functional style

2017-07-11 Thread Gregory Ewing
Steve D'Aprano wrote: - Greg's dict comprehension version requires N+1 passes through the data, one to convert to a list, and 1 per each possible key. Just to be clear, my solution was a response to the requirement that it be written in a purely functional style. It's not now I would actually

Re: Write this accumuator in a functional style

2017-07-11 Thread Steve D'Aprano
On Tue, 11 Jul 2017 04:58 pm, Chris Angelico wrote: > On Tue, Jul 11, 2017 at 4:11 PM, Steven D'Aprano wrote: [...] >> accumulator = {'blue': [], 'green': [], 'red': []} >> for parrot in parrots: >> accumulator[parrot.colour].append(parrot) [...] > It's a partitioning filter. (Three way, not

Re: Write this accumuator in a functional style

2017-07-11 Thread Pavol Lisy
On 7/11/17, Steven D'Aprano wrote: > I have a colleague who is allergic to mutating data structures. Yeah, I > know, he needs to just HTFU but I thought I'd humour him. > > Suppose I have an iterator that yields named tuples: > > Parrot(colour='blue', species='Norwegian', status='tired and shagged

Re: Write this accumuator in a functional style

2017-07-11 Thread Ian Kelly
On Tue, Jul 11, 2017 at 12:47 AM, Wolfgang Maier wrote: > On 07/11/2017 08:11 AM, Steven D'Aprano wrote: >> >> I have a colleague who is allergic to mutating data structures. Yeah, I >> know, he needs to just HTFU but I thought I'd humour him. >> >> Suppose I have an iterator that yields named tup

Re: Write this accumuator in a functional style

2017-07-11 Thread Alain Ketterlin
Steven D'Aprano writes: > I have a colleague who is allergic to mutating data structures. Yeah, I > know, he needs to just HTFU but I thought I'd humour him. > > Suppose I have an iterator that yields named tuples: > > Parrot(colour='blue', species='Norwegian', status='tired and shagged out') >

Re: Write this accumuator in a functional style

2017-07-11 Thread Terry Reedy
On 7/11/2017 2:11 AM, Steven D'Aprano wrote: I have a colleague who is allergic to mutating data structures. Yeah, I know, he needs to just HTFU but I thought I'd humour him. Suppose I have an iterator that yields named tuples: Parrot(colour='blue', species='Norwegian', status='tired and shagge

Re: Write this accumuator in a functional style

2017-07-11 Thread Peter Otten
Steven D'Aprano wrote: > I have a colleague who is allergic to mutating data structures. Yeah, I > know, he needs to just HTFU but I thought I'd humour him. > > Suppose I have an iterator that yields named tuples: > > Parrot(colour='blue', species='Norwegian', status='tired and shagged out') >

Re: Write this accumuator in a functional style

2017-07-11 Thread Chris Angelico
On Tue, Jul 11, 2017 at 4:11 PM, Steven D'Aprano wrote: > I have a colleague who is allergic to mutating data structures. Yeah, I > know, he needs to just HTFU but I thought I'd humour him. > > Suppose I have an iterator that yields named tuples: > > Parrot(colour='blue', species='Norwegian', stat

Re: Write this accumuator in a functional style

2017-07-10 Thread Wolfgang Maier
On 07/11/2017 08:11 AM, Steven D'Aprano wrote: I have a colleague who is allergic to mutating data structures. Yeah, I know, he needs to just HTFU but I thought I'd humour him. Suppose I have an iterator that yields named tuples: Parrot(colour='blue', species='Norwegian', status='tired and shag

Re: Write this accumuator in a functional style

2017-07-10 Thread Gregory Ewing
Steven D'Aprano wrote: Help me humour my colleague. class Parrot: def __init__(self, color, species, status): self.color = color self.species = species self.status = status def __repr__(self): return "%s/%s/%s" % (self.species, self.color, self.status)