14

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?

0

5 Answers 5

16

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)
16

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)
2
  • I very much like this version but I have marked @tuxdna's response as the correct one because it's shorter and I was asking for the shortest way.
    – joscas
    Commented Oct 21, 2014 at 16:59
  • @joscas The two solutions are exactly the same, as the for comprehension translates directly to the flatMap and map solution. This could fit on one line just the same. In fact, I would say this solution is somewhat more readable in this format. Commented Oct 22, 2014 at 2:13
8

The shortest way I know of to do this is with string interpolation and using division and modulus to get the 30s:

(0 to 47).map(i => f"t${i/2}%02d${30*(i%2)}%02d")
0
4

A variation:

for { time <- 0 to 47 }
    yield "t%02d%02d".format(time/2, (if( time % 2 == 0) 0 else 30))

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)
4

Just for fun ..

scala> (0 to 2330).map(x => f"t$x%04d").filter(_.matches(".*(00|30)$"))
res20: 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)
2
  • Nice one too. A bit convoluted though :-)
    – joscas
    Commented Oct 21, 2014 at 17:47
  • Yeah, I may never use it but it was fun :)
    – mohit
    Commented Oct 21, 2014 at 17:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.