我仍在掌握函数式编程的诀窍,并试图找出一些关于Monad的卷曲问题。在这种情况下,为了论证起见,我有一个发出HTTP请求并返回值列表的未来。然后我想要能够检查这个列表中是否存在一个特定的值,我会认为返回一个可能的单数就是胜利。但是,如果存在该值,我希望能够根据返回的值发出另一个HTTP请求,但只有当该值存在时,它才会运行。下面是一个使用ramda的例子--未来的幻想实现&也许是https://codesandbox.io/s/2xvy3m1qmy
我很可能把这整件事搞错了,所以如果你需要更多的信息,或者需要改变上面的任何一个,那就去做吧。为了澄清这一点,我在伪代码中使用了以下工作流:
下面是一个初步的、评论性的尝试,如上面所链接的:
import R from "ramda";
import Fantasy from "ramda-fantasy";
const Future = Fantasy.Future;
const Maybe = Fantasy.Maybe;
// make a fake HTTP request and return a list of values
// response :: Future Array String
const response = Future.of(['one', 'two', 'three'])
const maybeOrNothing = val => val ? Maybe.Just(val) : Maybe.Nothing()
// Maybe return our given value
// getVal :: String -> Maybe String
const getVal = input => response.map(R.find(R.equals(input))).map(maybeOrNothing)
// make another fake request
// let's pretend that this takes an ID and makes a fake ajax request to get teh data
// getValueData :: String -> Future
const getValueData = id => Future((reject, resolve) => {
// fake HTTP request
setTimeout(() => {
resolve({
id: id,
foo: 'bar'
})
}, 100)
})
// 'one' should then run getValueData
// something isn't right here, do I need to map then chain?
getVal('one')
.chain(getValueData)
.fork(console.error, console.log)
// 'five' isn't there, so shouldn't run getValueData
// something isn't right here as getValueData still runs
// map(R.chain) works to not run getValueData but then it causes issues later on
getVal('five')
.chain(getValueData)
.fork(console.error, console.log)发布于 2018-11-12 22:27:40
您一定是在寻找能够将 operations转换为Future<Maybe<…>>的Maybe<Future<…>>,然后您可以加入到您的外部未来。
getVal('one')
.chain(traverse(Future.of, getValueData))
.fork(console.error, console.log)发布于 2018-11-13 19:18:27
设法解决了我自己的问题,部分是因为Bergi的建议。Bergi上面的回答非常接近,但实现并不完全正确。问题是,我试图处理一个未来,然后将它改为一个可能的未来,所以我处理了以下几个问题:
import R from "ramda";
import Fantasy from "ramda-fantasy";
const Future = Fantasy.Future;
const Maybe = Fantasy.Maybe;
// make a fake HTTP request and return a list of values
// response :: Future Array String
const response = Future.of(['one', 'two', 'three'])
const maybeOrNothing = val => val ? Maybe.Just(val) : Maybe.Nothing()
const maybeToFuture = m => Maybe.isNothing(m) ? Future.reject() : Future.of(m.value)
// Maybe return our given value
// getVal :: String -> Maybe String
const getVal = input => response.map(R.find(R.equals(input))).map(maybeOrNothing)
// make another fake request
// let's pretend that this takes an ID and makes a fake ajax request to get teh data
// getValueData :: String -> Future
const getValueData = id => Future((reject, resolve) => {
// fake HTTP request
setTimeout(() => {
resolve({
id: id,
foo: 'bar'
})
}, 100)
})
// 'one' should then run getValueData
getVal('one')
.chain(maybeToFuture)
.chain(getValueData)
.fork(console.error, console.log)
// five isn't there so getValueData doesn't run
getVal('five')
.chain(maybeToFuture)
.chain(getValueData)
.fork(console.error, console.log)https://stackoverflow.com/questions/53269025
复制相似问题