r/shittyprogramming • • Apr 10 '19

One Line of Code™

https://gist.github.com/WorryingWonton/ce8a3a74b0298f0b734fe4dd0a883a63
101 Upvotes

22 comments sorted by

View all comments

Show parent comment

1

u/the_pw_is_in_this_ID Apr 12 '19

This is good insight, thanks!

So you have some context on what 'faking lazy eval' provides in other languages, so you have some idea on what (it sounds like) generators provide:

I know that lots of external IO processes can rely heavily on lazy-evaluation of generator objects. Python's generators seem, to me, to be the perfect implementation of receiving IO streams - from files, hardware, whatever.

There are also situations I'm aware of in webdev-world where the actual generator Object can be created, mutated, and then evaluated only once mutations are finished - a good example is database queries, where two classes might know two separate things about the query: one knows about ordering, and one knows about filtering, and each can mutate the DB query separately before some parent class evaluates it. (You can imagine how applying a filter before the query is valuable here.) And then, hey: once you have a generator object representing your query instead of a raw list of data, you might as well pipe it into an HTTP response which just reads straight from the database's output stream! (Not always ideal, but I like streams in general.)

I think you're on point about

we don't actually need the list after the fact

though - all this only works if you manage to keep your various functions/classes caring about one datum at a time, and agnostic to the list as a whole.