首页 \ 问答 \ 了解flatMap和Map(Understanding flatMap and Map)

了解flatMap和Map(Understanding flatMap and Map)

Scala函数式编程 (ISBN:978-1617290657)的练习4.4中,我们需要遍历选项列表,并将这些选项中的值连接成一个包含列表的选项。 如果初始列表包含无选项,则最终选项也应为无。

功能签名

def sequence[A](a: List[Option[A]]): Option[List[A]]

样品1

scala> sequence(List(Some(3), Some(5), Some(1)))
Option[List[Int]] = Some(List(3, 5, 1))

样品2

scala> sequence(List(Some(3), None, Some(1)))
Option[List[Int]] = None

这是我在网上找到的解决方案:

def sequence[A](a: List[Option[A]]): Option[List[A]] = a match {
  case Nil => Some(Nil)
  case h :: t => h flatMap (hh => sequence(t) map (hh :: _))
}

尽管我尽最大的努力来理解这个实现(通过在纸上写入函数跟踪),但我无法直观地理解这个函数。 我能够理解map和flatMap意味着什么,而不是在这个函数的上下文中。

是否有另一种方法来看问题,并从这个角度,直观地得出上述代码?


In exercise 4.4 of the book Functional Programming in Scala (ISBN: 978-1617290657), we need to run through a List of Options, and concatenate the values in those Options into a single Option containing a List. If the initial List contains a None Option, the final Option should be None as well.

Function Signature

def sequence[A](a: List[Option[A]]): Option[List[A]]

Sample 1

scala> sequence(List(Some(3), Some(5), Some(1)))
Option[List[Int]] = Some(List(3, 5, 1))

Sample 2

scala> sequence(List(Some(3), None, Some(1)))
Option[List[Int]] = None

This is a solution that I found online:

def sequence[A](a: List[Option[A]]): Option[List[A]] = a match {
  case Nil => Some(Nil)
  case h :: t => h flatMap (hh => sequence(t) map (hh :: _))
}

Despite my best efforts at understanding this implementation (by writing the function trace on paper), I'm unable to get an intuitive grasp of this function. I am able to understand what map and flatMap means in isolation, but not in the context of this function.

Is there another way to look at the problem, and through that perspective, derive the code above intuitively?


原文:https://stackoverflow.com/questions/48866303
更新时间:2023-09-15 13:09

最满意答案

使用GUID。 这“几乎”是独一无二的。 随机可能导致两次相同的值。

http://msdn.microsoft.com/en-us/library/system.guid.aspx


Use a GUID. This is "almost" unique. Random could result in same value twice.

http://msdn.microsoft.com/en-us/library/system.guid.aspx

相关问答

更多

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)