This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------------------------------------- // | |
// Swift 3.0: | |
func fibs(through: Int, includingZero useZero: Bool = false) | |
-> UnfoldSequence<Int, (Int, Int)> { | |
return sequence(state: useZero ? (1, 0) : (0, 1), | |
next: { (pair: inout (Int, Int)) -> Int? in | |
return pair.1 <= through ? { | |
defer { pair = (pair.1, pair.0 + pair.1) } | |
return pair.1 }() : nil | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For details, see | |
// http://stackoverflow.com/questions/40261857/remove-nested-key-from-dictionary | |
import Foundation | |
extension Dictionary { | |
subscript(keyPath keyPath: String) -> Any? { | |
get { | |
guard let keyPath = Dictionary.keyPathKeys(forKeyPath: keyPath) | |
else { return nil } | |
return getValue(forKeyPath: keyPath) |