我仍然对函数式编程一窍不通,并试图找出Monads的一个卷曲问题。我遇到的情况是,为了论证,将来会有一个HTTP请求并返回值列表。然后,我希望能够检查该列表中是否存在特定值,我本以为在这里返回Maybe monad将是胜利。但是,如果存在该值,则我希望能够基于该返回值发出另一个HTTP请求,但只有在该值存在的情况下它才应运行。这是一个使用Future&Maybe https://codesandbox.io/s/2xvy3m1qmy
的ramda-fantasy实现的示例我很可能将整个事情弄错了,因此,如果您需要更多信息,或者需要更改以上任何内容,请坚持下去。只是为了澄清这就是我的伪代码工作流程:
这是最初的,经过评论的尝试,如上所述:
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)
答案 0 :(得分:0)
您必须寻找sequence
operations,它可以将Maybe<Future<…>>
变成Future<Maybe<…>>
,然后可以加入您的外部未来。
使用traverse
:
getVal('one')
.chain(traverse(Future.of, getValueData))
.fork(console.error, console.log)
答案 1 :(得分:0)
设法解决了我自己的问题,部分原因是Bergi的建议。 Bergi在上面的回答非常接近,但是实现并不十分正确。问题是我试图处理一个Future,然后将其更改为Maybe,所以我用以下方法处理它:
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)