Then icon indicating copy to clipboard operation
Then copied to clipboard

add `let` idiom which could return a significant value other than `self` or `Unit`

Open lolicon opened this issue 4 years ago • 4 comments

lolicon avatar Aug 15 '19 07:08 lolicon

Here is another useful use case I came across:

let dateString = DateFormatter().let {
  // configure formatter
  return $0.string(from: date)
}

Mazyod avatar Aug 23 '19 15:08 Mazyod

what different with the map ?

ZhipingYang avatar Aug 26 '19 04:08 ZhipingYang

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.

Mazyod avatar Aug 26 '19 04:08 Mazyod

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

lolicon avatar Aug 26 '19 09:08 lolicon