We have the following sequence in our code:
val halfHourlyColumnNames = Seq("t0000", "t0030", "t0100", "t0130", "t0200", "t0230", "t0300", "t0330", "t0400", "t0430", "t0500", "t0530", "t0600", "t0630", "t0700", "t0730", "t0800", "t0830", "t0900", "t0930", "t1000", "t1030", "t1100", "t1130", "t1200", "t1230", "t1300", "t1330", "t1400", "t1430", "t1500", "t1530", "t1600", "t1630", "t1700", "t1730", "t1800", "t1830", "t1900", "t1930", "t2000", "t2030", "t2100", "t2130", "t2200", "t2230", "t2300", "t2330")
I would like to rewrite this in a much more concise way. What would be the shortest way to create the above sequence in Scala?
The time order words 'first', 'after that', 'then', and 'finally' help to bring the events together and tell you which ones happened first, second, third and last. Time order words help to clarify our speech and writing, to make sure that listeners and readers understand the order of events.
A sequential time is one in which the numbers form a normal sequence, such as 1:02:03 4/5/06 (two minutes and three seconds past 1 am on 4 May 2006 (or April 5, 2006 in the United States) or the same time and date in the "06" year of any other century).
Examples of sequence words These words are signals that tell you a story is starting. 'Then', 'later', 'after' and 'suddenly' are sequence words that might be found in the middle of a story, and signal that a new event is being described.
Here you go:
scala> (0 to 23).flatMap( h => List(0,30).map( m => "t%02d%02d".format(h,m) ))
res8: scala.collection.immutable.IndexedSeq[String] = Vector(
t0000, t0030, t0100, t0130, t0200, t0230, t0300, t0330, t0400, t0430, t0500,
t0530, t0600, t0630, t0700, t0730, t0800, t0830, t0900, t0930, t1000, t1030,
t1100, t1130, t1200, t1230, t1300, t1330, t1400, t1430, t1500, t1530, t1600,
t1630, t1700, t1730, t1800, t1830, t1900, t1930, t2000, t2030, t2100, t2130,
t2200, t2230, t2300, t2330)
Using Scala 2.10: for
comprehension and string interpolation with formatting:
scala> for( h <- 0 to 23; m <- Seq(0,30)) yield f"t$h%02d$m%02d"
res6: scala.collection.immutable.IndexedSeq[String] = Vector(
t0000, t0030, t0100, t0130, t0200, t0230, t0300, t0330, t0400, t0430, t0500,
t0530, t0600, t0630, t0700, t0730, t0800, t0830, t0900, t0930, t1000, t1030,
t1100, t1130, t1200, t1230, t1300, t1330, t1400, t1430, t1500, t1530, t1600,
t1630, t1700, t1730, t1800, t1830, t1900, t1930, t2000, t2030, t2100, t2130,
t2200, t2230, t2300, t2330)
For comprehensions is more demonstrative for Cartesian product
scala> for {
| hour <- 0 to 23
| minutes <- List(0, 30)
| } yield "t%02d%02d".format(hour, minutes)
res0: scala.collection.immutable.IndexedSeq[String] = Vector(t0000, t0030, t0100, t0130, t0200, t0230, t0300, t0330, t0400, t0430, t0500, t0530, t0600, t0630, t0700, t0730, t0800, t0830, t0900, t0930, t1000, t1030, t1100, t1130, t1200, t1230, t1300, t1330, t1400, t1430, t1500, t1530, t1600, t1630, t1700, t1730, t1800, t1830, t1900, t1930, t2000, t2030, t2100, t2130, t2200, t2230, t2300, t2330)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With