4 ms·
They're great until you find something like this in code: [x for y in z for x in zz if y in x] Then you begin to wonder how great they really are.
by ascotan 6y ago
They're great until you find something like this in code:
[x for y in z for x in zz if y in x]
Then you begin to wonder how great they really are.
- mlthoughts2018 6y agoTo me that looks super easy to read. You just go left to right. for y in z: for x in zz: if y in x: yield x It’s extremely easy to sight read. If the lines become long, just split them up and it’s even more obvious. [x for y in z for x in zz if y in x] There’s nothing tricky about multiple loops and conditions in comprehensions in most languages that support them. Just go left to right and it mirrors outer to inner loops/conditionals.
- xushiwei 6y agoAgree.
- wenc 6y agoAlso, most data scientists presumably have some advanced math background so it's akin to reading set-builder notation (albeit with listcomps the sets are sometimes nested). Your listcomp reads: {x : y ∈ z, x ∈ zz | y ∈ x} If you've read enough statistics/ML papers, this becomes second nature.
- JNRowe 6y ago> If you've read enough statistics/ML papers, this becomes second nature. As someone who breathes that style of notation, there is excellent support in vim and emacs for custom display for readability. Conceal syntax in vim¹(builtin for rust/help/$few_others and external such as vim-cute-python²), and pretty-mode³ in emacs. I'm sure similar things exist in other editors too, but I don't use those ;) You don't get the exact representation in your example with the things I've mentioned, but if you're used to a more "mathy"-style they really are quite nice. YM[and taste]MV. 1. http://vimdoc.sourceforge.net/htmldoc/syntax.html#conceal http://vimdoc.sourceforge.net/htmldoc/syntax.html#conceal 2. https://github.com/ehamberg/vim-cute-python https://github.com/ehamberg/vim-cute-python (moresymbols branch for er… more symbols) 3. https://github.com/akatov/pretty-mode https://github.com/akatov/pretty-mode
- ascotan 6y agoInteresting. I didn't know that!