加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Scala- zip with future

发布时间:2020-12-16 18:13:15 所属栏目:安全 来源:网络整理
导读:以下是我想要了解的代码: object Tryouts extends App{ val studentIds= Future{ List("s1","s2","s3") } val details = studentIds zip(Future{List("Tim","Joe","Fin")}).map(x=x.tail) details.foreach(println) Thread.sleep(1000)} 问题: val details
以下是我想要了解的代码:

object Tryouts extends App{
    val studentIds= Future{
        List("s1","s2","s3")
    }
    val details = studentIds zip(Future{List("Tim","Joe","Fin")}).map(x=>x.tail)
    details.foreach(println)
    Thread.sleep(1000)

}

问题:

val details = studentIds
zip(Future{List(“Tim”,”Joe”,”Fin”)}).map(x=>x.tail)

在这里,如果你注意到,我没有使用“.”拉链之前,只是给了一个空间.我猜可能是 .和空间都以相同的方式工作,并验证了一些堆栈溢出问题.在应用地图之前的上述表达式将导致Future [(List [String],List [String])].所以,当我说

.map(x=x.tail) should show compilation error in IDE because tail operation can be applied only on list and not for tuple. But it is actually executing successfully.

The same statement when executed with “.” before zip function like this:

val details = studentIds.zip(Future{List(“Tim”,”Fin”)}).map(x=>x.tail) the map(x=>x.tail) gives error.

可能是什么原因?

解决方法

当你省略空格(通过替换.)时,你也必须省略括号,否则编译器将考虑作为初始表达式的一部分后面的任何内容 – 在你的情况下,map(x => x.tail)将是适用于未来{List(“Tim”,“Joe”,“Fin”)}.

这里可以看到一个简单的例子:

val y = 3 to(5).toDouble

#toDouble实际应用于数字5.如果您尝试使用范围定义的方法,它将无法工作.

回到你的代码,如果你删除.在尾调用之前,您将得到预期的编译错误:

val details = ids zip Future.successful(List("Tim","Fin")) map (_.tail)
// compile error: "Cannot resolve symbol tail"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读