首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在未来处理嵌套的,可能是单一的

在未来处理嵌套的,可能是单一的
EN

Stack Overflow用户
提问于 2018-11-12 19:41:51
回答 2查看 180关注 0票数 0

我仍在掌握函数式编程的诀窍,并试图找出一些关于Monad的卷曲问题。在这种情况下,为了论证起见,我有一个发出HTTP请求并返回值列表的未来。然后我想要能够检查这个列表中是否存在一个特定的值,我会认为返回一个可能的单数就是胜利。但是,如果存在该值,我希望能够根据返回的值发出另一个HTTP请求,但只有当该值存在时,它才会运行。下面是一个使用ramda的例子--未来的幻想实现&也许是https://codesandbox.io/s/2xvy3m1qmy

我很可能把这整件事搞错了,所以如果你需要更多的信息,或者需要改变上面的任何一个,那就去做吧。为了澄清这一点,我在伪代码中使用了以下工作流:

  1. 请求所有的值
  2. 筛选值,并可能找到匹配的值
  3. 如果找到,请请求获取其余的数据。

下面是一个初步的、评论性的尝试,如上面所链接的:

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

发布于 2018-11-12 22:27:40

您一定是在寻找能够将 operations转换为Future<Maybe<…>>Maybe<Future<…>>,然后您可以加入到您的外部未来。

traverse

代码语言:javascript
复制
getVal('one')
.chain(traverse(Future.of, getValueData))
.fork(console.error, console.log)
票数 0
EN

Stack Overflow用户

发布于 2018-11-13 19:18:27

设法解决了我自己的问题,部分是因为Bergi的建议。Bergi上面的回答非常接近,但实现并不完全正确。问题是,我试图处理一个未来,然后将它改为一个可能的未来,所以我处理了以下几个问题:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53269025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档