Hacker News new | ask | show | jobs
by thomasahle 3742 days ago
The comprehension approach, while requiring more syntax, seems to often produce a more 'natural' order of operations. For example, compare

  [p for p in range(2,100) if all(p%q!=0 for q in range(2,p))]
with

  range(2,100).filter(p => range(2,p).map(q => p%q!=0).all())
It might be a small thing, but in the first one I feel the prime 'p' becomes the center piece, whereas in the second, 'p' is burrowed somewhat in the expression.
1 comments

Both are fairly unintelligible to me (probably partly due to my lack of interest in prime numbers), so it seems like splitting hairs to draw comparisons, but if I had to figure out what was going on, I'd rather be staring at this (~ES6):

    range(2, 100).filter(p => {
  
        // I'd probably stick a comment here to explain this ...
        // I'd split out this whole section into a named function once I understood it.
        return range(2, p)
            .map(q => p % q != 0)
            .all();

    })
I don't feel like I could iterate on understanding this problem so well in Python because I have to reach for one of 2 seemingly inferior solutions (list comprehensions or borked lambdas).