If statement 将Javascript if/else转换为Ramda cond问题
在if/else逻辑检查后,我发现处理上存在一些问题。让我们举个例子 基本配置If statement 将Javascript if/else转换为Ramda cond问题,if-statement,ramda.js,If Statement,Ramda.js,在if/else逻辑检查后,我发现处理上存在一些问题。让我们举个例子 基本配置 const result = { data: 1, state: { pass: 'N' } } 对于JS if/else检查,逻辑检查后不应显示日志 function checking() { if(result.state.pass !== 'Y') { return result; } console.log("Should not appear the log")
const result = {
data: 1,
state: {
pass: 'N'
}
}
对于JS if/else检查,逻辑检查后不应显示日志
function checking() {
if(result.state.pass !== 'Y') {
return result;
}
console.log("Should not appear the log")
}
checking()
然后,我尝试使用Ramda cond函数来翻译它
function checking() {
R.cond([
[
R.compose(R.not, R.equals('Y'), R.prop('pass'), R.prop('state')),
(res) => res
]
])(result)
console.log("Should not appear the log")
}
checking()
但是,日志显示在Ramda cond示例中。我可以知道吗
- 有什么问题吗
- 关于这个例子,我有什么可以加强的吗
谢谢。您忘了添加退货声明
const结果={
数据:1,
声明:{
传球:“N”
}
}
函数检查(){
//需要在下面添加返回语句
返回R.cond([
[
R.compose(
R.not,
R等于('Y'),
R.道具(“通行证”),
R.prop(“州”),
(res)=>res
]
])(结果)
console.log(“不应出现在日志中”)
}
检查()
您忘记添加返回语句
const结果={
数据:1,
声明:{
传球:“N”
}
}
函数检查(){
//需要在下面添加返回语句
返回R.cond([
[
R.compose(
R.not,
R等于('Y'),
R.道具(“通行证”),
R.prop(“州”),
(res)=>res
]
])(结果)
console.log(“不应出现在日志中”)
}
检查()
更新了:我最初是反向阅读这篇关于日志应该显示的时间的文章的
我的建议如下:
const checking = R.when(
R.compose(R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
下面是我如何从您的代码中获得这些信息的:我的第一个任务是解决cond
的使用问题:
const result1 = {data: 1, state: {pass: 'N'}}
const result2 = {data: 2, state: {pass: 'Y'}}
const checking = R.cond([
[
R.compose(R.not, R.equals('Y'), R.prop('pass'), R.prop('state')),
R.identity
],
[
R.T,
(res) => {console.log("Should not appear in the log"); return res;}
]
])
checking(result1);
//=> {data: 1, state: {pass: 'N'}}
checking(result2);
// logs "Should not appear in the log
//=> {data: 2, state: {pass: 'Y'}}
请注意,cond
最像一个switch
语句:它获取一组条件结果对,并返回一个函数,该函数将其参数传递给每一对,直到找到一个条件为真的条件,然后返回调用其结果。因此,对于第二个条件,我们只需检查R.T
,这是一个始终返回true
的函数,并使用identity
返回输入
这个新函数现在接受一个结果
对象,并将其原封不动地返回,如果它与初始测试不匹配,则向控制台记录一条消息
但这并不是结束。这段代码可以重构
下面是一个简单的修复方法:
const checking = R.cond([
[
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
R.identity
],
[
R.T,
(res) => {console.log("Should not appear in the log"); return res;}
]
])
这只是从compose(prop('pass')、prop('state'))
更改为path(['state','pass'])
。这是一个小的调整,但我认为这更干净
下一个变化更具实质性
const checking = R.ifElse(
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
R.identity,
(res) => {console.log("Should not appear in the log"); return res;}
)
当我们有一个只有两个分支的cond
语句,而第二个分支在R.T
上测试时,我们可以使用ifElse
将其写得更清楚。这需要一个条件和两个结果,一个用于条件通过时,一个用于条件失败时
这可能是您想要达到的程度,特别是如果您最终计划在失败条件下做一些不同的事情。但是如果您不是,并且您确实只需要一个结果,那么R。除非
对ifElse
提供了进一步的简化,对于第二个结果只是传递的情况:
const checking = R.unless(
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
除非
只检查条件,如果条件为false,则运行结果,如果条件为true,则返回完整的输入
但是我们也可以通过从切换到来拉出而不是,除非在时切换到:
const checking = R.when(
R.compose(R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
我可能会把它放在那里,尽管为了清晰起见,我可能会考虑一个log
函数
所有这些都可以在更新的上获得(包括带有日志功能的最终版本):我最初是反向阅读这篇关于日志应该显示的时间的文章的
我的建议如下:
const checking = R.when(
R.compose(R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
下面是我如何从您的代码中获得这些信息的:我的第一个任务是解决cond
的使用问题:
const result1 = {data: 1, state: {pass: 'N'}}
const result2 = {data: 2, state: {pass: 'Y'}}
const checking = R.cond([
[
R.compose(R.not, R.equals('Y'), R.prop('pass'), R.prop('state')),
R.identity
],
[
R.T,
(res) => {console.log("Should not appear in the log"); return res;}
]
])
checking(result1);
//=> {data: 1, state: {pass: 'N'}}
checking(result2);
// logs "Should not appear in the log
//=> {data: 2, state: {pass: 'Y'}}
请注意,cond
最像一个switch
语句:它获取一组条件结果对,并返回一个函数,该函数将其参数传递给每一对,直到找到一个条件为真的条件,然后返回调用其结果。因此,对于第二个条件,我们只需检查R.T
,这是一个始终返回true
的函数,并使用identity
返回输入
这个新函数现在接受一个结果
对象,并将其原封不动地返回,如果它与初始测试不匹配,则向控制台记录一条消息
但这并不是结束。这段代码可以重构
下面是一个简单的修复方法:
const checking = R.cond([
[
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
R.identity
],
[
R.T,
(res) => {console.log("Should not appear in the log"); return res;}
]
])
这只是从compose(prop('pass')、prop('state'))
更改为path(['state','pass'])
。这是一个小的调整,但我认为这更干净
下一个变化更具实质性
const checking = R.ifElse(
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
R.identity,
(res) => {console.log("Should not appear in the log"); return res;}
)
当我们有一个只有两个分支的cond
语句,而第二个分支在R.T
上测试时,我们可以使用ifElse
将其写得更清楚。这需要一个条件和两个结果,一个用于条件通过时,一个用于条件失败时
这可能是您想要达到的程度,特别是如果您最终计划在失败条件下做一些不同的事情。但是如果您不是,并且您确实只需要一个结果,那么R。除非
对ifElse
提供了进一步的简化,对于第二个结果只是传递的情况:
const checking = R.unless(
R.compose(R.not, R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
除非
只检查条件,如果条件为false,则运行结果,如果条件为true,则返回完整的输入
但是我们也可以通过从切换到来拉出而不是,除非在时切换到:
const checking = R.when(
R.compose(R.equals('Y'), R.path(['state', 'pass'])),
(res) => {console.log("Should not appear in the log"); return res;}
)
我可能会把它放在那里,尽管为了清晰起见,我可能会考虑一个log
函数
所有这一切都是一场灾难