gpt4 book ai didi

scala - 构造与时间相关的名称序列

转载 作者:行者123 更新时间:2023-12-03 21:02:31 25 4
gpt4 key购买 nike

我们的代码中有以下序列:
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 中创建上述序列的最短方法是什么?

最佳答案

For comprehensions 更能说明笛卡尔积

 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)

关于scala - 构造与时间相关的名称序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26488660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com