These are chat archives for evhub/coconut
reverseddoesn't work on
mapapplied to a sequence in Python is a problem I would be willing to try and solve in Coconut's implementation of
map(in fact I think it already should work in Coconut for
map). I'll raise an issue for it. In the meantime, I would recommend just doing
map) instead of after.
reversedlike @Nexus6 and I said above (it shouldn't matter whether you reverse before or after, since
map) don't care about order).
mapclass to add support for operations such as indexing (
__getitem__) and reversing (
__reversed__), simply by not actually doing the operation. Instead, Coconut remembers that the operation needs to be done next time something is retrieved from the
map, and does it then. In functional programming, this technique is called lazy evaluation. In fact, I just pushed a commit that should add similar support to
filteras well. Unfortunately, the above example still won't work, since
lencan't be applied lazily to a
filter, since unlike
map, you can't infer what the length will be without doing any calculations. However, the specific error @Slepice1 was encountering, for doing
filter, should disappear.
reversed((x for x in [1,2,3]))throwing an error?
range), that's true. But you can stack an arbitrary number of
zipoperations on top of any sequence and you'll still be able to reverse it.
reversed((x for x in [1,2,3]))will still fail, but something like the example in #207 won't anymore.
reversedshould now work on
filterin the latest
coconut-develop, and will be released in the next version. In the meantime, just
pip install coconut-develop.
filterso that instead of returning a plain iterator, it returns a more complex lazily-evaluated object with a
__reversed__method that just reverses the object that
filteris filtering. That way, if the object you pass into
reversed, then the object returned by
filterdoes as well. You can see the actual code I added in this commit: evhub/coconut@9067feb
zipobjects already worked this way, it just seems I forgot to do the same for
reversed. Sorry if I wasn't being clear about that!
@Nexus6 I agree #201 should probably be closed as a won't fix--I'll likely do that soon. #190 is definitely something I would like to implement, but it's true that it's going to be a lot of work. I'm also not yet entirely convinced that it would be superior to the alternative of using
return. For example, in #190 you provide the example of
def new_assign(foo) = if foo == 0: "Zero" elif foo == 1: "One" else: "Dunno"
which can be rewritten using
addpattern to be
def new_assign(0) = "Zero" @addpattern(new_assign) def new_assign(1) = "One" @addpattern(new_assign) def new_assign(foo) = "Dunno"