I was studying a functor and monad. So I got to know that a functor uses map
and a monad uses flatMap
, and also that definition of map
and flatMap
looks like below.
enum Box<T> {
case some(T)
case empty
}
extension Box {
func map<U>(_ f: @escaping (T) -> U) -> Box<U> {
// ...
}
}
extension Box {
func flatMap<U>(_ f: (T) -> Box<U>) -> Box<U> {
// ...
}
}
I totally got those are using different f
parameter(Thanks for this article).
Then, I just wanted to check definition of map
and flatMap
in Optional
, Result
and Array
. Because I heard those are monad as well. In Optional
and Result
, the definition looks like same as custom map
and flatMap
above.
But in Array
, does not.
func map<T>(_ transform: (Element) throws -> T) rethrows -> [T]
func flatMap<SegmentOfResult>(_ transform: (Element) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.Element] where SegmentOfResult : Sequence
I'm bit confused now. Because I knew a monad apply a function that returns wrapped value to wrapped value, so I expected the definition of flatMap
looking like below(definitely wrong).
func flatMap<T>(_ transform: (Element) throws -> [T]) rethrows -> [T]
But It's not anyway.
Am I missing something? Where is the point that I misunderstood?
You were expecting
func flatMap<T>(_ transform: (Element) throws -> Array<T>) rethrows -> Array<T>
for Array
.
If you look closely, the above is merely a special case of the signature that you found, when SegmentOfResult == Array<T>
.
The flatMap
in Array
is defined not just for the case when SegmentOfResult == Array<T>
, but also for any SegmentOfResult
that is a Sequence
.
Why? Because we would like to define functions so that they work on as many types as possible (i.e. as general as possible). It turns out that flatMap
would also work if the function mapped to any Sequence
. That is, it doesn't depend on the fact that the function returns specifically an Array
. It only depends on the fact that the function returns some kind of a Sequence
. Check out how flatMap
is implemented here if you are interested.
For the same reason, flatMap
isn't just available on Array
, it's available on any type that conforms to Sequence
!