咱们想经过 Swift 简单的实现全排列。html
首先先写个例子,列出全部把元素插入到数组中的状况:git
func between<T>(x: T, ys: [T]) -> [[T]] { if let (head, tail) = ys.decompose { return [[x] + ys] + between(x, tail).map { [head] + $0 } } else { return [[x]] } } between(0, [1, 2, 3]) // [[0, 1, 2, 3], [1, 0, 2, 3], [1, 2, 0, 3], [1, 2, 3, 0]]
接下来再经过 between
函数实现全排列:github
func permutations<T>(xs: [T]) -> [[T]] { if let (head, tail) = xs.decompose { return permutations(tail) >>= { permTail in between(head, permTail) } } else { return [[]] } }
其中 >>=
这个符号前面有提到过,返回全部的组合结果。完整的实现代码以下:数组
extension Array { var decompose : (head: T, tail: [T])? { return (count > 0) ? (self[0], Array(self[1..<count])) : nil } } infix operator >>= {} func >>=<A, B>(xs: [A], f: A -> [B]) -> [B] { return xs.map(f).reduce([], combine: +) } func between<T>(x: T, ys: [T]) -> [[T]] { if let (head, tail) = ys.decompose { return [[x] + ys] + between(x, tail).map { [head] + $0 } } else { return [[x]] } } func permutations<T>(xs: [T]) -> [[T]] { if let (head, tail) = xs.decompose { return permutations(tail) >>= { permTail in between(head, permTail) } } else { return [[]] } }
咱们能够在原来的API上面封装一层减小频繁解包的麻烦:app
typealias JSONDictionary = [String:AnyObject] func decodeJSON(data: NSData) -> JSONDictionary? { return NSJSONSerialization.JSONObjectWithData(data, options: .allZeros, error: nil) as? JSONDictionary }
固然 encode
也同样:函数
func encodeJSON(input: JSONDictionary) -> NSData? { return NSJSONSerialization.dataWithJSONObject(input, options: .allZeros, error: nil) }