This file contains hidden or 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
| internal final class SpinLock { | |
| private let lock = NSLock() | |
| func sync<T>(action: () -> T) -> T { | |
| lock.lock() | |
| defer { lock.unlock() } | |
| return action() | |
| } | |
| } |
This file contains hidden or 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 String { | |
| private static let whitespaceRegularExpression = try! NSRegularExpression(pattern: "\\s") | |
| var escapingWhitespaces: String { | |
| return String.whitespaceRegularExpression.stringByReplacingMatches( | |
| in: self, | |
| range: NSRange(location: 0, length: self.utf16.count), | |
| withTemplate: "\\\\$0" | |
| ) | |
| } |
This file contains hidden or 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
| protocol Semigroup { | |
| // Binary, associative semigroup operation (op) | |
| func op (_ g: Self) -> Self | |
| } | |
| extension Int : Semigroup { | |
| func op (_ n: Int) -> Int { | |
| return self + n | |
| } | |
| } |
This file contains hidden or 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
| protocol Semigroup { | |
| // Binary semigroup operation | |
| // **AXIOM** Should be associative: | |
| // a.op(b.op(c)) == (a.op(b)).op(c) | |
| func op (_ g: Self) -> Self | |
| } | |
| extension Int : Semigroup { | |
| func op (_ n: Int) -> Int { | |
| return self + n |
This file contains hidden or 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
| indirect enum List <A> { | |
| case Nil | |
| case Cons( | |
| A, | |
| List<A> | |
| ) | |
| } | |
| func map <A, B> (_ f: @escaping (A) -> B) -> (List<A>) -> List<B> { | |
| return { xs in |
This file contains hidden or 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
| import Foundation | |
| import Dispatch | |
| import PlaygroundSupport | |
| PlaygroundPage.current.needsIndefiniteExecution = true | |
| precedencegroup Additive { | |
| associativity: left | |
| } |
This file contains hidden or 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
| import Foundation | |
| typealias JSONDict = [String: Any] | |
| struct Parameter<A> { | |
| let apiDescription: String | |
| let parse: (Any) -> A? | |
| } | |
| extension Parameter { |
This file contains hidden or 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
| // http://www.futantan.com/2016/04/14/NSTimer-tips/#more | |
| extension NSTimer { | |
| private class FTTimerClosureWraper { | |
| private (set) var timerClosure: () -> () | |
| init(timerClosure: () -> () ) { | |
| self.timerClosure = timerClosure | |
| } | |
| } |
This file contains hidden or 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
| struct Stock { | |
| var name = "" | |
| var id = "" | |
| } | |
| let stock1 = Stock(name: "name", id: "1") | |
| let stock2 = Stock(name: "name", id: "2") | |
| let stock3 = Stock(name: "name", id: "3") | |
| let stock4 = Stock(name: "name", id: "4") | |
| let stock5 = Stock(name: "name", id: "5") |