基于元素的Scala3中通用2D阵列文字的良好实现是什么?

发布于 2025-01-22 03:57:16 字数 3823 浏览 5 评论 0原文

以下代码是一种未整理的方式来证明在scala3中声明2D数组文字的方法。它基于对一个相关问题的答案,该答案实现了scala2

https://stackover.com/a /13863525/666886

下面的非元素代码提供了干净的阵列文字声明,但wrt类型和维度不灵活。最好从元组和行数中得出数组尺寸。

#!/usr/bin/env scala3
object Array2d {
  def main(args: Array[String]): Unit = {
    prettyPrintArray()
  }

  type X=Int
  type Tuple26 = (X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X)
  type Array2d = Array[Array[X]]

  def apply(tuples: Tuple26 *): Array2d = {
    for {
      tupe <- tuples
      row = for ( i <- tupe.toList ) yield i
    } yield row.toArray
  }.toArray

  lazy val letterFrequencies = Array2d(
  (46,615,763,839,1745,325,628,651,1011,128,573,1319,797,1123,884,726,49,1642,2241,1162,631,299,408,97,659,184),
  (15,103,128,202,597,49,126,107,358,32,123,321,171,226,483,38,6,439,565,233,340,21,58,21,227,40),
  (63,128,106,218,689,86,75,407,526,14,241,369,197,307,627,208,5,507,675,343,361,60,66,24,226,28),
  (39,202,218,149,1257,108,183,175,634,35,140,407,212,418,692,194,10,596,763,272,409,97,184,52,348,46),
  (745,597,689,1257,919,396,568,583,1214,111,504,1315,726,1083,1217,763,34,1876,2323,1223,662,437,455,199,655,187),
  (25,49,86,108,396,118,75,72,295,11,68,258,58,129,248,26,7,299,415,208,205,14,50,20,151,24),
  (28,126,75,183,568,75,99,141,424,24,55,324,146,416,476,129,3,423,536,206,326,48,66,4,244,28),
  (51,107,407,175,583,72,141,53,399,21,145,282,198,238,488,174,6,342,715,454,285,44,158,14,207,23),
  (11,358,526,634,1214,295,424,399,169,71,392,853,518,884,594,467,50,970,1471,853,335,246,215,104,323,130),
  (28,32,14,35,111,11,24,21,71,2,31,41,28,52,98,26,1,45,112,33,75,9,12,1,38,3),
  (73,123,241,140,504,68,55,145,392,31,61,247,102,300,376,152,8,338,744,184,240,21,86,2,224,20),
  (319,321,369,407,1315,258,324,282,853,41,247,250,341,369,875,395,10,468,1237,491,566,173,201,61,463,50),
  (97,171,197,212,726,58,146,198,518,28,102,341,102,257,555,195,6,429,769,273,401,33,61,40,260,44),
  (123,226,307,418,1083,129,416,238,884,52,300,369,257,159,849,265,18,533,1027,537,499,113,220,45,374,65),
  (84,483,627,692,1217,248,476,488,594,98,376,875,555,849,525,566,18,1096,1658,856,462,175,329,85,521,130),
  (26,38,208,194,763,26,129,174,467,26,152,395,195,265,566,127,8,488,874,347,353,35,83,24,333,28),
  (9,6,5,10,34,7,3,6,50,1,8,10,6,18,18,8,1,16,37,25,96,0,2,0,7,1),
  (642,439,507,596,1876,299,423,342,970,45,338,468,429,533,1096,488,16,246,1461,789,673,189,234,52,491,77),
  (241,565,675,763,2323,415,536,715,1471,112,744,1237,769,1027,1658,874,37,1461,717,1367,1088,221,484,86,598,105),
  (162,233,343,272,1223,208,206,454,853,33,184,491,273,537,856,347,25,789,1367,254,548,72,202,49,384,52),
  (31,340,361,409,662,205,326,285,335,75,240,566,401,499,462,353,96,673,1088,548,73,61,41,32,317,51),
  (99,21,60,97,437,14,48,44,246,9,21,173,33,113,175,35,0,189,221,72,61,20,25,10,63,7),
  (8,58,66,184,455,50,66,158,215,12,86,201,61,220,329,83,2,234,484,202,41,25,11,12,133,19),
  (7,21,24,52,199,20,4,14,104,1,2,61,40,45,85,24,0,52,86,49,32,10,12,1,34,2),
  (59,227,226,348,655,151,244,207,323,38,224,463,260,374,521,333,7,491,598,384,317,63,133,34,43,54),
  (84,40,28,46,187,24,28,23,130,3,20,50,44,65,130,28,1,77,105,52,51,7,19,2,54,42),
  )

  def prettyPrintArray(): Unit = {
    val alphabet = "abcdefghijklmnopqrstuvwxyz"
    val toprow = alphabet.map { "%4s".format(_) }.mkString(",")
    printf("//       %s\n", toprow)
    for (a <- alphabet){
      val freqs: Seq[String] = for {
        b <- alphabet
        (x,y) = (alphabet.indexOf(a), alphabet.indexOf(b))
        freq = letterFrequencies(x)(y)
      } yield "%4d".format(freq)
      printf("/* %s */ (%s),\n", a, freqs.mkString(","))
    }
  }
}

The following code is a proof-of-concept for an uncluttered way to declare 2D array literals in scala3. It's based on this answer to a related question, implemented for scala2:

https://stackoverflow.com/a/13863525/666886

The non-generic code below provides a clean Array literal declaration, but is inflexible w.r.t. type and dimension. It would be better to derive array dimensions from tuple arity and number of rows.

#!/usr/bin/env scala3
object Array2d {
  def main(args: Array[String]): Unit = {
    prettyPrintArray()
  }

  type X=Int
  type Tuple26 = (X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X)
  type Array2d = Array[Array[X]]

  def apply(tuples: Tuple26 *): Array2d = {
    for {
      tupe <- tuples
      row = for ( i <- tupe.toList ) yield i
    } yield row.toArray
  }.toArray

  lazy val letterFrequencies = Array2d(
  (46,615,763,839,1745,325,628,651,1011,128,573,1319,797,1123,884,726,49,1642,2241,1162,631,299,408,97,659,184),
  (15,103,128,202,597,49,126,107,358,32,123,321,171,226,483,38,6,439,565,233,340,21,58,21,227,40),
  (63,128,106,218,689,86,75,407,526,14,241,369,197,307,627,208,5,507,675,343,361,60,66,24,226,28),
  (39,202,218,149,1257,108,183,175,634,35,140,407,212,418,692,194,10,596,763,272,409,97,184,52,348,46),
  (745,597,689,1257,919,396,568,583,1214,111,504,1315,726,1083,1217,763,34,1876,2323,1223,662,437,455,199,655,187),
  (25,49,86,108,396,118,75,72,295,11,68,258,58,129,248,26,7,299,415,208,205,14,50,20,151,24),
  (28,126,75,183,568,75,99,141,424,24,55,324,146,416,476,129,3,423,536,206,326,48,66,4,244,28),
  (51,107,407,175,583,72,141,53,399,21,145,282,198,238,488,174,6,342,715,454,285,44,158,14,207,23),
  (11,358,526,634,1214,295,424,399,169,71,392,853,518,884,594,467,50,970,1471,853,335,246,215,104,323,130),
  (28,32,14,35,111,11,24,21,71,2,31,41,28,52,98,26,1,45,112,33,75,9,12,1,38,3),
  (73,123,241,140,504,68,55,145,392,31,61,247,102,300,376,152,8,338,744,184,240,21,86,2,224,20),
  (319,321,369,407,1315,258,324,282,853,41,247,250,341,369,875,395,10,468,1237,491,566,173,201,61,463,50),
  (97,171,197,212,726,58,146,198,518,28,102,341,102,257,555,195,6,429,769,273,401,33,61,40,260,44),
  (123,226,307,418,1083,129,416,238,884,52,300,369,257,159,849,265,18,533,1027,537,499,113,220,45,374,65),
  (84,483,627,692,1217,248,476,488,594,98,376,875,555,849,525,566,18,1096,1658,856,462,175,329,85,521,130),
  (26,38,208,194,763,26,129,174,467,26,152,395,195,265,566,127,8,488,874,347,353,35,83,24,333,28),
  (9,6,5,10,34,7,3,6,50,1,8,10,6,18,18,8,1,16,37,25,96,0,2,0,7,1),
  (642,439,507,596,1876,299,423,342,970,45,338,468,429,533,1096,488,16,246,1461,789,673,189,234,52,491,77),
  (241,565,675,763,2323,415,536,715,1471,112,744,1237,769,1027,1658,874,37,1461,717,1367,1088,221,484,86,598,105),
  (162,233,343,272,1223,208,206,454,853,33,184,491,273,537,856,347,25,789,1367,254,548,72,202,49,384,52),
  (31,340,361,409,662,205,326,285,335,75,240,566,401,499,462,353,96,673,1088,548,73,61,41,32,317,51),
  (99,21,60,97,437,14,48,44,246,9,21,173,33,113,175,35,0,189,221,72,61,20,25,10,63,7),
  (8,58,66,184,455,50,66,158,215,12,86,201,61,220,329,83,2,234,484,202,41,25,11,12,133,19),
  (7,21,24,52,199,20,4,14,104,1,2,61,40,45,85,24,0,52,86,49,32,10,12,1,34,2),
  (59,227,226,348,655,151,244,207,323,38,224,463,260,374,521,333,7,491,598,384,317,63,133,34,43,54),
  (84,40,28,46,187,24,28,23,130,3,20,50,44,65,130,28,1,77,105,52,51,7,19,2,54,42),
  )

  def prettyPrintArray(): Unit = {
    val alphabet = "abcdefghijklmnopqrstuvwxyz"
    val toprow = alphabet.map { "%4s".format(_) }.mkString(",")
    printf("//       %s\n", toprow)
    for (a <- alphabet){
      val freqs: Seq[String] = for {
        b <- alphabet
        (x,y) = (alphabet.indexOf(a), alphabet.indexOf(b))
        freq = letterFrequencies(x)(y)
      } yield "%4d".format(freq)
      printf("/* %s */ (%s),\n", a, freqs.mkString(","))
    }
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2025-01-29 03:57:16

以下作用,但是基于新的scala 3 type iarray的同一件事也很不错。

#!/usr/bin/env scala3

// Example of declaring a generic NxN Array[Array[_]] in scala3 by using
// scala 3 tuple extensions, for a concise declaration of a 2d array.
// An improved version would avoid the deprecated ClassTag reference,
// and the use of `asInstanceOf[List[T]]`.
// Perhaps `shapeless 3` would help in this regard?
object Array2d {
  def main(args: Array[String]): Unit = {
    prettyPrintArray()
  }

  // it would be nice if a Tuple with a single homogeneous field Type
  // automatically converted to an Array of that type, rather
  // than an Array[AnyRef]

  import scala.reflect.ClassTag
  def apply[T:ClassTag](tuples: Tuple *): Array[Array[T]] = {
    val rows: Seq[Array[T]] = for {
      tupe <- tuples
      row: Seq[T] = tupe.toList.asInstanceOf[List[T]]
      arr: Array[T] = (for (i:T <- row) yield i).toArray
    } yield arr
    rows.toArray
  }

  // this table is useful for analyzing nytimes Wordle guesses.
  lazy final val letterFrequencies = Array2d[Int](
    (46,615,763,839,1745,325,628,651,1011,128,573,1319,797,1123,884,726,49,1642,2241,1162,631,299,408,97,659,184),
    (15,103,128,202,597,49,126,107,358,32,123,321,171,226,483,38,6,439,565,233,340,21,58,21,227,40),
    (63,128,106,218,689,86,75,407,526,14,241,369,197,307,627,208,5,507,675,343,361,60,66,24,226,28),
    (39,202,218,149,1257,108,183,175,634,35,140,407,212,418,692,194,10,596,763,272,409,97,184,52,348,46),
    (745,597,689,1257,919,396,568,583,1214,111,504,1315,726,1083,1217,763,34,1876,2323,1223,662,437,455,199,655,187),
    (25,49,86,108,396,118,75,72,295,11,68,258,58,129,248,26,7,299,415,208,205,14,50,20,151,24),
    (28,126,75,183,568,75,99,141,424,24,55,324,146,416,476,129,3,423,536,206,326,48,66,4,244,28),
    (51,107,407,175,583,72,141,53,399,21,145,282,198,238,488,174,6,342,715,454,285,44,158,14,207,23),
    (11,358,526,634,1214,295,424,399,169,71,392,853,518,884,594,467,50,970,1471,853,335,246,215,104,323,130),
    (28,32,14,35,111,11,24,21,71,2,31,41,28,52,98,26,1,45,112,33,75,9,12,1,38,3),
    (73,123,241,140,504,68,55,145,392,31,61,247,102,300,376,152,8,338,744,184,240,21,86,2,224,20),
    (319,321,369,407,1315,258,324,282,853,41,247,250,341,369,875,395,10,468,1237,491,566,173,201,61,463,50),
    (97,171,197,212,726,58,146,198,518,28,102,341,102,257,555,195,6,429,769,273,401,33,61,40,260,44),
    (123,226,307,418,1083,129,416,238,884,52,300,369,257,159,849,265,18,533,1027,537,499,113,220,45,374,65),
    (84,483,627,692,1217,248,476,488,594,98,376,875,555,849,525,566,18,1096,1658,856,462,175,329,85,521,130),
    (26,38,208,194,763,26,129,174,467,26,152,395,195,265,566,127,8,488,874,347,353,35,83,24,333,28),
    (9,6,5,10,34,7,3,6,50,1,8,10,6,18,18,8,1,16,37,25,96,0,2,0,7,1),
    (642,439,507,596,1876,299,423,342,970,45,338,468,429,533,1096,488,16,246,1461,789,673,189,234,52,491,77),
    (241,565,675,763,2323,415,536,715,1471,112,744,1237,769,1027,1658,874,37,1461,717,1367,1088,221,484,86,598,105),
    (162,233,343,272,1223,208,206,454,853,33,184,491,273,537,856,347,25,789,1367,254,548,72,202,49,384,52),
    (31,340,361,409,662,205,326,285,335,75,240,566,401,499,462,353,96,673,1088,548,73,61,41,32,317,51),
    (99,21,60,97,437,14,48,44,246,9,21,173,33,113,175,35,0,189,221,72,61,20,25,10,63,7),
    (8,58,66,184,455,50,66,158,215,12,86,201,61,220,329,83,2,234,484,202,41,25,11,12,133,19),
    (7,21,24,52,199,20,4,14,104,1,2,61,40,45,85,24,0,52,86,49,32,10,12,1,34,2),
    (59,227,226,348,655,151,244,207,323,38,224,463,260,374,521,333,7,491,598,384,317,63,133,34,43,54),
    (84,40,28,46,187,24,28,23,130,3,20,50,44,65,130,28,1,77,105,52,51,7,19,2,54,42),
  )

  def prettyPrintArray(): Unit = {
    val alphabet = "abcdefghijklmnopqrstuvwxyz"
    val toprow = alphabet.map { "%4s".format(_) }.mkString(",")
    printf("//       %s\n", toprow)
    for (a <- alphabet){
      val freqs: Seq[String] = for {
        b <- alphabet
        (x,y) = (alphabet.indexOf(a), alphabet.indexOf(b))
        freq = letterFrequencies(x)(y)
      } yield "%4d".format(freq)
      printf("/* %s */ (%s),\n", a, freqs.mkString(","))
    }
  }
}

The following works, but the same thing based on the new scala 3 type IArray would be nice to have as well.

#!/usr/bin/env scala3

// Example of declaring a generic NxN Array[Array[_]] in scala3 by using
// scala 3 tuple extensions, for a concise declaration of a 2d array.
// An improved version would avoid the deprecated ClassTag reference,
// and the use of `asInstanceOf[List[T]]`.
// Perhaps `shapeless 3` would help in this regard?
object Array2d {
  def main(args: Array[String]): Unit = {
    prettyPrintArray()
  }

  // it would be nice if a Tuple with a single homogeneous field Type
  // automatically converted to an Array of that type, rather
  // than an Array[AnyRef]

  import scala.reflect.ClassTag
  def apply[T:ClassTag](tuples: Tuple *): Array[Array[T]] = {
    val rows: Seq[Array[T]] = for {
      tupe <- tuples
      row: Seq[T] = tupe.toList.asInstanceOf[List[T]]
      arr: Array[T] = (for (i:T <- row) yield i).toArray
    } yield arr
    rows.toArray
  }

  // this table is useful for analyzing nytimes Wordle guesses.
  lazy final val letterFrequencies = Array2d[Int](
    (46,615,763,839,1745,325,628,651,1011,128,573,1319,797,1123,884,726,49,1642,2241,1162,631,299,408,97,659,184),
    (15,103,128,202,597,49,126,107,358,32,123,321,171,226,483,38,6,439,565,233,340,21,58,21,227,40),
    (63,128,106,218,689,86,75,407,526,14,241,369,197,307,627,208,5,507,675,343,361,60,66,24,226,28),
    (39,202,218,149,1257,108,183,175,634,35,140,407,212,418,692,194,10,596,763,272,409,97,184,52,348,46),
    (745,597,689,1257,919,396,568,583,1214,111,504,1315,726,1083,1217,763,34,1876,2323,1223,662,437,455,199,655,187),
    (25,49,86,108,396,118,75,72,295,11,68,258,58,129,248,26,7,299,415,208,205,14,50,20,151,24),
    (28,126,75,183,568,75,99,141,424,24,55,324,146,416,476,129,3,423,536,206,326,48,66,4,244,28),
    (51,107,407,175,583,72,141,53,399,21,145,282,198,238,488,174,6,342,715,454,285,44,158,14,207,23),
    (11,358,526,634,1214,295,424,399,169,71,392,853,518,884,594,467,50,970,1471,853,335,246,215,104,323,130),
    (28,32,14,35,111,11,24,21,71,2,31,41,28,52,98,26,1,45,112,33,75,9,12,1,38,3),
    (73,123,241,140,504,68,55,145,392,31,61,247,102,300,376,152,8,338,744,184,240,21,86,2,224,20),
    (319,321,369,407,1315,258,324,282,853,41,247,250,341,369,875,395,10,468,1237,491,566,173,201,61,463,50),
    (97,171,197,212,726,58,146,198,518,28,102,341,102,257,555,195,6,429,769,273,401,33,61,40,260,44),
    (123,226,307,418,1083,129,416,238,884,52,300,369,257,159,849,265,18,533,1027,537,499,113,220,45,374,65),
    (84,483,627,692,1217,248,476,488,594,98,376,875,555,849,525,566,18,1096,1658,856,462,175,329,85,521,130),
    (26,38,208,194,763,26,129,174,467,26,152,395,195,265,566,127,8,488,874,347,353,35,83,24,333,28),
    (9,6,5,10,34,7,3,6,50,1,8,10,6,18,18,8,1,16,37,25,96,0,2,0,7,1),
    (642,439,507,596,1876,299,423,342,970,45,338,468,429,533,1096,488,16,246,1461,789,673,189,234,52,491,77),
    (241,565,675,763,2323,415,536,715,1471,112,744,1237,769,1027,1658,874,37,1461,717,1367,1088,221,484,86,598,105),
    (162,233,343,272,1223,208,206,454,853,33,184,491,273,537,856,347,25,789,1367,254,548,72,202,49,384,52),
    (31,340,361,409,662,205,326,285,335,75,240,566,401,499,462,353,96,673,1088,548,73,61,41,32,317,51),
    (99,21,60,97,437,14,48,44,246,9,21,173,33,113,175,35,0,189,221,72,61,20,25,10,63,7),
    (8,58,66,184,455,50,66,158,215,12,86,201,61,220,329,83,2,234,484,202,41,25,11,12,133,19),
    (7,21,24,52,199,20,4,14,104,1,2,61,40,45,85,24,0,52,86,49,32,10,12,1,34,2),
    (59,227,226,348,655,151,244,207,323,38,224,463,260,374,521,333,7,491,598,384,317,63,133,34,43,54),
    (84,40,28,46,187,24,28,23,130,3,20,50,44,65,130,28,1,77,105,52,51,7,19,2,54,42),
  )

  def prettyPrintArray(): Unit = {
    val alphabet = "abcdefghijklmnopqrstuvwxyz"
    val toprow = alphabet.map { "%4s".format(_) }.mkString(",")
    printf("//       %s\n", toprow)
    for (a <- alphabet){
      val freqs: Seq[String] = for {
        b <- alphabet
        (x,y) = (alphabet.indexOf(a), alphabet.indexOf(b))
        freq = letterFrequencies(x)(y)
      } yield "%4d".format(freq)
      printf("/* %s */ (%s),\n", a, freqs.mkString(","))
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文