I find it really difficult to believe that there are developers out there that never need to be able to break or continue in a loop. What exactly would they be developing?
I basically rely on map 80% of the time for iteration. I do have to spend some mental cycles on deciding where to go if I needed to break or skip elements. That was until I discovered the beautiful .flatMap() trick of returning an empty array to skip or exclude an index item.
I see people use JavaScript Array.map a lot when they aren't doing anything with the returned array, which I find kind of irritating.
So if you're using map sensibly, no there's nothing wrong. But I don't like the dogma I read a lot which is "use map always" because it leads to inexperience developers using it in the wrong circumstances.
They don't want to keep the unmapped value in the resulting collection at all. flatMap allows for removal of an element in one traverse, unlike filter+map with eager behaviour.
Regardless of how you choose to interpret my comment, it is the case that both `break` and `continue` are literally never necessary, as those two constructs can be implemented in terms of `map` and `reduce`.
(if you don’t need the array later, you can just use “array” instead of [...array]; the latter is just used because the break effect is achieved by mutating the array to delete all the elements subsequent to the one you are working, terminating the iteration, and copying the array initially means you don’t stomp the original.)
huh, that's actually interesting and horrifying I guess, I knew the third argument of map was the original array but I had never seen a usage of it - I guess mutating an array while working on it goes against instinct for just about everyone.
so I guess I was wrong, you can get the same functionality of break in map, albeit in a way that, right at this moment, makes me uneasy.
There is no sensible way to `break` the iteration while mapping. That's not what `map` is for. In fact, there's very little reason for anyone to need `break` ever. The use of `break` implies an effectful procedure rather than a pure function, so at this point you're not really doing Functional Programming.
Let me know what it is you're trying to achieve — at a high level, not in terms of specific implementation details — and I can show you how this might be expressed without using anything as low-level as a `for` loop.
I'm not asking help with programming anything, and it seems somewhat insulting that somehow you have managed to turn multiple people's observations that you can't break in a map and so most programmers probably need something more than map or reduce into a request for tutelage - especially when you've spent a lot of time saying the impossible (implementing break in map) was trivial before switching to observing it was not sensible to do - which I take as meaning an admission of the aforementioned impossibility.
so to clarify - I am not asking for your help in solving my programming problems. You made a response to another poster asserting that
>all most programmers need is either `map` or `reduce`
that poster said
>I find it really difficult to believe that there are developers out there that never need to be able to break or continue in a loop.
you said
>I’m not sure where you got “never” from in my comment.
I, in my normal long-winded way gave you the benefit of the doubt that you meant something else but advised the previous poster had assumed you meant more than 50% of programmers will not ever need other array methods than map or reduce and hence was confused because you cannot break or continue with those (with map and reduce together you can implement something that has the same end effect as continue but it is not 100% the same of course)
but for some reason you just wanted to keep saying you were right and others were wrong even though the clear meaning of everyone's text was - you can't break in a map.
And you said:
>Regardless of how you choose to interpret my comment, it is the case that both `break` and `continue` are literally never necessary, as those two constructs can be implemented in terms of `map` and `reduce`.
frankly exasperated I said
>ok, how do you implement a break of the iteration in map in JavaScript? I believe it cannot be done, you believe it can? Teach me.
now admittedly here you may think I am confused and need help, but really I am saying you are wrong in a nice way when you say break can be implemented in map. But then you say:
>There is no sensible way to `break` the iteration while mapping. That's not what `map` is for.
right, what everyone's been telling you from the beginning.
>Let me know what it is you're trying to achieve — at a high level, not in terms of specific implementation details — and I can show you how this might be expressed
Thank you for your kindness but it should be obvious from the whole conversation nobody is asking you for programming help here, they are just saying your initial statement of most programmers only need map and reduce was overly bold.
As to why someone might want to break a loop, generally you do that when you have a long array - say 10000 items (don't bother telling me that 10000 is not a long array, I know, but a long array is often used as meaning an array requiring a lot of processing and that is partially determined by what you need to do with that array, if that is not good enough for you arbitrarily add 0s to the array length until you feel you have a long array) and have a number of conditions that can cause you to only have to process a number of them which probably in that case you would use find (because often in such a case you are processing an array to find one item in it) or if for some reason you needed to drop down to a lower level I would prefer to use while instead of a for loop because semantically I think while indicates to anyone reading the code without going into the loop - hey Bryan doesn't expect to have to look at every element of this array!
BUT ALL THAT DOESN'T MATTER - because the subject matter of this long discussion was you saying most programmers only need map and reduce and someone asked why do you think most programmers will NEVER need to break. And you wanted to know why they assumed that you thought most programmers would NEVER need to break because evidently you felt you never said anything remotely like that.
a better idea is to first use filter() to obtain a collection you can then map() over. So then you dont need to continue or break, the offending elements of the collection were already filtered out.
I believe it was gotten from the ambiguities of English - when you say "all most programmers need is " it could mean all that is needed most of the time but probably the more usual meaning would be that a great number of programmers (most programmers - greater than 50% of programmers), will never need anything but `map` or `reduce`. If the second meaning is taken then this means as a corollary that these developers never need to be able to break or continue in a loop.
Of course if it were assumed they never need anything but the higher level methods: map, reduce, some, every etc. - then they would probably use some or every if they wanted to break.
out of the edit window, evidently was typing quickly, but would not use every for implementing a break like functionality on an array - some could do it in some instances or maybe find.
I suppose filter could maybe be hacked to do it in some cases dependent on implementation, if the implementation determined that it was impossible to ever return true for any of the items remaining in an array that implementation might effectively break the array processing however every way I can think of doing that would be artificial, horrible, and probably full of side effects.
I thought the original statement was break or continue, I was actually focusing on break? As far as I know you don't break in map, you can return null but not break the loop itself.
on edit: although I almost never break in a loop, I use some other construct instead.
on second edit: maybe I should say iteration instead of loop but will let it stand.
Is it bad to use map?