- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
[Style Guide] for-comprehensions
Sat, 2009-11-21, 22:46
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Sun, 2009-11-22, 16:27
#2
Re: [Style Guide] for-comprehensions
So, is the idea that we just want to avoid semi-colons, or are we using braces and parens to document the intent of the for-comprehension (parens for traditional 'for loop', and braces for difficult-to-explain monadic operations)?
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
Unfortunately, the "always use parens" approach requires the use of semi-colons (except with filters, which is weird IMHO), even when the generators are split across multiple lines. The example you gave doesn't actually parse.
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland <davec@naildrivin5.com> wrote:
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Sun, 2009-11-22, 20:07
#3
Re: [Style Guide] for-comprehensions
There's definitely a sound stylistic reason for both forms.As with many of these grey areas I tend to work with one variant when things are simple, and another when it gets more complicated.
And, yes, "gets more complicated" is excruciatingly subjective, hence the heated discussions :)
One possibility might be to look at the problem backwards. In the guide you start with the worst case scenario, and then document a shorter form to be used when things are sufficiently trivial.
On Sun, Nov 22, 2009 at 3:18 PM, David Copeland <davetron5000@gmail.com> wrote:
And, yes, "gets more complicated" is excruciatingly subjective, hence the heated discussions :)
One possibility might be to look at the problem backwards. In the guide you start with the worst case scenario, and then document a shorter form to be used when things are sufficiently trivial.
On Sun, Nov 22, 2009 at 3:18 PM, David Copeland <davetron5000@gmail.com> wrote:
So, is the idea that we just want to avoid semi-colons, or are we using braces and parens to document the intent of the for-comprehension (parens for traditional 'for loop', and braces for difficult-to-explain monadic operations)?
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
Unfortunately, the "always use parens" approach requires the use of semi-colons (except with filters, which is weird IMHO), even when the generators are split across multiple lines. The example you gave doesn't actually parse.
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland <davec@naildrivin5.com> wrote:
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Sun, 2009-11-22, 20:27
#4
Re: [Style Guide] for-comprehensions
I like that approach; seeing how the style works when things are really dicey is a good way to vet it (as with the class definition stuff in the other thread). So, how quickly can someone come up with a gnarly for comprehension? :)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sun, Nov 22, 2009 at 1:56 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sun, Nov 22, 2009 at 1:56 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
There's definitely a sound stylistic reason for both forms.As with many of these grey areas I tend to work with one variant when things are simple, and another when it gets more complicated.
And, yes, "gets more complicated" is excruciatingly subjective, hence the heated discussions :)
One possibility might be to look at the problem backwards. In the guide you start with the worst case scenario, and then document a shorter form to be used when things are sufficiently trivial.
On Sun, Nov 22, 2009 at 3:18 PM, David Copeland <davetron5000@gmail.com> wrote:
So, is the idea that we just want to avoid semi-colons, or are we using braces and parens to document the intent of the for-comprehension (parens for traditional 'for loop', and braces for difficult-to-explain monadic operations)?
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
Unfortunately, the "always use parens" approach requires the use of semi-colons (except with filters, which is weird IMHO), even when the generators are split across multiple lines. The example you gave doesn't actually parse.
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland <davec@naildrivin5.com> wrote:
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Sun, 2009-11-22, 20:37
#5
Re: [Style Guide] for-comprehensions
I think we may have something available from the scala-incubator work on IO and ARMThe Scala OTP is probably also a good place to look. I'll see if I can find a few spare moments to put an example together :)
On Sun, Nov 22, 2009 at 7:23 PM, David Copeland <davetron5000@gmail.com> wrote:
On Sun, Nov 22, 2009 at 7:23 PM, David Copeland <davetron5000@gmail.com> wrote:
I like that approach; seeing how the style works when things are really dicey is a good way to vet it (as with the class definition stuff in the other thread). So, how quickly can someone come up with a gnarly for comprehension? :)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sun, Nov 22, 2009 at 1:56 PM, Kevin Wright <kev.lee.wright@googlemail.com> wrote:
There's definitely a sound stylistic reason for both forms.As with many of these grey areas I tend to work with one variant when things are simple, and another when it gets more complicated.
And, yes, "gets more complicated" is excruciatingly subjective, hence the heated discussions :)
One possibility might be to look at the problem backwards. In the guide you start with the worst case scenario, and then document a shorter form to be used when things are sufficiently trivial.
On Sun, Nov 22, 2009 at 3:18 PM, David Copeland <davetron5000@gmail.com> wrote:
So, is the idea that we just want to avoid semi-colons, or are we using braces and parens to document the intent of the for-comprehension (parens for traditional 'for loop', and braces for difficult-to-explain monadic operations)?
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
Unfortunately, the "always use parens" approach requires the use of semi-colons (except with filters, which is weird IMHO), even when the generators are split across multiple lines. The example you gave doesn't actually parse.
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland <davec@naildrivin5.com> wrote:
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Wed, 2009-11-25, 00:07
#6
Re: [Style Guide] for-comprehensions
The general idea is to avoid forcing semi-colons to avoid surprising new users who might not even know that Scala accepts semicolons in its syntax. You would be surprised how many people think that Ruby won't parse semicolon-delimited statements. Additionally, the curly braces do help to document the monadic stuff.
In my experience, almost every for comprehension with more than two generators is going to be doing something monadic. This isn't always the case, but *almost* always. Hence, the guideline.
Daniel
On Nov 22, 2009, at 9:18 AM, "David Copeland" <davetron5000@gmail.com> wrote:
In my experience, almost every for comprehension with more than two generators is going to be doing something monadic. This isn't always the case, but *almost* always. Hence, the guideline.
Daniel
On Nov 22, 2009, at 9:18 AM, "David Copeland" <davetron5000@gmail.com> wrote:
So, is the idea that we just want to avoid semi-colons, or are we using braces and parens to document the intent of the for-comprehension (parens for traditional 'for loop', and braces for difficult-to-explain monadic operations)?
While I personally don't claim to understand the intricies of the for-comprehension, I would generally agree that the style guide should be for intermediate-or-better scala developers and not Java developers (and I think my initial suggestion is, in retrospect, more of a Java developer way of viewing the construct)
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
On Sat, Nov 21, 2009 at 8:20 PM, Daniel Spiewak < (djspiewak [at] gmail [dot] com> wrote:Unfortunately, the "always use parens" approach requires the use of semi-colons (except with filters, which is weird IMHO), even when the generators are split across multiple lines. The example you gave doesn't actually parse.
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland < (davec [at] naildrivin5 [dot] com> wrote:
I recall this being discussed, but wasn't sure if there was a consensus.
http://davetron5000.github.com/scala-style/control_structures/comprehensions.html
This seems very confusing to me, wrt when to use parens and when to use braces in the generator portion. It seems to be that it would be simpler to just always use parens for the generator and always use braces for the body (unless the body fits on the same line), e.g.
for (x <- 1 to 100) yield x * x
for (x <- 1 to 100; y <- 1 to 200) yield {
if (x < y) x + y
else x - y
}
for (x <- 1 to 100
y <- 1 to 400
z <- 4 to 44) {
println(x + y + z)
}
This just seems a lot easier to remember and get right. I don't actually know if there is a difference is between using parens and braces for the generators, but it just seems simpler this way.
Dave
---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com
Braces make a lot more sense when you start considering for-comprehensions as a convenience syntax for the monadic operations (well, one of them anyway). However, since no one (other than sigfpe) has ever been able to explain monads in one paragraph or less, I thought it was easier to just use the "multiple generators" guideline. :-)
Daniel
On Sat, Nov 21, 2009 at 3:45 PM, David Copeland <davec@naildrivin5.com> wrote: