Something that has been haunting me for some time. I don't know if it's a gripe, a feature request or just lack of knowledge but:
There are cases where I want to do a complicated comprehension.
In these cases I just wish there was a way to make the expression more readable by having some local assignments.
example time
mysuperlongandcomplicateddataretrieval(x) = x+1
v = [0,1,2,3]
# so normally one would do:
com = [mysuperlongandcomplicateddataretrieval(x) >= 0 ? mysuperlongandcomplicateddataretrieval(x) : mysuperlongandcomplicateddataretrieval(x)-1 for x in v]
# what I would like to do is:
com = [y >= 0 ? y : y-1 for x in v for y=mysuperlongandcomplicateddataretrieval(x)]
you are probably wondering "Dude, that works!" but please have some fantasy and imagine that my list has non iteratable elements!
Normally I default doing something like this:
# iterate a single element list
com = [y >= 0 ? y : y-1 for x in v for y=[mysuperlongandcomplicateddataretrieval(x)]]
But it feels kind of hacky. I don't want to iterate, I just want to make an assignment.
com = [
let y = mysuperlongandcomplicateddataretrieval(x)
y >= 0 ? y : y-1
end for x in v
]
That's an interesting one but would prefer if it was an oneliner.. which get me thinking; how come there is still not a @let
or @begin
macro. Then one could do:
com = [@let(y = mysuperlongandcomplicateddataretrieval(x), y >= 0 ? y : y-1) for x in v]
I guess this could lead to many parsing problems..
[let y = mysuperlongandcomplicateddataretrieval(x); y >= 0 ? y : y-1; end for x in v]
then
yes, I considered it but the end
term in the middle of the comprehension can be confusing, which is why I though of a macro use case.
Last updated: Nov 06 2024 at 04:40 UTC