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

1 comments

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.