top | item 3256248

Moka - Minimalist functional python library

53 points| phzbOx | 14 years ago |github.com | reply

34 comments

order
[+] phzbOx|14 years ago|reply
Clickable link for the doc: http://phzbox.com/moka/index.html

Moka is still in an alpha stage; but that being said, I'd love to hear some feedback. Feel free to browse the code, it should be pretty straightforward to any python dev.

[+] tef____|14 years ago|reply
You've invented your own incompatible dialect of python. You've broken composition, objects and the general design choices of python.

1. You break Composition

Your magic flag within a list changes all the semantics of the methods between immutable and mutable lists.

> x = List([1,2,3]).saving()

So now, if you're passed a list you have no idea if the operations you do will mutate the list.

This breaks composition entirely. You can't pass a mutable list into a function built for immutable lists without destroying things.

2. You break objects

Picking another example, this breaks duck typing and inheritance and polymorphism.

>def user_logged(users): > return List(users).all(User.is_logged)

this does not have the same semantics as:

> def user_logged(users): > return all(user.is_logged() for user)

Because the method lookup is done per instance, rather than assuming everything is the same class.

3. You're writing jquery in python

Python chose not to demand that all iterables implement a series of operators, but provides them as functions within a module. The rationale is that it is easier to add new functions within itertools, and there is far less to do to correctly implement the iterator protocol

You can see this in the "".join(foo) operator too. Instead of demanding all iterables support join, string takes an iterable as argument.

Making readable and maintainable python comes from using the existing idioms within the language and used within the community. Your proposed solution isn't readable, and it isn't pythonic.

[+] andrewcooke|14 years ago|reply
two small suggestions: change .saving() to .mutable() and .rem() to .remove() (or filter(), reversing the semantics, since that is already in python) (every other method is a full word).

also, maybe .mutable() would be better as a flag in the constructor. it makes no sense to use it in a chain (in fact, that could be confusing) and is the kind of thing you should probably fix on construction rather than changing later.

ps. it's not clear to me that this is better than list comprehensions, which already do much of what you have.

[+] rhizome31|14 years ago|reply
You should really give Ruby a try.
[+] sashahart|14 years ago|reply
Here's a gist comparing the Moka front page example to a slightly more typical Python way of doing things:

https://gist.github.com/1380898

EDIT: using the timeit module on these, the Moka version is about 18 times slower on my machine... not that this matters much if it's a much nicer way of expressing the program

[+] phzbOx|14 years ago|reply
Thanks for sharing. And ya, it's freaking slow right now.
[+] ynd|14 years ago|reply
The doc at http://www.phzbox.com/moka/index.html is pretty complete. The only thing I find is missing is a few words about efficiency. Does calling List(l) copy the list l for example?
[+] phzbOx|14 years ago|reply
Optimizing for performance is the next big step of Moka and I understand that it's the breaking point for lots of python developers. So, I'll make sure to work on that and provide realistic benchmarks.

As for the copy, yes it does as it tries to act like a builtin list as much as possible. (In fact, moka.List inherit from list). When it's in immutable state, a new list is returned at each step. If it's in mutable, self[:] = (..) is used.

[+] lucian1900|14 years ago|reply
This is indeed minimalist, there's less code in it than I'd expected.

The idioms it introduces do clash with Python ones, but on the other hand, I've been learning Clojure lately and my Python code has more comprehensions than previously. I'm not sure what to think.

[+] tmcw|14 years ago|reply
Needs a README and examples without a hop.
[+] Codayus|14 years ago|reply
This is pretty interesting looking. I'm tempted to try using it on my next project...anyone else used it yet? Seem stable/bug free?
[+] phzbOx|14 years ago|reply
It's fairly new but feel free to browse the code; it should be pretty straightforward to you. I've tried to provide good examples to make it easy to learn/use on phzbox.com/moka/.