hailey
in the process of thinking "this could look nicer still" i realized that this thing really is kind of cursed ๐Ÿ˜ฎโ€๐Ÿ’จ
1 5
Bossett
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?
1 9
Samuel
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
2 2
Samuel
oh right haha so what, return null or something and then filter out afterwards?
1
Samuel
Performance is not an issue here and reduce is wildly more unreadable and bug prone. Avoid reduce where possible imo unless you have a specific need
chelsea bridge
> 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
1 8
1 more reply
Spoonful of bitter
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.
Samuel
ish - the arrays are now different lengths, so `items[i + 1]` is not necessarily the next item
3 7
Biscuit
my brain hurts but wouldn't i + 1 eventually be an out of bounds rascal or is this some javascript wizardry
2 5
Samuel
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 ;)
2
Orual
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).
5
James Munns
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?
1 1
fuckface "squeaky" from:me
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) => {`
2 3
fuckface "squeaky" from:me
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
1 1
fuckface "squeaky" from:me
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
1 2
Samuel
yeah in our code it wasn't `this.items` because items was a new array, but I agree it would've been way easier to spot
1
Michel Bestaat
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...
1 3
Samuel
correct! solution is to use the 3rd arg of map, which will be the filtered array
2 5
Michel Bestaat
Not sure if there would be performance and/or additional memory costs though. I probably cannot count on the compiler to optimize the code. :-)
1 1
Samuel
No itโ€™s perform exactly the same, the runtime would totally be able to deal with that
1
Metaflame
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`.
1
Justin Buist
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.
1 1
nil
would editing arrays while iterating through them be possible to warn/err with linting or compiler checks?