last day (15 days later) » 

17:43
0
A: Why python function programming functions are not collection methods?

Winston EwertWhich class would you put these methods on? In python, I can use map on lists, tuples, dictionaries, files, strings, sets, arrays, etc. There is no collection base class to put a map,reduce,filter etc on. Now, python could have had a collection class that all these different things inherited fr...

@Doval, see artima.com/weblogs/viewpost.jsp?thread=98196 for Guido's thoughts on reduce. He thinks that outside of a few trivial cases reduces are really hard to read and are better done as explicit for loops.
@Doval, who said anything about choosing one construct over another because Guido said so? I'm not actually personally a fan of comprehensions. But, the design of python and the community has gone in the direction of preferring them over the functional language tradition of map/reduce/filter.
"There is no collection base class to put a map,reduce,filter etc on.": Why do you think that such methods should be put in a base class? Each collection classes could implement these methods and they would just happen to be there when you need them (duck typing).
@Giorgio, you really want every class that I can iterate on to have seperate implementations of all those methods?
@WinstonEwert: What would be so bad about that? After all the existing function implementations have to discriminate between the different collections they are applied to in the same way as the corresponding methods would.
@Giorgio, no they don't. They all just call __iter__ to iterate over the element and implement there particular operation on top of that. They don't have to reimplement their logic for every possible collection.
17:43
@WinstonEwert: Then each method can be a thin wrapper around this common function implementation using __iter__.
@Giorgio, you're missing the point. The point is that I can use map,reduce,filter on anything that defines __iter__ if I can only use them on objects that have also decided to specifically add these methods I've lost a great deal of the power of duck typing.
Hi, I understand your point: as soon as an object defines the __iter__ method, you can apply the generic function to it.
My point was that it would not be difficult to add the methods. Like all collection must have an iter method, they could have a map, a filter method and so on. They could still share the implementation.
One reason not to add these methods is that there are the functions already.
Maybe
sure, its not difficult
but its also not free
Why?
on your proposal, every new collection would have to implement several additional functions.
17:49
Like it has to implement __iter__
sure they are easy to implement, but you still do actually have to implement them
sure
but it is additional functions on top of that
They would all be one-liners.
What about adding a common superclass? Do you think it would be feasible?
there are other ways to solve this problem
python chose the generic functions operating on a small interface paradigm
other places like java have gone down the common superclass route
both have pros/cons
adding a superclass simply wouldn't be the python way
Ah OK.
that doesn't mean its wrong, it just not the way python went about it
17:52
Why wouldn't it be considered pythonic?
This is an interesting topic because we have a few places where we used inheritance in our code (at my workplace we work in Python).
Maybe we should have considered the generic function + duck typing approach?
you should have considered it yes
whether or not it was right depends
Multiple inheritance turned out to be complex and we had a pretty sneaky bug lately
I avoid multiple inheritance almost always
Ok, thanks for the hints. Maybe we meet in chat some other time.
Bye bye

last day (15 days later) »