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 Foundation | |
extension UnicodeScalar : ForwardIndexType { | |
public func successor() -> UnicodeScalar { | |
return UnicodeScalar(value + 1) | |
} | |
} | |
var operatorHeads: [UnicodeScalar] = Array("=-+!*%<>&|^~?".unicodeScalars) | |
operatorHeads += Array("\u{00A1}" ... "\u{00A7}") |
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
protocol StringType { | |
var isEmpty: Bool { get } | |
} | |
extension String : StringType { } | |
extension Optional where Wrapped: StringType { | |
var isNullOrEmpty: Bool { | |
return self?.isEmpty ?? 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
// The MIT License (MIT) | |
// | |
// Copyright (c) 2014 Nate Cook | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: |
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 NSTimer { | |
/** | |
Creates and schedules a one-time `NSTimer` instance. | |
- Parameters: | |
- delay: The delay before execution. | |
- handler: A closure to execute after `delay`. | |
- Returns: The newly-created `NSTimer` instance. | |
*/ |
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
// NSScanner+Swift.swift | |
// A set of Swift-idiomatic methods for NSScanner | |
// | |
// (c) 2015 Nate Cook, licensed under the MIT license | |
import Foundation | |
extension NSScanner { | |
// MARK: Strings |
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
// Curry Recipe | |
func curryRecipe(n: Int) -> String { | |
let types = join(", ", map(1...n, { "T\($0)" })) | |
let returnType = join(" -> ", map(1...n, { "T\($0)" })) | |
let closures = join(" in ", map(1...n, { "{ t\($0)" })) | |
let braces = join(" ", Array(count: n, repeatedValue: "}")) | |
return "func curry<\(types), R>(f: (\(types)) -> R) -> \(returnType) -> R {\r" + | |
" return \(closures) in f(\(types.lowercaseString)) \(braces)\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
extension String.StringInterpolation { | |
mutating func appendInterpolation<T>(_ value: T?, default: @autoclosure () -> String) { | |
self.appendLiteral(value.map(String.init(describing:)) ?? `default`()) | |
} | |
} | |
let num = Int("21") | |
print("The number is: \(num, default: "<invalid>")") | |
// The number is: 21 |
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 String { | |
var isHomogeneous: Bool { | |
if let firstChar = characters.first { | |
for char in dropFirst(characters) where char != firstChar { | |
return false | |
} | |
} | |
return 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
// integerWithBytes.swift | |
// as seen in http://natecook.com/blog/2015/03/a-sanatorium-for-swift-generics/ | |
// | |
// (c) 2015 Nate Cook, licensed under the MIT license | |
protocol BitshiftOperationsType { | |
func <<(lhs: Self, rhs: Self) -> Self | |
func >>(lhs: Self, rhs: Self) -> Self | |
func <<=(inout lhs: Self, rhs: Self) | |
func >>=(inout lhs: Self, rhs: Self) |
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
// NSCalendar+Swift.swift | |
// A set of Swift-idiomatic methods for NSCalendar | |
// | |
// (c) 2015 Nate Cook, licensed under the MIT license | |
extension NSCalendar { | |
/// Returns the hour, minute, second, and nanoseconds of a given date. | |
func getTimeFromDate(date: NSDate) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) { | |
var (hour, minute, second, nanosecond) = (0, 0, 0, 0) | |
getHour(&hour, minute: &minute, second: &second, nanosecond: &nanosecond, fromDate: date) |
NewerOlder