Hacker News new | ask | show | jobs
by yakshaving_jgt 1584 days ago
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`.
1 comments

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.
> ok, how do you implement a break of the iteration in map in JavaScript?

If you want something that behaves like:

  array.map(x => {
    if (x > 2) { break; } // can't really use break here!
    else { return x; }
  });
you do this:

  [...array].map((x,i,a) => {
    if (x > 2) { a.splice(i); }
    else { return x; }
  });
(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.

hey man -

i don't think you really listened to the other guy.

i think he's saying that there's no need for break/continue with FP

because you simply approach the problem from a different angle

it's a different paradigm

that's probably why he asked for an example (not to belittle you)

i might do a filter before a map to recreate a lot of the functionality of a continue or break

and when I wrote/write OOP I might avoid break/continue because these things can be leaky

ok, maybe I'm too aggressive lately, but the only reason to do a break is to stop iterating over the array. You can easily achieve the output of using break using filter, but you cannot achieve the behavior because filter goes over every item in your array.

Probably the most common reason you want to break is that you found what you were looking for so find is what you would use.

so to make an example that would not be reasonable for find:

you have a long array of objects, you need to display 5 of the 'interesting' objects out of this array. What constitutes 'interesting' is determined by some relatively complicated logic on each item in the array. Since you do not know how many items in this array are interesting you want to process all items until you have 5 items. When you have 5 items you want to stop processing items therefore you break, if your first 5 items have the property of being 'interesting' you just saved a lot of work in your application and things feel zippy so you want to do that (because your application maybe needs the help at this point)

It is obviously, as all examples when one does not need something at the very moment, somewhat artificial - but I would say it is not unthinkable.

However, as I made clear earlier, I am not arguing for the necessity of break - if I wanted to do what I just described I would probably use while unless the syntax became unwieldy - I dislike break.

The only reason I got into this is because someone said they didn't believe most programmers wouldn't ever need break (which can be used to stop iteration of an array) and the guy who said all most programmers needed was map and reduce (both of which iterate over every item of an array) then said they never said most programmers wouldn't need break.

> You can easily achieve the output of using break using filter, but you cannot achieve the behavior because filter goes over every item in your array.

Because JS’s map and filter provide the index and array as arguments, you can, in fact, achieve the behavior of break. If you need the original array after the iteration, you need to apply the map or filter to a copy, though, because the way to achieve the behavior of break involves modifying the array by deleting all the items after the current one, which stops the iteration.

If the language implements them as lazy iteration, then I think take_until would be effectively equivalent to break, with early exiting behavior.

List.where(…).map(…).take_until(…)

  const findInterestingItems = (arr, isInteresting, num, i=0) =>
    num == 0 || i >= arr.length
    ? []
    : isInteresting(arr[i])
      ? [arr[i], ...findInterestingItems(arr, isInteresting, num-1, i+1)]
      : findInterestingItems(arr, isInteresting, num, i+1)
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.
But then you're processing the whole list, probably defeating the point of breaking.
that's a performance optimization. you have other performance optimizations available often, with tradeoffs.

FP is a higher level paradigm so you might have to trade off with performance too

>FP is a higher level paradigm so you might have to trade off with performance too

Then it can hardly he said be to unequivocally be better, can it.

that's like saying high level languages cannot be unequivocally better then writing assembly, not doing your own memory management is bad, etc...

Functional programming is a higher level abstraction. If the underlying implementation is not well implemented/optimized then you could have bad performance while the opposite is also true.

i don't understand what you mean. Are you referring to performance or implementation details?