- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Better way to write this?
Sat, 2012-01-14, 01:12
I feel like there is a slightly shorter way to write the foreach part, but I can't recall:
val list = 1 :: 2L :: 3d :: Nil
list.foreach { n => n match { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case _ => println("Unknown: " + n) }}
val list = 1 :: 2L :: 3d :: Nil
list.foreach { n => n match { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case _ => println("Unknown: " + n) }}
Sat, 2012-01-14, 01:41
#2
Re: Better way to write this?
On Fri, Jan 13, 2012 at 6:17 PM, Derek Williams <derek@fyrie.net> wrote:
Yes, of course. It felt wrong, and it was.
Thanks!
You should be able to drop the match part:
Yes, of course. It felt wrong, and it was.
Thanks!
Sat, 2012-01-14, 23:21
#3
Re: Better way to write this?
Omg something new again... and beautiful too
On Fri, Jan 13, 2012 at 4:17 PM, Derek Williams <derek@fyrie.net> wrote:
On Fri, Jan 13, 2012 at 4:17 PM, Derek Williams <derek@fyrie.net> wrote:
On Fri, Jan 13, 2012 at 5:11 PM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
I feel like there is a slightly shorter way to write the foreach part, but I can't recall:
val list = 1 :: 2L :: 3d :: Nil
list.foreach { n => n match { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case _ => println("Unknown: " + n) }}
You should be able to drop the match part:
list foreach { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case n => println("Unknown: " + n)}
--
Derek Williams
You should be able to drop the match part:
list foreach { case i: Int => println("Int: " + i) case l: Long => println("Long: " + l) case d: Double => println("Double: " + d) case n => println("Unknown: " + n)}
--
Derek Williams