question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 10 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
zamfofexcommented, Aug 26, 2017

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:

function expand<Element>({{Element*}*} streams) => {for(value stream in streams) *stream};
value div = Div{for(section in sections) (Title(section.title), Text(section.text))};

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:

if(!(exists foo, exists bar, hours < 24))
{
	return(null);
}

return(foo + bar + hours);

It’s interesting to note that those conditions would still be only allowed inside if, assert, and while, but this restriction would be done by the typechecker, not the parser. That is, using prefix exists 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.

value foo = exists bar; // syntax error
value foo = [exists bar]; // semantical error
0reactions
jvasileffcommented, Aug 26, 2017

+1 for figuring this out. When I get a chance, I’ll link to real world code examples where this would help.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found