3 ms·
Can anyone explain whats the reasoning behind renaming flatMap?
by tabs_masterrace 9y ago
Can anyone explain whats the reasoning behind renaming flatMap?
- masklinn 9y agoThe SEP makes it pretty clear: https://github.com/apple/swift-evolution/blob/master/proposals/0187-introduce-filtermap.md https://github.com/apple/swift-evolution/blob/master/proposa... Basically, they did not rename flatMap they renamed one specific overload: Sequence.flatMap<U>(_: (Element) -> U?) -> [U] or in Haskell parlance mapMaybe :: (a -> Maybe b) -> [a] -> [b] which filters after mapping more than it flattens (in fact the Rust and OCaml versions are called filter_map). The issue is that due to Optional promotion you could misuse flatMap (where you should have used map I guess) and could get this filtering behaviour unexpectedly instead of the straightforward mapping you would be expecting (it also led to extra branches and optional wrappings and unwrappings).