[swift-evolution] Add an ifPresent function to Optional

Dmitri Gribenko gribozavr at gmail.com
Mon Mar 14 17:51:22 CDT 2016


On Mon, Mar 14, 2016 at 3:44 PM, Erica Sadun <erica at ericasadun.com> wrote:
>> No.  My argument is that map on collections and on optionals are two
>> completely different things.  They unify on a level that does not
>> exist in Swift (higher-kinded types).  We could name Optional.map()
>> differently, and that would be a separate decision from renaming
>> Collection.map(), but we used the name `map` primarily because of
>> precedent, not because there is a HKT notion of these two operations
>> being the same.
>
> Convergent evolution then, like tenrecs and hedgehogs?
>
> Because the way I've been thinking of it is like a teeny tiny collection that either can fit one thing snugly into it or not.

Optional.map returns an Optional.

Array.map returns an Array.
Set.map returns an Array.
<any other collection>.map returns an Array.

I can't say that it is not valid to think about an Optional as a tiny
collection, but as implemented in Swift, .map() does objectively
behave differently...

> You're going to make me have completely different mental images when I use Optional.map now, and I don't even
> have a mental metaphor to replace it with.

Don't know if it is helpful, but here's one: Optional is a result of a
computation that can fail.  .map() allows you to chain more things
onto a computation if it succeeded, and do nothing if it failed
already.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list