Then
Then copied to clipboard
add `let` idiom which could return a significant value other than `self` or `Unit`
Here is another useful use case I came across:
let dateString = DateFormatter().let {
// configure formatter
return $0.string(from: date)
}
what different with the map ?
I had the exact same question in mind when I first saw this, but realized that map
semantics are usually different. When we map
a sequence, we are mapping each element, not the object as a whole, so it makes sense to differentiate.
Maybe mapSingle
or similar naming can be an option, but personally I really like the consistency with Kotlin.
what different with the map ?
although not quoted from the swift stdlib documentation, generally map
should have a signature of
map :: Functor f => (a -> b) -> f a -> f b
, which could simply treat map
function as unwrap the value (self
) and provide it as arguments to closure and wrap the closure result again, e.g
Optional<T>.map / Array<T>.map / Result<T>.map, while let
is just a shorthand to let user write anonymous $0 within a limited scope, no wrapping and unwrapping involved in this process, which is just a similar use case of Then
library