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.
> 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.
A good source of inspiration: Ruby's Enumerable[1] and Scala's Iterable[2]. Toguether, they have one of the most complete functional collection methods library.
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.
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
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?
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.
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.
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/.
[+] [-] phzbOx|14 years ago|reply
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
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.
[+] [-] DanielRibeiro|14 years ago|reply
[1] http://ruby-doc.org/core-1.9.3/Enumerable.html
[2] http://www.scala-lang.org/api/current/scala/collection/immut...
[+] [-] andrewcooke|14 years ago|reply
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
[+] [-] sashahart|14 years ago|reply
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
[+] [-] ynd|14 years ago|reply
[+] [-] phzbOx|14 years ago|reply
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.
[+] [-] ericmoritz|14 years ago|reply
[+] [-] lucian1900|14 years ago|reply
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
[+] [-] hyphyphyph|14 years ago|reply
[deleted]
[+] [-] Codayus|14 years ago|reply
[+] [-] phzbOx|14 years ago|reply