作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
= Some [1] 我想将它转换成一个 List。 有公-6ren">
假设我有类似的东西
#r @"nuget: FSharpPlus"
open FSharpPlus
let maybeXs: Option<List<int>> = Some [1]
我想将它转换成一个 List
有公式化的机制吗?
let unit = Some
let almostThere: Option<List<Option<int>>> = map (fun xs -> map unit xs) maybeXs
我只需要“消除”外部选项....然后它似乎特定于我的数据类型所以
let there: List<Option<int>> =
fold (fun s t -> t) [] (map (fun xs -> map unit xs) maybeXs)
一般来说,这是一个公平的方法吗?还是有一些功能性的神奇配方,可以带来一些清晰度?
最佳答案
每当你有一堆 Functor
,并且外部 Functor
是 Traversable
时,遍历 会翻转命令。您可以使用 sequence
简单地翻转两者:
let there = sequence maybeXs
这会产生 [Some 1]
。
关于functional-programming - 颠倒仿函数顺序的一般方法(一个可折叠的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68489354/
我是一名优秀的程序员,十分优秀!