James Sinclair
jrsinclair.bsky.social
James Sinclair
@jrsinclair.bsky.social
I type for a living. Sometimes code, sometimes not.
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.
April 1, 2025 at 3:24 AM
The first, takeWhile(), traverses your array and keeps adding items to a new array until a predicate returns false.
April 1, 2025 at 3:24 AM
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.
March 30, 2025 at 9:34 PM
All we've really done with the third option, though, is re-implement the Set structure from scratch. So, we might as well use that. The most efficient option also happens to be the most concise.
March 26, 2025 at 10:39 PM
To avoid traversing the array more than we need to, we can use a Map to keep track of the elements we've seen so far. This way we only traverse the array once. Once we've been through the list, we return the keys of the Map as an array.
March 26, 2025 at 10:39 PM
A (slightly) 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.
March 26, 2025 at 10:39 PM
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.
March 26, 2025 at 10:39 PM
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 @[email protected] for introducing me to the idea.
March 25, 2025 at 9:48 PM
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.
March 24, 2025 at 10:07 PM
It's old news now, but swapping variables with destructuring still blows my mind every time I see it.
March 23, 2025 at 9:21 PM
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.
March 20, 2025 at 10:26 PM