3 ms·
> 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 difficu
by masklinn 7y 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.
- contravariant 7y agoYou 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; }
- masklinn 7y agoYou could also blow up your stack as there is no requirement whatsoever that javascript implementations be tail-recursive.