return more than one value from a for-comprehension
See original GitHub issue[@FroMage] I know we can do that with something that flattens the resulting comprehension afterwards, but it could be useful to support “returning” more than one value from a comprehension:
assert({1, 10, 2, 20, 3, 30}, {for (i in 1..3) *{i, i*10}});
Where I reuse the *
operator to spread more than one value in the comprehension.
[Migrated from ceylon/ceylon-spec#860]
Issue Analytics
- State:
- Created 10 years ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
python - Return multiple values from list comprehension if any ...
You can't use any because that checks the full list and returns true if any match, rather than returning the specific item which...
Read more >Return multiple values from a function in Python - thisPointer
To return more than one value using return statement, we can add them in a list and return the list. We can use...
Read more >Python List Comprehension: single, multiple, nested, & more
A list comprehension works by translating values from one list into another by placing a for statement inside a pair of brackets, formally...
Read more >For Comprehensions | Tour of Scala
A comprehension evaluates the body e for each binding generated by the enumerators and returns a sequence of these values. Here's an example:....
Read more >Advanced Python: Lists-of-Lists and List Comprehensions
You can only look for one value at a time; It only returns the index of the first occurrence of a value; if...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So, I’ve been thinking about this since I first found Ceylon, and I think I’ve figured out a pretty good, simple, intuitive, and regular syntax for this.
The idea is to have a syntax for both “expressions” and “multi-expressions”. Consider:
comma-list
:(multi-expression ("," multi-expression)*)?
multi-parentheses
:"(" multi-expression ")"
spread
:"*" expression
for-comprehension
:"for" for-iterator multi-expression
if-comprehension
:"if" condition-list multi-expression
multi-expression
:multi-parentheses
expression
comma-list
spread
for-comprehension
if-comprehension
With this approach, you would be able to have a comprehension that returns multiple values by either using the spread syntax or by using parentheses. For example:
Now, as an addendum to the idea, something interesting that could be done is to allow someone to define condition lists in terms of multi-expressions. That is, consider the following extension to the syntax of
multi-expression
:multi-expression
:is-condition
exists-condition
nonempty-condition
"!" multi-expression
And now look at how simple the syntax for
condition-list
becomes:condition-list
:"(" multi-expression ")"
This also helps fix the problem posed in #4540, by simply allowing people to write the following:
It’s interesting to note that those conditions would still be only allowed inside
if
,assert
, andwhile
, but this restriction would be done by the typechecker, not the parser. That is, using prefixexists
where an expression is expected would be syntactically incorrect, while using them where a multi-expression is expected (but outside a condition list) would be semantically incorrect.+1 for figuring this out. When I get a chance, I’ll link to real world code examples where this would help.