简体   繁体   English

在 Scala 中映射二维字符列表

[英]Mapping a 2d Char list in Scala

So I'm trying to recreate this code with Scala.所以我试图用 Scala 重新创建这段代码。 This is what I have so far:这是我到目前为止:

/*
This is a program for the #259 [Easy] Clarence the Slow Typist /r/dailyprogrammer challenge
The algorithm was created by /u/derision.
*/
object Clarence {
    def main(args: Array[String]) {
        val ip = args(0)
        println(ip.tail.zip(ip).map(_.map(x => "123456789.0".indexOf(x))))
    }
}

This should produce a 2d array, but when I run it I get the following error:这应该会产生一个二维数组,但是当我运行它时,我收到以下错误:

scala Clarence.scala 219.45.143.143                                                                                                                    
/home/mattjone/sw/coding/learning-scala/Clarence.scala:8: error: value productElements is not a member of (Char, Char)
        println(ip.tail.zip(ip).map(_.map(x => "123456789.0".indexOf(x))))
                                      ^
one error found

From what I can tell it's saying that you can't use .map on a array of characters, but that doesn't make sense.据我所知,您不能在字符数组上使用 .map ,但这没有意义。 How should I be doing this?我该怎么做?

It says that map is not a member of (Char, Char) , which is a Tuple , not an array.它说map不是(Char, Char)的成员,它是一个Tuple ,而不是一个数组。 If you extract ip.tail.zip(ip) into a var , you can see it's type is Seq[(Char, Char)] :如果您将ip.tail.zip(ip)提取到var ,您可以看到它的类型是Seq[(Char, Char)]

val zip: IndexedSeq[(Char, Char)] = ip.tail.zip(ip)   

You can solve this by transforming each tuple into a sequence (with size 2), for example using productIterator (but there are other ways):您可以通过将每个元组转换为一个序列(大小为 2)来解决这个问题,例如使用productIterator (但还有其他方法):

println(ip.tail.zip(ip).map(_.productIterator.toList.map(x => "123456789.0".indexOf(x))))

That would result in printing a 2D "array" (actually a Vector of List s).这将导致打印一个二维“数组”(实际上是ListVector )。

Perhaps a more readable version would be one that prints a list of Tuples, and names each part of the tuple for clarity:也许更易读的版本是打印元组列表,并为清楚起见命名元组的每个部分:

// this will be applied to each part of the tuple:
def transformChar(c: Char): Int = "123456789.0".indexOf(c)
// map each tuple to a new transformed tuple:
println(ip.tail.zip(ip).map { 
  case (prev, cur) => (transformChar(prev), transformChar(cur))
})

Tzach Zohar answered you about why map don't worked. Tzach Zohar 回答了您为什么map不起作用。

My codes may be not relate to that issue, but if you want to create 2D Char list in Scala, how about this solution?我的代码可能与该问题无关,但是如果您想在 Scala 中创建 2D Char 列表,此解决方案如何?

val x = "123456789.0".toList.sliding(2).map(_.reverse).toList

This code will transform from "12345..." to List(List(2, 1), List(3, 2), List(4, 3)...) ( List[List[Char]] )此代码将从“12345...”转换为 List(List(2, 1), List(3, 2), List(4, 3)...) ( List[List[Char]] )

But if you want the list members to be Tuple2 , you can use this code instead:但是,如果您希望列表成员为Tuple2 ,则可以改用以下代码:

val x = "123456789.0".toList.sliding(2).map(x => (x.last, x.head)).toList

and it will generate List((2,1), (3,2), (4,3), ...) ( List[(Char, Char)] ).它将生成 List((2,1), (3,2), (4,3), ...) ( List[(Char, Char)] )。 Then you can just print it later :)然后你可以稍后打印它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM