Clarification on Array Eq

Hi

I just have a quick question about how arrays operate, in particular how they handle equality. If for example I have two

Array PlaylistTimelineMeta

where PlaylistTimelineMeta looks like this:

newtype PlaylistTimelineMeta = PlaylistTimelineMeta
  { duration ∷ Seconds
  , zipURL ∷ String
  , zipModified ∷ Instant
  }
--

derive instance genericPlaylistTimelineMeta ∷ Generic PlaylistTimelineMeta _

derive instance newtypePlaylistTimelineMeta ∷ Newtype PlaylistTimelineMeta _

instance showPlaylistTimelineMeta ∷ Show PlaylistTimelineMeta where
  show = genericShow
--

derive instance eqPlaylistTimelineMeta ∷ Eq PlaylistTimelineMeta

Would I be able to just compare the two arrays for equality. I would assume since it doesn’t throw a hissy fit when I try it is indeed checking the the objects inside. Since all object in the array conform to Eq then it should be able to successfully compare them, correct? The main reason why I am asking here is because PlaylistTimelineMeta only changes once a day at 2am so its a pain to troubleshoot and would be much easier if I understood how this is actually working.

Thanks

2 Likes

Yes exactly - there is an Eq instance for arrays that does exactly what you describe (see the code here and here for the JS that does the actual comparision)

here is what is going on:

export const eqArrayImpl = function (f) {
  return function (xs) {
    return function (ys) {
      if (xs.length !== ys.length) return false;
      for (var i = 0; i < xs.length; i++) {
        if (!f(xs[i])(ys[i])) return false;
      }
      return true;
    };
  };
};

Here f is (==)/eq that passed from the Eq instance of the items - on your case the generic eq for PlaylistTimelineMeta that is checking each of your record fields.

2 Likes