gpt4 book ai didi

scala - 如何将 List[List[Long]] 转换为 List[List[Int]]?

转载 作者:行者123 更新时间:2023-12-04 19:51:59 31 4
gpt4 key购买 nike

转换 List[List[Long]] 的最佳方法是什么?到 List[List[Int]]在斯卡拉?

例如,给定以下类型列表 List[List[Long]]

val l: List[List[Long]] = List(List(11, 10, 11, 10, 11), List(8, 19, 24, 0, 2))

怎么转换成 List[List[Int]] ?

最佳答案

您也可以使用 cats lib 并撰写 List仿函数

import cats.Functor
import cats.implicits._
import cats.data._

val l: List[List[Long]] = List(List(11, 10, 11, 10, 11), List(8, 19, 24, 0, 2))

Functor[List].compose[List].map(l)(_.toInt)
//or
Nested(l).map(_.toInt).value

还有一种更纯粹的 Scala 方法(虽然不是很安全)
val res:List[List[Int]] = l.asInstanceOf[List[List[Int]]]

关于scala - 如何将 List[List[Long]] 转换为 List[List[Int]]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56398272/

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