了解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
最满意答案
使用GUID。 这“几乎”是独一无二的。 随机可能导致两次相同的值。
Use a GUID. This is "almost" unique. Random could result in same value twice.
相关问答
更多-
我仍然不知道出了什么问题,但我只是将回购重新克隆到较早的状态,并重新进行链接文章中所述的必要更改。 现在它可以工作。 一定在某个地方犯了一个错误。 谢谢您的帮助。 I still don't know what was wrong but I just re-cloned the repo to an earlier state and remade the necessary changes as described in the linked article. Now it works. Must've ...
-
本地临时表(以#开头)仅限于您的会话; 其他会话,甚至从同一个用户/连接字符串,看不到它们。 生命周期的规则取决于本地临时表是否在存储过程中创建: 当过程结束时,删除在存储过程中创建的本地临时表; 其他存储过程或调用进程看不到它们。 会话结束时,其他本地临时表将被丢弃。 全局临时表(以##开头)在会话之间共享。 他们被丢弃时: 创建它们的会话结束 没有其他会话是指他们 此命令可以方便查看哪些临时表存在: select TABLE_NAME from tempdb.information_schema.tab ...
-
回应你的评论: 通过在我的操作中添加[Authorize(User="SomeUser")] ,只允许特定的硬编码用户输入。 但是,如何创建数据的用户如何才能被授权。 对于它,当前用户ID和数据创建者用户ID应该匹配。很多像站点用户仪表板只能由创建它的用户访问。 MVC是否提供此类授权? 请指教 你是正确的,注意Authorize属性,像.NET中的所有属性,只能有const参数。 为了灵活性,您不能在这种情况下使用属性,您必须实现自己的授权逻辑并执行来自控制器操作的调用,如下所示: public Acti ...
-
使用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
-
我不愿意为每个用户添加一个引号表,因为每次添加客户时都可能遇到需要更新app / run迁移的问题。 首先,您是否真的需要将自动编号ID公开为唯一键? 你可能会更好地看一下可能在URL中的某种短随机字符串 - 例如quotes.com/quote/m2dhfy? 您可以在应用程序中生成它并将其用作查找键(我还维护一个自动编号标识列)。 其次,如果您确实需要这些顺序ID,则需要具有UserId和QuoteID的复合键。 在您的URL中,您可以使用quotes.com/client1/1并在UserID和Quo ...
-
如果我理解正确,即使他没有“管理员”或“编辑者”的角色,也希望让撰写新闻,文章的用户编辑他自己的新闻或文章。 那是一个棘手的问题,简单的解决方案是: 让你的CustomAuthorize,因为它是,但让它继续到行动,而不是返回一个错误视图或东西只是注入一个行动参数即: CustomAuthorize: //..Your Role Validation Logic Here... if (filterContext.ActionParameters.Keys.Contains("isAuthorize") ...
-
好吧,我明白了。 我没有意识到我需要做的就是将类别添加到该对象的实例中。 dateToCreate.Categories.Add(keyDateRepository.GetCategory(topic)); dateToCreate.Categories.Add(keyDateRepository.GetCategory(level)); dateToCreate.Categories.Add(keyDateRepository.GetCategory(subject)); dateToCreate.Cat ...
-
对于您的短语与单词问题,为什么不使用通配符和LIKE运算符? Select Count(*) from temptable where SearchPhrase LIKE '%Apple%' After a little poking around, it looks like SQL Server 2008's Full Text Search is what I would want to use. I'm not 100% sure yet, but it looks promising. ht ...
-
您可以在使用UserId作为外键的模型中添加User的导航属性。 查询该特定项目时包括用户详细信息。 在你的部门模型中说 public class Department { public int Id { get; set; } public string Name { get; set; } public ApplicationUser User { get; set; } //Navigation Property public s ...
-
在表中添加uniqueidentifier列,默认值为NEWID()。 这将确保每列获得一个新的唯一标识符,该标识符不是增量的。 CREATE TABLE MyTable ( ... PIN uniqueidentifier NOT NULL DEFAULT newid() ... ) uniqueidentifier保证是唯一的,不仅适用于此表,也适用于所有表。 如果它对于您的应用程序而言太大,您可以从此数字中获得较小的PIN,您可以这样做: SELECT RIGHT(REPLA ...