我正在使用Bluebird做异步操作,但现在必须做很多空/空/错误检查,我不想按照通常的Else路线.我正在考虑使用monad,但尚未彻底完成它.
此外,我希望它能与ramda一起使用,pipe / compose因为我的大多数其他代码都整齐地封装在功能管道中.根据许多 讨论,monadic Futures(Fluture似乎被推荐)比Promises更受欢迎,对pipeP的支持和composeP可能在未来版本中被删除.
Fluture似乎是一个不错的选择,因为据说它可以很好地适应那些坚持幻想 - 土地规格的图书馆(如ramda).
但是我完全迷失了如何实现将Ramda的管道与Fluture集成在一起的东西.我需要一些示例代码的帮助.
例如:
我有一个DB调用,返回一个对象数组.数组可能具有值,为空或未定义.我有一个功能管道,可以转换数据并将其返回到前端.
示例承诺代码:
fancyDBCall1(constraints)
.then(data => {
if (!data || data.length === 0) {
return []
}
return pipe(
...
transformation functions
...
)(data)
})
.then(res.ok)
.catch(res.serverError)
Run Code Online (Sandbox Code Playgroud)
有人可以就一个好的方法提出一些指示.
我已经安装conda在我的 Windows 10 环境中。当我尝试从我的 Ubuntu WSL 使用 conda 时,它抛出:
CommandNotFoundError:您的 shell 尚未正确配置为使用“conda activate”。如果您的 shell 是 Bash 或 Bourne 变体,请为当前用户启用 conda
$ echo ". C:\ProgramData\Miniconda3/etc/profile.d/conda.sh" >> ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我的 bash 在重新启动时抛出错误:
cygpath: command not found
-bash: /etc/profile.d/conda.sh: No such file or directory
我应该安装 Cygwin 以在 WSL 中使用 conda 吗?
在lodash中,我想将一个对象数组转换为一个包含每个属性数组的对象.
我有一个对象数组:
var students = [{
name: 'A',
idNo: 1,
marks: {
math: 98,
sci: 97,
eng: 89
}
}, {
name: 'B',
idNo: 2,
marks: {
math: 88,
sci: 87,
eng: 79
}
}, {
name: 'C',
idNo: 3,
marks: {
math: 87,
sci: 98,
eng: 91
}
}]
Run Code Online (Sandbox Code Playgroud)
我想像这样组合/重塑它们:
{
name: [A, B, C],
idNo: [1, 2, 3],
marks: [{
math: 98,
sci: 97,
eng: 89
}, {
math: 88,
sci: 87,
eng: 79
}, {
math: 87, …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的集合:
var scores = [{
name: foo,
score: 34
}, {
name: bar,
score: 23
}, {
name: baz,
score: 99
}]
Run Code Online (Sandbox Code Playgroud)
我还有一个包含名称的变量:
var selfName = 'bar';
Run Code Online (Sandbox Code Playgroud)
如何重新排列分数集合,使包含 selfName 的对象位于索引 0 处。预期结果:
[{
name: bar,
score: 23
}, {
name: foo,
score: 34
}, {
name: baz,
score: 99
}]
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过以下方式做到这一点:
var isSelfName = (selfName, score) => {
var _name = selfName;
return (score) => {
return score.name === selfName;
}
};
var result = _.filter(scores, isSelfName(selfName));
result.push(_.reject(scores, isSelfName(selfName)));
result …Run Code Online (Sandbox Code Playgroud) 我正在读这本书:"用Python构建机器学习系统".在Iris日期的分类中,我无法理解以下语法:
plt.scatter(features[target == t,0],
features[target == t,1],
marker=marker,
c=c)
Run Code Online (Sandbox Code Playgroud)
具体来说,features[target == t,0]实际意味着什么?
我想对从promise调用获得的结果进行一系列测试.我知道它可以做到:
it ('should pass test foo1', () => {
return promisecall()
.then(result => {
expect(result.length).to.equal(42)
expect(some other test on data)
expect(some other test on data)
.
.
})
})
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这样做是:运行单个测试(即应该通过测试foo1),只有在所有预期条件都为真时才会通过,并且这些测试期望部件不会显示在输出屏幕中.
如何在单个promise结果的结果上添加多个'it'/ unit测试,以便不同的测试用例实际显示在输出中?
说我有两个数组:
const data = [1, 2, 3, 4]
const predicateArray = [true, false, false, true]
Run Code Online (Sandbox Code Playgroud)
我希望返回值为:
[1, 4]
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了:
pipe(
zipWith((fst, scnd) => scnd ? fst : null)),
reject(isNil)
)(data, predicateArray)
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的/内置的方法来做到这一点?
最好使用Ramda中的解决方案。