在Scala中初始化2D(多维)数组
发布时间:2020-12-16 09:44:57 所属栏目:安全 来源:网络整理
导读:在Java中初始化2D数组(或者实际上是任何多维数组)很简单: int[][] x = new int[][] { { 3,5,7,},{ 0,4,9,{ 1,8,6,}; 它很容易阅读,它类似于2D矩阵等。 但是如何在Scala中做到这一点? 最好的,我可以想出看起来,很简单: val x = Array( Array(3,7),Array
在Java中初始化2D数组(或者实际上是任何多维数组)很简单:
int[][] x = new int[][] { { 3,5,7,},{ 0,4,9,{ 1,8,6,}; 它很容易阅读,它类似于2D矩阵等。 但是如何在Scala中做到这一点? 最好的,我可以想出看起来,很简单: val x = Array( Array(3,7),Array(0,9),Array(1,6) ) 我在这里看到的问题: >它一遍又一遍地重复“Array”(除了Array之外可能还有其他的东西) val x = Array( Array(3,4),// <= OK with compiler,silently ruins x Array(1,6) ) 有一个防守,直接指定类型,但它看起来比Java更加过分: val x: Array[Array[Int]] = Array( Array(3,// <= this one would trigger a compiler error Array(1,6) ) 这最后一个例子需要数组,甚至比Java中的int [] []要多3倍。 有没有明确的方法呢? 解决方法
我建议使用Scala 2.10和宏:
object MatrixMacro { import language.experimental.macros import scala.reflect.macros.Context import scala.util.Try implicit class MatrixContext(sc: StringContext) { def matrix(): Array[Array[Int]] = macro matrixImpl } def matrixImpl(c: Context)(): c.Expr[Array[Array[Int]]] = { import c.universe.{ Try => _,_ } val matrix = Try { c.prefix.tree match { case Apply(_,List(Apply(_,List(Literal(Constant(raw: String)))))) => def toArrayAST(c: List[TermTree]) = Apply(Select(Select(Ident("scala"),newTermName("Array")),newTermName("apply")),c) val matrix = raw split "n" map (_.trim) filter (_.nonEmpty) map { _ split "," map (_.trim.toInt) } if (matrix.map(_.length).distinct.size != 1) c.abort(c.enclosingPosition,"rows of matrix do not have the same length") val matrixAST = matrix map (_ map (i => Literal(Constant(i)))) map (i => toArrayAST(i.toList)) toArrayAST(matrixAST.toList) } } c.Expr(matrix getOrElse c.abort(c.enclosingPosition,"not a matrix of Int")) } } 用法与: scala> import MatrixMacro._ import MatrixMacro._ scala> matrix"1" res86: Array[Array[Int]] = Array(Array(1)) scala> matrix"1,2,3" res87: Array[Array[Int]] = Array(Array(1,3)) scala> matrix""" | 1,3 | 4,6 | 7,9 | """ res88: Array[Array[Int]] = Array(Array(1,3),Array(4,6),Array(7,9)) scala> matrix""" | 1,2 | 1 | """ <console>:57: error: rows of matrix do not have the same length matrix""" ^ scala> matrix"a" <console>:57: error: not a matrix of Int matrix"a" ^ 我不认为你会缩短。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |