Hacker News new | ask | show | jobs
by bryanrasmussen 1589 days ago
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.

3 comments

> 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.

yes, you showed this earlier https://news.ycombinator.com/item?id=30306256 by mutating the array parameter of the map, but I made my wrong statement before reading your example.

I guess it's a failure of imagination on my part not to have realized that I could modify the array length in that way, or really the failure of imagination was not seeing any good reason for sending the original array in as a parameter to map - when I saw that in documentation I thought what a weird idea!

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(…)

> If the language implements them as lazy iteration

JS doesn't, though.

> then I think take_until would be effectively equivalent to break

JS also doesn't have take_until.

>JS also doesn't have take_until.

I just assumed it was a new method coming out soon! Although when I was thinking of how I would make a method I just thought an until method that iterated all elements of an array outputting them until it got the return from the callback that was passed as the iteration stopper - default value null.

maybe, but that seems a weird way to say you can do it.

  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)
I don't see where you used map? You made a recursive function? the argument was clearly stated - person says all most programmers need is map and reduce on arrays. I agree that by recursing you also have found another way not to use break but you have evidently had to NOT use map and reduce to do it?

on edit: also how will that recursion perform on the large array we've supposed as being used if it turns out that we don't get our interesting items in early parts of array?

ok someone showed a way you can achieve break like functionality with map https://news.ycombinator.com/item?id=30306256 by mutating the array parameter of the map.