【问题标题】:How to map Result<'a,'b> to Reader<Result<'a,'b> in f#?如何在 f# 中将 Result<'a,'b> 映射到 Reader<Result<'a,'b>?
【发布时间】:2018-08-17 05:35:27
【问题描述】:

我有将函数映射到“Reader-Result”的函数,其中 f 是 'a->'b: ('a->'b) -> Reader<Result<'a,'c>> -> Reader<Result<'b,'c>>

let map f = Reader.map <| Result.map f

但是如何编写一个以函数'a-&gt;Result&lt;'b,'c&gt; 为输入的类似映射呢?

【问题讨论】:

    标签: f# reader-monad


    【解决方案1】:

    类似于map,但其参数返回Result&lt;_,_&gt; 的函数称为bind。它的签名是:

    bind : ('a -> Result<'b, 'c>) -> Result<'a, 'c> -> Result<'b, 'c>
    

    我假设你想要的签名是:

    yourFunction : ('a -> Result<'b, 'c>) -> Reader<Result<'a, 'c>> -> Reader<Result<'b, 'c>>
    

    要获得这样的功能,请将Result.bindReader.map结合起来:

    yourFunction f = Reader.map <| Result.bind f
    

    【讨论】:

    • 当然,当我看到它时很明显!正是我想要的,谢谢!
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多