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
struct Participant { | |
let identifiers: [(label: String?, value: String)] | |
} | |
// This segfaults. Combining the two maps or changing "" to nil works. | |
let participants = ["a", "b", "c"] | |
.map { ("", $0) } | |
.map { Participant(identifiers: [$0]) } | |
// Update: Here's an error from a later build of Swift which explains the error. |
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
let numbers = [ | |
"one", | |
"2", | |
"0x3", | |
"42", | |
] | |
// This will run the function on all values and only keep the ones that are not nil: | |
let parsedNumbers = numbers.flatMap { Int($0, radix: 10) } | |
// [2, 42] |
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
indirect enum Node { | |
case tag(String, [Node]) | |
case text(String) | |
} | |
extension Node: CustomStringConvertible { | |
var description: String { | |
switch self { | |
case let .tag(name, children): | |
if children.count == 1, case let .some(.text(text)) = children.first { |
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
// Example: | |
let url = NSURL(string: "https://example.com/hello?bla=one&bla=two&foo&bar=1")! | |
let values = url.parseQueryString() | |
// Result: ["bla": ["one", "two"], "bar": ["1"], "foo": []] | |
extension NSURL { | |
func parseQueryString() -> [String: [String]]? { | |
guard let items = NSURLComponents(URL: self, resolvingAgainstBaseURL: false)?.queryItems else { | |
return 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
def point_distance(a, b): | |
degrees_to_radians = math.pi / 180 | |
phi_a = (90 - a.lat) * degrees_to_radians | |
phi_b = (90 - b.lat) * degrees_to_radians | |
theta_a = a.lon * degrees_to_radians | |
theta_b = b.lon * degrees_to_radians | |
cos = (math.sin(phi_a) * math.sin(phi_b) * math.cos(theta_a - theta_b) + | |
math.cos(phi_a) * math.cos(phi_b)) | |
arc = math.acos(cos) | |
return arc |
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 OrderedDictionary: DictionaryLiteralConvertible { | |
init(dictionaryLiteral elements: (Key, Value)...) { | |
for (key, value) in elements { | |
self[key] = value | |
} | |
} | |
} |
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 NSData { | |
var hex: String { | |
let pointer = UnsafePointer<UInt8>(self.bytes) | |
var hex = "" | |
for i in 0..<self.length { | |
hex += String(format: "%02x", pointer[i]) | |
} | |
return hex | |
} | |
} |
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
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { | |
guard #available(iOS 9.0, *) else { | |
return | |
} | |
// MAX FORCE! | |
guard let maxForce = touches.maxElement({ $0.force < $1.force })?.force where maxForce > 3 else { | |
return | |
} | |
// Forceful touching is going on. | |
} |
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
import UIKit | |
extension String { | |
var hexColor: UIColor? { | |
let hex = self.stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet().invertedSet) | |
var int = UInt32() | |
guard NSScanner(string: hex).scanHexInt(&int) else { | |
return nil | |
} | |
let a, r, g, b: UInt32 |
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
function format(strings, ...values) { | |
const pieces = []; | |
for (let [index, string] of strings.entries()) { | |
let value = values[index]; | |
const formatIndex = string.lastIndexOf('$'); | |
if (formatIndex != -1) { | |
const format = string.substr(formatIndex + 1); | |
string = string.substr(0, formatIndex); | |
// TODO: Use an actual format engine here. |