Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

They could have also implemented flat in terms of flatMap: flatMap(x => x)

I personally feel flatMap is a much more used method than flat, so if you want to remove one, I would remove flat.



> They could have also implemented flat in terms of flatMap: flatMap(x => x)

Flat can flatten any level of nesting (it just defaults to 1), so would be difficult to implement in terms of flatMap.


You could reproduce that behaviour of 'flat' by doing something like:

    function flatten(x, n=1) {
        return n > 0 ? x.flatmap(y => flatten(y, n-1))
                     : x;
    }


You could also blow up your stack as there is no requirement whatsoever that javascript implementations be tail-recursive.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: