4 ms·
They should implement Python's list comprehensions in full. Right now Hy only allows one generator clause and one if clause, something like (list-comp (, x
by omaranto 12y ago
They should implement Python's list comprehensions in full. Right now Hy only allows one generator clause and one if clause, something like
(list-comp (, x y) (x (range 5)) (> x 2) (y (range 2)))
gives an error about list_comp only taking 3 or 4 arguments.
- Foxboron 12y agoAfter a quick IRC discussion; you wrote it wrong :D! Using hy --spy so you can see the equivalent python code: => (list-comp (, x y) [x (range 5) y (range 5)] (> 2 x)) from hy.core.language import range [(x, y) for x in range(5) for y in range(5) if (2 > x)] [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1) .....] Can submit a PR and fix the docs so this is better displayed!
- omaranto 12y agoDoesn't what you wrote work in a different, slower, way? I had in mind something analogous to r = [] for x in range(5): if 2>x: for y in range(5): r.append((x,y)) I think what you wrote probably does: r = [] for x in range(5): for y in range(5): if 2>x: r.append((x,y)) The first loop completely skips looping over y if the condition on x fails, the second loop repeatedly tests x.
- Foxboron 12y agoHurm yes indeed. Interesting problem. I'll copypasta this and fix up an issue! Nice catch!