arrays - 类型安全的矩形多维数组类型

标签 arrays scala collections types multidimensional-array

你如何代表 长方形 Scala中的二维(或多维)数组数据结构?

也就是说,每一行都有相同的长度,编译时验证 ,但尺寸是在运行时确定的?
Seq[Seq[A]]具有所需的接口(interface),但它允许用户提供“参差不齐”的数组,这可能导致运行时失败。
Seq[(A, A, A, A, A, A)] (和类似的)确实验证了长度是否相同,但它也强制在编译时指定这个长度。

示例界面

这是我的意思的示例界面(当然,内部维度不必是元组;它可以指定为列表或其他类型):

// Function that takes a rectangular array
def processArray(arr : RectArray2D[Int]) = {
    // do something that assumes all rows of RectArray are the same length
}

// Calling the function (OK)
println(processArray(RectArray2D(
    ( 0,  1,  2,  3),
    (10, 11, 12, 13),
    (20, 21, 22, 23)
)))
// Compile-time error
println(processArray(RectArray2D(
    ( 0,  1,  2,  3),
    (10, 11, 12),
    (20, 21, 22, 23, 24)
)))

最佳答案

这可以使用 Shapeless图书馆的大小类型:

import shapeless._

def foo[A, N <: Nat](rect: Seq[Sized[Seq[A], N]]) = rect

val a = Seq(Sized(1, 2, 3), Sized(4, 5, 6))
val b = Seq(Sized(1, 2, 3), Sized(4, 5))

现在foo(a)编译,但 foo(b)没有。

这使我们可以编写非常接近您所需界面的内容:
case class RectArray2D[A, N <: Nat](rows: Sized[Seq[A], N]*)

def processArray(arr: RectArray2D[Int, _]) = {
  // Run-time confirmation of what we've verified at compile-time.
  require(arr.rows.map(_.size).distinct.size == 1)
  // Do something.
}

// Compiles and runs.
processArray(RectArray2D(
  Sized( 0,  1,  2,  3),
  Sized(10, 11, 12, 13),
  Sized(20, 21, 22, 23)
))

// Doesn't compile.
processArray(RectArray2D(
  Sized( 0,  1,  2,  3),
  Sized(10, 11, 12),
  Sized(20, 21, 22, 23)
))

关于arrays - 类型安全的矩形多维数组类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11517437/

相关文章:

sql - 给定两个数组如何获取不在两个数组中的项目?

php - glob() - 按上次修改的日期时间戳对文件数组进行排序

Scala Spark - 将元组的值映射到单个值

scala - 解析器组合器 : Does repsep allows back-tracking?

c# - 如何使字典按值查找对象键

c# - 用于组织的 Linq 表达式(类似于 Group By 但不同)

Java:从集合中删除元素

c# - 在 C# 中修剪数组中的所有字符串

arrays - 使用 Bash 生成乐透号码

java - scala @Serializable 和 Java Serializable 有什么区别?