Scala flatMap,ms和e是什么? [英] Scala flatMap, what are ms and e?

查看:78
本文介绍了Scala flatMap,ms和e是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Scala,并在下面的代码中使用flatMap(摘自99个Scala问题)

I'm learning Scala and in the below code using flatMap (taken from 99 Scala problems) I have

def myflatten(somelist : List[Any]): List[Any] = somelist flatMap {
    case ms: List[_] => flatten(ms)
    case e => List(e)
}

现在我完全迷惑了"ms"和"e"是什么?他们必须是关键的东西,就好像我将ms改为dd一样,得到了错误未找到值ms".如果将鼠标悬停在它们上方,我会得到一个提示,提示"ms"是"List [] []","e"是"Any".

Now Im totally confused what the 'ms' and 'e' are? they must be something key as if I change ms to say dd, I get the error "not found value ms". If I hover over them i get a tool-tip saying 'ms' is a 'List[ ][ ]' and 'e' is 'Any'.

我尝试使用谷歌搜索"Scala列表元素ms和e"或"Scala flatMap ms和e"或"Scala case ms和case e是什么意思?"但我似乎找不到任何能解释这些'ms'和'e'的东西.

I've tried googling for "Scala List elements ms and e" or "Scala flatMap ms and e" or "Scala what do case ms and case e mean?" but I have not seem to find anything that explains what these 'ms' and 'e' are.

它们是关键字吗?在Scala文档中哪里可以找到有关它们的信息?

Are they keywords? Where can I find info on them in the Scala documentation?

P.S.我了解代码,但不知道ms和e是什么

P.S. I understand the code, but just not what ms and e are

推荐答案

如果您是新手,请让我一步一步地说明这里的情况.

If you're new, let me explain step by step whats going on here.

def myflatten(somelist : List[Any]): List[Any] = somelist flatMap {
    case ms: List[_] => flatten(ms)
    case e => List(e)
}

冗长的文字:

def myflatten(somelist : List[Any]): List[Any] = somelist flatMap { something =>
    something match {
      case ms: List[_] => flatten(ms)
      case e => List(e) 
    }
}

什么是mse?

首先让我解释一下.

您正在将平面映射到List[Any].

val x = List("Str1", "Str2", "Str3")
val y = x flatMap { elementInList => List(elementInList) }

这意味着对于列表中的每个元素,您正在创建一个包含该元素的新列表.因为它的flatMap本质上是返回相同(相同元素)的列表.

This means for each element in the list, you are creating a new List with that element in it. Because its a flatMap you essentially get the same (same elements) list back.

签出如果使用map而不是flatMap会发生什么:

Checkout what would happen if you use map instead of flatMap:

val x = List("Str1", "Str2", "Str3")
val y = x map { elementInList => List(elementInList) }

val y为:

List(List("Str1"), List("Str2"), List("Str3"))

签出 http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/

现在,看一下详细示例:

now, looking at the verbose example:

def myflatten(somelist : List[Any]): List[Any] = somelist flatMap { something =>
    something match {
      case ms: List[_] => flatten(ms)
      case e => List(e) 
    }
}

您正在匹配列表中的元素,在本例中为something. 您还匹配元素type.

you are matching on the element in the list or in this case called something. you are also matching on the elements type.

例如

def myMatch(e: Any): String = {
  e match {
    case x: String  => "Its a String: " + x
    case y: Int     => "Its a Int: " + y
    //notice how im using x twice.. its because they're in separate scope!
    case x: Boolean => "Its a Boolean: " + x
    case _          => "Its something else!"
  }
}

使用参数"hello"调用myMatch,它将返回"Its a String: hello".

invoke myMatch with the param "hello" and it will return "Its a String: hello".

使用参数1调用myMatch,它将返回"Its a Int: 1".

invoke myMatch with the param 1 and it will return "Its a Int: 1".

签出 http://docs.scala-lang.org/tutorials/tour/pattern-matching.html

什么是ems?

让我们看看您的代码:

def myflatten(somelist : List[Any]): List[Any] = somelist flatMap {
    case ms: List[_] => flatten(ms)
    case e => List(e)
}

如果我们当前正在查看的列表中的元素的类型为List[_](与List[Any]相同),则我们执行此块flatten(ms). ms是匹配后分配给元素的val.

If the element in the list which we are currently looking at is of the type List[_] (the same as List[Any]), we then execute this block, flatten(ms). ms is the val assigned to the element once matched.

如果我们当前正在查看的列表中的元素的类型为_或本质上是默认值(case _ =>),则返回包含一个元素的List(e),这是我们正在查看的元素.

If the element in the list which we are currently looking at is of the type _ or essentially default (case _ =>) then return a List(e) containing one element, which is the element which we were looking at.

这将始终返回List[List[Any]].然后将其展平为List[Any].

This would always return a List[List[Any]]. Which is then flattened to a List[Any].

我希望这会有所帮助,

Rhys

这篇关于Scala flatMap,ms和e是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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