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
func spaces(n: Int) -> String { | |
return reduce(0..<max(0, n), "") { r, _ in r + " " } | |
} |
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
init() { | |
view = { | |
frame = CGRect() | |
return UIView(frame: frame) | |
}() | |
} |
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
extension Optional { | |
func both_then<T, U>(_ other: Optional<U>, f: (Wrapped, U) -> T) -> Optional<T> { | |
guard let a = self, let b = other else { return nil } | |
return f(a, b) | |
} | |
func neither<T>(_ other: Optional<T>) -> Bool { | |
return self == nil && other == 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
/** | |
* Uses the `produce` function to produce values until the `predicate` returns false. | |
* If supplied, each value is transformed by `transform` before being yielded. | |
* | |
* @param {Function} produce produces the next value | |
* @param {Function} predicate return true if the production should continue | |
* @param {Function} transform transforms the produced value, defaults to identity | |
*/ | |
function* produceWhile(produce, predicate, transform = value => value) { | |
while(true) { |
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
const makeAsync = require('es6-async') | |
const timeout = (milliseconds) => new Promise(resolve => setTimeout(resolve, milliseconds)) | |
const random = () => Promise.resolve(Math.random()) | |
const asyncFunc = makeAsync(function* () { | |
yield timeout(1000) | |
return yield random() | |
}) |
OlderNewer