The second, dropWhile(), does the opposite. It traverses your array and ignores items until a predicate returns false. Then it will give you the rest of the array.
The first, takeWhile(), traverses your array and keeps adding items to a new array until a predicate returns false.
In some ways, this is so obvious it shouldn't need stating. But it does. And the tension is real. We want our code to be elegant, simple, concise. Yet, we rarely trade that off against the complexity that elegance creates in the interfaces (user interfaces and APIs) we develop.
A more efficient approach is to use .indexOf() to check if the current element is the first occurrence of that element in the array. But it still traverses the array many more times than it needs to.
Our first approach uses a filter and `.includes()`. This is inefficient because it traverses the array multiple times, and creates lots of new intermediate arrays. It works, though.
Did you know that you can use an array tuple in place of a Maybe structure? It elegantly handles empty values using familiar `.map()`, `.flatMap()`, and `.reduce()` methods. I think it's rather neat. Credit goes to
@jmsfbs for introducing me to the idea.
I love this quote from John Ousterhout:
> I have found over and over that specialization leads to complexity; I now think that over-specialization may be the single greatest cause of complexity in software.
On the surface, it appears to contradict YAGNI, but not necessarily.
It's old news now, but swapping variables with destructuring still blows my mind every time I see it.
I still see lots of people setting default values in JS using the || operator. This is fine if the values you're dealing with are always objects. But if you deal with numbers, booleans or strings, this can be problematic. In most cases, the ?? operator is a better choice.
I had a great time at Web Directions Summit '23, last week. Thanks so much to
@johnallsopp, Craig Sharkie, and the rest of the team for providing the opportunity to present.