Most languages I’ve worked in that wouldn’t work either since the return would be for the inner function and not the full foreach. In that case you would need to change the way you iterate over the collection.
No, you're right. One of the things with the built-in higher-order functions is that they don't give you a way to end the looping early. (There are some third-party versions that do, though.)
Array.some and Array.every can both short-circuit and break early, so even though Array.includes is the better choice for finding a primitive value, using Array.every((section) => section != sectionName) would be the halfway point between that and the current implementation. It's also an expression, so you can just return it directly.
37
u/Notimecelduv Dec 20 '21
12
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Dec 20 '21
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
7
u/Imbasaur Dec 21 '21
Looks like the foreach won’t break out when the condition is found. Isn’t that bad since it still solves the problem but could be better.
16
u/ergotofwhy Dec 20 '21
Me, first pass: "I should really name this something better because I know I'm going to use it later."
Me, second pass: "I swear to god I already wrote this, but I can't find it anywhere... fuck it"
2
u/vasametropolis Dec 21 '21
Holy shit do these two functions do literally the exact same thing?
Edit: Yes, yes they do. Change the function name and the parameter and variables names. What on Earth.
2
u/drlogwasoncemine Dec 21 '21
I always like to call my variables "variable"
3
u/kappcity Dec 20 '21
Totally different functions. Makes sense.