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
| func doThing(defaultable value: Int = 4, _ required: Void -> Int) -> Int { | |
| return value + required() | |
| } | |
| doThing(defaultable: 4, { 6 }) // works | |
| doThing { 6 } // works | |
| doThing({ 6 }) // doesn't compile: Missing argument for parameter #2 in call |
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 XCTest | |
| @testable import <#project#> | |
| class StringMD5Test: XCTestCase { | |
| func testMD5() { | |
| let string = "soroush khanlou" | |
| XCTAssertEqual(string.md5, "954d741d14b14002d1ba88f600eee635") |
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
| UIFont.familyNames.forEach({ familyName in | |
| let fontNames = UIFont.fontNames(forFamilyName: familyName) | |
| print(familyName, fontNames) | |
| }) |
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 LinkedList<T> { | |
| var head: Node<T> | |
| init(head: Node<T>) { | |
| self.head = head | |
| } | |
| } | |
| indirect enum Node<T> { |
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 Sequence where Self.Iterator.Element: Numeric { | |
| var sum: Self.Iterator.Element { | |
| return self.reduce(0, +) | |
| } | |
| } | |
| extension Collection where Element: BinaryFloatingPoint { | |
| var average: Element { | |
| return self.reduce(0, +) / Element((0 as IndexDistance).distance(to: self.count)) | |
| } |
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 State {} | |
| struct Transition<From: State, To: State> { | |
| let transition: () -> To | |
| } | |
| struct Machine<CurrentState: State> { | |
| var state: CurrentState | |
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 Collection { | |
| func eachConsecutive(_ size: Int) -> Array<SubSequence> { | |
| let droppedIndices = indices.dropFirst(size - 1) | |
| return zip(indices, droppedIndices) | |
| .map { return self[$0...$1] } | |
| } | |
| } |
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 SignedInteger { | |
| func times() -> AnySequence<()> { | |
| return AnySequence<()>({ () -> AnyIterator<()> in | |
| var count: Self = 0 | |
| return AnyIterator<()>({ | |
| if count == self { | |
| return nil | |
| } |
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 | |
| final class SafeSyncQueue { | |
| struct QueueIdentity { | |
| let label: String | |
| } | |
| let queue: DispatchQueue | |
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 | |
| extension Data { | |
| var hexString1: String { | |
| return self.map({ return String(format: "%02hhx", $0) }).joined() | |
| } | |
| var hexString2: String { | |
| return self.reduce("", { return $0 + String(format: "%02hhx", $1) }) |