passing functions back and forth always feels like there's so much syntax guff that you miss stuff like this - do we just not iterate through arrays anymore?
what part? we need a function that does 2 things
1. filter out deleted messages
2. provide the next message, for the UI to use
I'm curious what you'd do differently
> Or use a normal loop.
i was waiting for someone to say that
you don't have a function that needs to do two things, you have two distinct problems to solve and a stack of code that hides bugs and performance issues
Why not return each undeleted item with their next companion deleted or not?
The exception raised when the last item is not deleted is a feature, not a bug.
No specs, no bug.
Well itโll always be in bounds, because itโll always be smaller than the number of items in the array. But it wont necessarily be pointing at the right item ;)
Yeah, that stood out to me as well. I would definitely get an exception from doing that unchecked in Kotlin, and Rust might not even let it compile (at least in debug mode).
Also, does items[i + 1] index off the end of the array? does js have an equivalent to rust's "windows", where you get an iterator over like like (0, 1), (1, 2), ... (N-2, N-1), (N-1, N)?
Also is this just iterator invalidation issues?
ah, this is why you use the third argument to map functions, which passes the array being mapped - a correct line 9 could shadow it by changing the invocation to `.map((item, i, items) => {`
one could also argue that `items` ought to come in as an argument, and whatever larger object provides an argumentless variant for a fixed `items` should be binding that function in its constructor
yeah, for all my griping about how JavaScript code ought to embrace lexical scoping more heavily, seeing it being spelled `this.items` probably would have made this bug a little more obvious to me
and of course this pattern should be followed for any reference into the source array for all iterative functions, precisely for reasons of composition when chained like this
I guess the real bug is that the code to get the next accesses the original items array whereas the map function runs on a filtered array from which items may be removed.
Just don't have deleted items and all will be fine...
It's using indices of the filtered array on the original array, which returns irrelevant non-following items (as well as possibly deleted items). Also possible out-of-bounds access, but I think that's just `undefined`.
Syntactic sugar. Functionally, it does nothing, but if you add a 3rd item to the list by copy/pasting the 2nd one you don't have to fiddle with adding or removing commas by always leaving a trailing one.