On Thu, May 31, 2018 at 5:39 AM Peter O'Connor <peter.ed.oconnor@gmail.com> wrote:
Well, there need not be any ambiguity if you think of "B given A" as "execute A before B", and remember that "given" has a lower precedence than "for" (So [B given A for x in seq] is parsed as [(B given A) for x in seq]
Then
retval = [expr(name) given name=something(x) for x in seq]
Is:
retval = [] for x in seq: name = something(x) retval.append(expr(name))
And
retval = [expr(name, x) for x in seq given name=something]
Is: retval = [] name = something for x in seq: retval.append(expr(name, x))
But this is probably not a great solution, as it forces you to mentally unwrap comprehensions in a strange order and remember a non-obvious precedence rule.
You hit the nail on the head. It forces you to unwarp comprehensions in a strange order. This is why I want the "given" to be interspersed with the "for" and "if" and for everything to be in the order you declare it.
On the plus-side, it lets you initialize generators with in-loop updates (which cannot as far as I see be done nicely with ":="):
retval = [expr(name, x) given name=update(name, x) for x in seq given name=something]
Is:
retval = [] name = something for x in seq: name = update(name, x) retval.append(expr(name, x))
Why wouldn't you want to just put the outer given outside the entire comprehension? retval = [expr(name, x) given name=update(name, x) for x in seq] given name=something The more I think about it, the more i want to keep "given" in comprehensions, and given in expressions using parentheses when given is supposed to bind to the expression first.
On Thu, May 31, 2018 at 10:44 AM, Neil Girdhar <mistersheik@gmail.com> wrote:
Yes, you're right. That's the ambiguity I mentioned in my last message. It's too bad because I want given for expressions and given for comprehensions. But if you have both, there's ambiguity and you would at least need parentheses:
[(y given y=2*x) for x in range(3)]
That might be fine.
On Thu, May 31, 2018 at 4:34 AM Peter O'Connor < peter.ed.oconnor@gmail.com> wrote:
* Sorry, message sent too early:
On Thu, May 31, 2018 at 4:50 AM, Neil Girdhar <mistersheik@gmail.com> wrote:
[expression given name=something for x in seq]
retval = [] name = something for x in seq: retval.append(expression) return retval
That's a little confusing then, because, given the way given is used outside of comprehensions, you would expect
[y given y=2*x for x in range(3)]
to return [0, 2, 4], but it would actually raise an error.
On Thu, May 31, 2018 at 10:32 AM, Peter O'Connor < peter.ed.oconnor@gmail.com> wrote:
On Thu, May 31, 2018 at 4:50 AM, Neil Girdhar <mistersheik@gmail.com> wrote:
[expression given name=something for x in seq]
retval = [] name = something for x in seq: retval.append(expression) return retval
That's a little strange confusing then, because, given the way given is used outside of comprehensions, you would expect
for x in range(3): y given y=2*x
[y given y=2*x for x in range(3)]
to return [0, 2, 4], but it would actually raise an error.