映射操作中的元组解包

发布于 2024-11-27 12:38:52 字数 533 浏览 3 评论 0原文

I frequently find myself working with Lists, Seqs, and Iterators of Tuples and would like to do something like the following,

val arrayOfTuples = List((1, "Two"), (3, "Four"))
arrayOfTuples.map { (e1: Int, e2: String) => e1.toString + e2 }

However, the compiler never seems to agree with this syntax. Instead, I end up writing,

arrayOfTuples.map { 
    t => 
    val e1 = t._1
    val e2 = t._2
    e1.toString + e2 
}

Which is just silly.我该如何解决这个问题?

I frequently find myself working with Lists, Seqs, and Iterators of Tuples and would like to do something like the following,

val arrayOfTuples = List((1, "Two"), (3, "Four"))
arrayOfTuples.map { (e1: Int, e2: String) => e1.toString + e2 }

However, the compiler never seems to agree with this syntax. Instead, I end up writing,

arrayOfTuples.map { 
    t => 
    val e1 = t._1
    val e2 = t._2
    e1.toString + e2 
}

Which is just silly. How can I get around this?

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

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

发布评论

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

评论(6

请恋爱 2024-12-04 12:38:52

解决方法是使用 case

arrayOfTuples map {case (e1: Int, e2: String) => e1.toString + e2}

A work around is to use case :

arrayOfTuples map {case (e1: Int, e2: String) => e1.toString + e2}
饮惑 2024-12-04 12:38:52

我喜欢元组函数;它既方便又重要的是,输入安全:

import Function.tupled
arrayOfTuples map tupled { (e1, e2) => e1.toString + e2 }

I like the tupled function; it's both convenient and not least, type safe:

import Function.tupled
arrayOfTuples map tupled { (e1, e2) => e1.toString + e2 }
天气好吗我好吗 2024-12-04 12:38:52

似乎

arrayOfTuples.map {t => t._1.toString + t._2 }

如果您多次需要参数,或者需要不同的顺序,或者在嵌套结构中,为什么不使用 _ 不起作用,

arrayOfTuples map {case (i, s) => i.toString + s} 

是一种简短但可读的形式。

Why don't you use

arrayOfTuples.map {t => t._1.toString + t._2 }

If you need the parameters multiple time, or different order, or in a nested structure, where _ doesn't work,

arrayOfTuples map {case (i, s) => i.toString + s} 

seems to be a short, but readable form.

泛泛之交 2024-12-04 12:38:52

另一种选择是

arrayOfTuples.map { 
    t => 
    val (e1,e2) = t
    e1.toString + e2
}

Another option is

arrayOfTuples.map { 
    t => 
    val (e1,e2) = t
    e1.toString + e2
}
遥远的绿洲 2024-12-04 12:38:52

从 Scala 3 开始,参数 untupling 已得到扩展,允许这样的语法:

// val tuples = List((1, "Two"), (3, "Four"))
tuples.map(_.toString + _)
// List[String] = List("1Two", "3Four")

其中每个 _ 指的是关联的元组部分。

Starting in Scala 3, parameter untupling has been extended, allowing such a syntax:

// val tuples = List((1, "Two"), (3, "Four"))
tuples.map(_.toString + _)
// List[String] = List("1Two", "3Four")

where each _ refers in order to the associated tuple part.

御守 2024-12-04 12:38:52

我认为 用于理解 是这里最自然的解决方案:

for ((e1, e2) <- arrayOfTuples) yield {
  e1.toString + e2
}

I think for comprehension is the most natural solution here:

for ((e1, e2) <- arrayOfTuples) yield {
  e1.toString + e2
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文