元组在地图操作中开箱 [英] Tuple Unpacking in Map Operations

查看:120
本文介绍了元组在地图操作中开箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现自己与元组的列表,Seqs和迭代器一起工作,并且想做如下的操作:

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?

推荐答案

一个工作是使用 case

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

这篇关于元组在地图操作中开箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆