[swift-evolution] [Proposal] Higher Kinded Types (Monads, Functors, etc.)

Al Skipp al_skipp at fastmail.fm
Wed Dec 16 08:13:59 CST 2015

> let myDictionary: [String:Int] = ["test": 1]
> var otherDictionary = myDictionary.myMap { ($0, Float($1)) }
> // does not fail:
> assert(otherDictionary is [String:Float])

The trouble with Dictionary is that peculiar things can happen if we can map over keys and return a Dictionary.

let dict = ["a":1, "b":2, "c":3]
let dict2 = dict.map { ("x", $1 * 2) } // [“x”:6]

We start with a Dictionary with 3 keys and end up with a Dictionary with 1 key! As the order of the keys is unknown, we have no idea what the value of the key will be either, it might be 6, 4 or 2. 

Swift currently avoids this bedlam by returning an Array of tuples when ‘mapping’ a Dictionary. Strictly speaking this isn’t ‘map’ in the Functor sense, but then again Dictionary is a rogue that refuses to obey the Functor laws anyway.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151216/bb75f090/attachment.html>

More information about the swift-evolution mailing list