从第二个列表中删除出现在一个数组中的字符串(如果它们存在于 OCaml/ReasonML 中)
Remove Strings That Appear in one Array from second list if they exist in OCaml/ReasonML
我必须要像这样的日期数组:
let slots = [|
"2014-08-11T10:00:00-04:00",
"2014-08-11T10:30:00-04:00",
"2014-08-11T11:00:00-04:00",
"2014-08-11T11:30:00-04:00",
"2014-08-11T12:00:00-04:00",
"2014-08-11T12:30:00-04:00",
"2014-08-11T13:00:00-04:00"
|];
let badSlots = [|
"2014-08-11T11:00:00-04:00",
"2014-08-11T11:30:00-04:00",
"2014-08-11T12:00:00-04:00",
|];
如何从第一个数组中删除出现在第二个数组中的项目,以便结果为:
result [
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T13:00:00-04:00'
]
到目前为止,我已经尝试了这个,似乎找到了匹配项,但结果格式全错了。
let checkBool = s => Belt.Array.map(badSlots, bs => s !== bs);
let check = s =>
Belt.Array.keepMap(badSlots, bs =>
if (s !== bs) {
Some(s);
} else {
None;
}
);
let checkBoolResult = Belt.Array.map(slots, s => checkBool(s));
Js.log2("checkBoolResult", checkBoolResult);
let checkResult = Belt.Array.keepMap(slots, s => Some(check(s)));
Js.log2("checkResult", checkResult);
记录:
checkBoolResult [
[ true, true, true ],
[ true, true, true ],
[ false, true, true ],
[ true, false, true ],
[ true, true, false ],
[ true, true, true ],
[ true, true, true ]
]
checkResult [
[
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:00:00-04:00'
],
[
'2014-08-11T10:30:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T10:30:00-04:00'
],
[ '2014-08-11T11:00:00-04:00', '2014-08-11T11:00:00-04:00' ],
[ '2014-08-11T11:30:00-04:00', '2014-08-11T11:30:00-04:00' ],
[ '2014-08-11T12:00:00-04:00', '2014-08-11T12:00:00-04:00' ],
[
'2014-08-11T12:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T12:30:00-04:00'
],
[
'2014-08-11T13:00:00-04:00',
'2014-08-11T13:00:00-04:00',
'2014-08-11T13:00:00-04:00'
]
]
任何语法中的任何指导都将不胜感激。谢谢。
通过原因不和谐论坛 here:
此解决方案有效:
let x = Js.Array.filter(x => !Array.mem(x, badSlots), slots);
//output
x [
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T13:00:00-04:00'
]
我必须要像这样的日期数组:
let slots = [|
"2014-08-11T10:00:00-04:00",
"2014-08-11T10:30:00-04:00",
"2014-08-11T11:00:00-04:00",
"2014-08-11T11:30:00-04:00",
"2014-08-11T12:00:00-04:00",
"2014-08-11T12:30:00-04:00",
"2014-08-11T13:00:00-04:00"
|];
let badSlots = [|
"2014-08-11T11:00:00-04:00",
"2014-08-11T11:30:00-04:00",
"2014-08-11T12:00:00-04:00",
|];
如何从第一个数组中删除出现在第二个数组中的项目,以便结果为:
result [
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T13:00:00-04:00'
]
到目前为止,我已经尝试了这个,似乎找到了匹配项,但结果格式全错了。
let checkBool = s => Belt.Array.map(badSlots, bs => s !== bs);
let check = s =>
Belt.Array.keepMap(badSlots, bs =>
if (s !== bs) {
Some(s);
} else {
None;
}
);
let checkBoolResult = Belt.Array.map(slots, s => checkBool(s));
Js.log2("checkBoolResult", checkBoolResult);
let checkResult = Belt.Array.keepMap(slots, s => Some(check(s)));
Js.log2("checkResult", checkResult);
记录:
checkBoolResult [
[ true, true, true ],
[ true, true, true ],
[ false, true, true ],
[ true, false, true ],
[ true, true, false ],
[ true, true, true ],
[ true, true, true ]
]
checkResult [
[
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:00:00-04:00'
],
[
'2014-08-11T10:30:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T10:30:00-04:00'
],
[ '2014-08-11T11:00:00-04:00', '2014-08-11T11:00:00-04:00' ],
[ '2014-08-11T11:30:00-04:00', '2014-08-11T11:30:00-04:00' ],
[ '2014-08-11T12:00:00-04:00', '2014-08-11T12:00:00-04:00' ],
[
'2014-08-11T12:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T12:30:00-04:00'
],
[
'2014-08-11T13:00:00-04:00',
'2014-08-11T13:00:00-04:00',
'2014-08-11T13:00:00-04:00'
]
]
任何语法中的任何指导都将不胜感激。谢谢。
通过原因不和谐论坛 here:
此解决方案有效:
let x = Js.Array.filter(x => !Array.mem(x, badSlots), slots);
//output
x [
'2014-08-11T10:00:00-04:00',
'2014-08-11T10:30:00-04:00',
'2014-08-11T12:30:00-04:00',
'2014-08-11T13:00:00-04:00'
]