Very cute! The first example also made me realise that map, filter, and the likes, that work on iterables, are lazy.
defnewfunc(val):print('lazy '+str(val))# this is proof for lazy
returnval*2result=map(newfunc,[1,2,3,4])print(result)# prints the map object, but the newfunc operation
# was not invoked on any of the values
print(next(result))# only invokes newfunc on 1
print(list(result))# invokes newfunc on the rest
Awesome! It's an interesting thing that I haven't known yet. Thanks a lot for your sharing.
Btw, Don't you mind if I share your example in the original post? It will help other developers more understand map and filter.
Very cute! The first example also made me realise that
map,filter, and the likes, that work on iterables, are lazy.repl.it example
Awesome! It's an interesting thing that I haven't known yet. Thanks a lot for your sharing.
Btw, Don't you mind if I share your example in the original post? It will help other developers more understand map and filter.
Also, notice that my code example has syntax highlighting.
This is because I use
Instead of
Thanks for the notice. It helps me a lot. :)
I most certainly don't mind :)