构造一系列与时间相关的名称

时间:2014-10-21 14:03:19

标签: scala

我们的代码中包含以下序列:

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

我想以更简洁的方式重写这一点。在Scala中创建上述序列的最短方法是什么?

5 个答案:

答案 0 :(得分:16)

你走了:

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)

使用Scala 2.10:for理解和字符串插值格式化:

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)

答案 1 :(得分:16)

因为理解对笛卡尔积更具说明性

 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 :(得分:8)

我所知道的最简单的方法是使用字符串插值并使用除法和模数来得到30秒:

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

答案 3 :(得分:4)

变体:

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 :(得分:4)

只是为了好玩..

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)