scala - Scala / Cats:如何解压缩 NonEmptyList
问题描述
标准库提供了unzip
on 的方法List
:
scala>val l = List((1, "one"), (2, "two"), (3, "three"), (4, "four"), (5, "five"))
scala> l.unzip
// res13: (List[Int], List[String]) = (
// List(1, 2, 3, 4, 5),
// List("one", "two", "three", "four", "five")
//)
有没有办法NonEmptyList
从cats
图书馆实现同样的目标:
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of((1, "one"), (2, "two"), (3, "three"), (4, "four"), (5, "five"))
//res15: NonEmptyList[(Int, String)] = NonEmptyList(
// (1, "one"),
// List((2, "two"), (3, "three"), (4, "four"), (5, "five"))
//)
解决方案
您可以简单地调用nel.toList
并使用标准l.unzip
,然后NonEmptyList.fromList(unziped_list)
使用结果。
编辑:正如@Dylan 所说,您也可以使用.fromListUnsafe
来摆脱该选项。
推荐阅读
- sql - 同一列ms访问sql(mdb)中的值差异
- javascript - 修改 JS 代码以删除 DatePicker 选项
- apache-spark - 使用 .net-spark 加载具有多个部分的固定位置文件
- node.js - 使用链接包的项目上的 npm 过时/更新失败
- customization - 汇总明细行的值(在扩展中)并将其放在标题中(在扩展中)
- database - 正确分析 Azure 日志
- python - RandomForestClassifier skl2onnx 的 final_types
- prolog - 希望 PROLOG 要求 == 匹配已经匹配的变量,= 用于其他变量
- aws-iot - SiteWise 在为后期数据生成指标方面不一致
- firebase - 服务帐号无权访问 Firestore