News for hackers. Stuff that matters.
Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @02:11AM (#30298047)
Actually, all most programmers need is either `map` or `reduce`. A classic `for` loop is too low-level for most people’s needs.
Re:FP techniques that will help you write better JavaScript by Xevi on Friday February 11, 2022 @03:12AM (#30298550)
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?
Re:FP techniques that will help you write better JavaScript by nefitty on Friday February 11, 2022 @04:44AM (#30299348)
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.

Is it bad to use map?

Re:FP techniques that will help you write better JavaScript by bluefirebrand on Friday February 11, 2022 @09:39AM (#30301970)
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.

Re:FP techniques that will help you write better JavaScript by Akronymus on Friday February 11, 2022 @10:08AM (#30302278)
Does JS have something like array.iter?
Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @05:03AM (#30299486)
What are you struggling with when trying to skip elements?

> Is it bad to use map?

No.

Re:FP techniques that will help you write better JavaScript by nefitty on Friday February 11, 2022 @05:25AM (#30299664)
Whether to use a for loop or for of or forEach or map().filter() etc

flatMap takes care of a lot of that

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @05:29AM (#30299692)
Why are you using a “trick” to skip an element in some structure when mapping a function over it?

For posterity (and forgive me if this JavaScript syntax is inaccurate; I don't write it so much these days)…

    xs.map(a => shouldBeSkipped ? a : f(a))
Re:FP techniques that will help you write better JavaScript by wlamartin on Friday February 11, 2022 @09:34AM (#30301917)
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.
Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @09:38AM (#30301964)
Then why not just use a fold?
Re:FP techniques that will help you write better JavaScript by wlamartin on Friday February 11, 2022 @10:09AM (#30302287)
Sure, there's many ways to skin a cat. Given the option, I wouldn't choose either of these, and instead use something like filterMap[1] which I think conveys intent better than a fold or flatMap.

Btw, I'm assuming that the original map questioner wasn't solely using flatMap for side-effectful iteration, which reading again, I'm a bit suspicious about.

1: https://gcanti.github.io/fp-ts/modules/Array.ts.html#filterm...

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @03:19AM (#30298607)
I’m not sure where you got “never” from in my comment.
Re:FP techniques that will help you write better JavaScript by just1comment on Friday February 11, 2022 @04:12AM (#30299122)
Come on, you just said that most people only need map or reduce, which implies they never need to break or continue.
Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @05:06AM (#30299515)
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`.
Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @05:58AM (#30299920)
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.
Re:FP techniques that will help you write better JavaScript by dragonwriter on Friday February 11, 2022 @02:57PM (#30306256)
> 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.)
Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @05:11PM (#30307629)
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.

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @06:17AM (#30300067)
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.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @07:18AM (#30300530)
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.

Re:FP techniques that will help you write better JavaScript by appletrotter on Friday February 11, 2022 @11:52AM (#30303649)
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

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @12:30PM (#30304230)
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.

Re:FP techniques that will help you write better JavaScript by dragonwriter on Friday February 11, 2022 @04:52PM (#30307452)
> 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.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Saturday February 12, 2022 @03:06AM (#30310883)
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!

Re:FP techniques that will help you write better JavaScript by setr on Friday February 11, 2022 @01:55PM (#30305530)
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(…)

Re:FP techniques that will help you write better JavaScript by dragonwriter on Friday February 11, 2022 @04:54PM (#30307473)
> 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.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Saturday February 12, 2022 @01:33AM (#30310481)
>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.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @04:28PM (#30307189)
maybe, but that seems a weird way to say you can do it.
Re:FP techniques that will help you write better JavaScript by xigoi on Friday February 11, 2022 @04:08PM (#30307022)

  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)
Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @04:45PM (#30307390)
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?

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @06:00PM (#30308049)
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.
Re:FP techniques that will help you write better JavaScript by bil7 on Friday February 11, 2022 @08:44AM (#30301369)
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.
Re:FP techniques that will help you write better JavaScript by thfuran on Friday February 11, 2022 @09:09AM (#30301646)
But then you're processing the whole list, probably defeating the point of breaking.
Re:FP techniques that will help you write better JavaScript by appletrotter on Friday February 11, 2022 @11:54AM (#30303682)
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

Re:FP techniques that will help you write better JavaScript by thfuran on Friday February 11, 2022 @12:25PM (#30304145)
>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.

Re:FP techniques that will help you write better JavaScript by 0ldskool on Friday February 11, 2022 @07:44PM (#30308842)
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.

Re:FP techniques that will help you write better JavaScript by thfuran on Friday February 11, 2022 @11:14PM (#30309884)
>that's like saying high level languages cannot be unequivocally better then writing assembly,

Yes, it is like saying that. At least, assuming by "cannot be" you mean "is not".

>not doing your own memory management is bad

No, it's not like saying that. Note how differently those claims are phrased.

Re:FP techniques that will help you write better JavaScript by bil7 on Friday February 11, 2022 @09:43AM (#30302013)
i don't understand what you mean. Are you referring to performance or implementation details?
Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @04:09AM (#30299083)
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.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @07:14AM (#30300498)
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.

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @05:09AM (#30299541)
> this means as a corollary that these developers never need to be able to break or continue in a loop.

No it doesn’t.

Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @05:40AM (#30299780)
I don't understand this no it doesn't, the logic I outlined can be boiled down to

If I only need to do X

And X cannot do Y

Then I do not need to do Y

-----------------------------

If I need to do Y

And X cannot do Y

Then I need to do something other than X

-------------------------

Give your answer of no it doesn't please be so kind as to outline the logical reasoning that you are envisioning?

on edit: added line between logical statements.

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @05:44AM (#30299814)
Unless I've misunderstood you, you said that, for example, `continue` cannot be implemented in terms of `map`, which simply isn't true. It's trivial.
Re:FP techniques that will help you write better JavaScript by bryanrasmussen on Friday February 11, 2022 @05:49AM (#30299862)
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.

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @06:05AM (#30299967)
It's not clear to me what you're trying to achieve. Sorry.
Re:FP techniques that will help you write better JavaScript by ReleaseCandidat on Friday February 11, 2022 @04:39AM (#30299307)
> Actually, all most programmers need is either `map` or `reduce`.

You actually don't need `map`, you can easily implement that using `reduce` too ;)

Re:FP techniques that will help you write better JavaScript by feupan on Friday February 11, 2022 @08:54AM (#30301484)
Nobody needs reduce, you’re just making code make less sense. If you’re using reduce when map works you’re just a masochist.

https://twitter.com/jaffathecake/status/1213077702300852224

Re:FP techniques that will help you write better JavaScript by yakshaving_jgt on Friday February 11, 2022 @04:47AM (#30299381)
I'm well aware of that, but I don't think that's a good idea.

There is utility in communicating whether you're working with a catamorphism vs a homomorphism. Code is written for humans, after all.

Re:FP techniques that will help you write better JavaScript by ReleaseCandidat on Friday February 11, 2022 @04:58AM (#30299438)
Of course, that wasn't meant seriously. That's why i like `for_each` (or `iter` or whatever it's called) as 'third' function too, because it makes it clear you're doing side-effects.