arraysfilterocamlreason

Remove Strings That Appear in one Array from second list if they exist in OCaml/ReasonML


I have to arrays of dates that look like this:

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",
|];

How would I remove items from the first array that appear in the second array so that the result is:

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'
]

So far I have tried this in reason which seems to finding the matches but the result format is all wrong.

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);

Which logs:

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'
  ]
]

Any guidance would be appreciated in either syntax. Thanks.


Solution

  • Via the reason discord forum here:

    This solution works:

    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'
    ]