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
| // The contingency table contains the support counts for each item | |
| // in comparision with one another. | |
| struct ContingencyTable { | |
| var bc: Int // B and C | |
| var b_c: Int // B and not C | |
| var _bc: Int // not B and C | |
| var _b_c: Int // not B and not C | |
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
| var accumulatedItemSets = Dictionary(uniqueKeysWithValues: database.map { | |
| (Set([$0.key]), Set($0.value)) }) | |
| var itemSetsToEvaluate = accumulatedItemSets | |
| while !itemSetsToEvaluate.isEmpty { | |
| var temp: [Set<String>: Set<Int>] = [:] | |
| itemSetsToEvaluate.pairs().forEach { a, b in | |
| let key = a.key.union(b.key) | |
| let value = a.value.intersection(b.value) | |
| if value.count >= absoluteSupport { |
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 Quality = Int | |
| typealias SellIn = Int | |
| typealias UpdateRule = (Item) -> Item | |
| struct Item { | |
| enum Kind: String { | |
| case normal |
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 StoryboardInitializable where Self: UIViewController { | |
| static var storyboardName: String { | |
| return "Main" | |
| } | |
| static var storyboardBundle: Bundle? { | |
| return nil | |
| } | |
| static var storyboardIdentifier: String { | |
| return String(describing: self) |
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 BeginsWith { | |
| let value: String | |
| init(_ value: String) { | |
| self.value = value | |
| } | |
| } | |
| func ~=(pattern: BeginsWith, value: String) -> Bool { | |
| return value.hasPrefix(pattern.value) | |
| } |
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
| class Person {} | |
| unowned var zombie: Person | |
| do { | |
| let alive = Person() | |
| zombie = alive | |
| } | |
| // a zombie is born |
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
| public enum LinkedList<T> { | |
| case end | |
| indirect case node(value: T, next: LinkedList) | |
| public func cons(_ value: T) -> LinkedList { | |
| return .node(value: value, next: self) | |
| } | |
| } |
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
| let str = "Hello, Keita" | |
| var index = str.characters.startIndex | |
| while index < str.characters.endIndex { | |
| print(str.characters[index]) | |
| str.characters.formIndex(after: &index) | |
| } |
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
| let str = "acxz" | |
| func isFunny(_ s: AnySequence<UInt8>) -> Bool { | |
| let n1: [Int] = zip(s.dropFirst(), s).map { Int($0)-Int($1) } | |
| let n2: [Int] = zip(s.reversed().dropFirst(), s.reversed()).map { Int($1)-Int($0) } | |
| return n1 == n2 | |
| } | |
| isFunny(AnySequence(str.utf8)) |
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 | |
| private enum RandomSource { | |
| static private let file = fopen("/dev/urandom","r")! | |
| static private let queue = DispatchQueue(label: "random") | |
| static func get(count: Int) -> [Int8] { | |
| let capacity = count + 1 // fgets adds null termination | |
| var data = UnsafeMutablePointer<Int8>.allocate(capacity: capacity) | |
| defer { | |
| data.deallocate(capacity: capacity) |