Maintaining sets
Being able to remove duplicate values from lists is great. The problem with this approach is that you have to remove duplicates every time that you run a side-effect. This is wasteful for two reasons:
- You're storing values that aren't used for anything
- You have to spend valuable CPU cycles to get rid of them
To counter these issues, you can just use ordered sets instead of lists to prevent duplicates from ever appearing in the first place.
Adding unique values
Let's implement a function that prints set values only if the set
argument value has changed since the last time it was called:
const printValues = (set) => { if (!set.equals(printValues.prev)) { printValues.prev = set; set .valueSeq() .map(v => v.toJS()) .forEach(v => console.log(v)); } };
If this code looks familiar, it's because you learned about this change detection technique in the previous chapter. The reason that we need this here is so that we can see how sets change as we add...