Skip to content

Instantly share code, notes, and snippets.

@natecook1000
natecook1000 / Decimal+FloatingPoint.swift
Created July 21, 2017 20:13
FloatingPoint conformance for Decimal
import Foundation
extension Decimal.RoundingMode {
init(_ rule: FloatingPointRoundingRule, for value: Decimal) {
switch rule {
case .down: self = .down
case .up: self = .up
case .awayFromZero: self = value < 0 ? .down : .up
case .towardZero: self = value < 0 ? .up : .down
prefix func !<T>(predicate: @escaping (T) -> Bool) -> (T) -> Bool {
return { !predicate($0) }
}
func isEven(_ x: Int) -> Bool {
return x % 2 == 0
}
let nums = [5, 7, 3, 4, 2, 3, 4]
nums.first(where: isEven) // 4
extension Collection {
func sliced(
where predicate: (Element, Element) -> Bool
) -> [SubSequence] {
var result: [SubSequence] = []
var start = startIndex
for (i, j) in zip(indices, indices.dropFirst()) {
if predicate(self[i], self[j]) {
result.append(self[start..<j])
extension Sequence {
/// Returns an array with the transformed elements of the sequence if all
/// the transformations succeed.
func flatMapAll<T>(_ transform: (Element) -> T?) -> [T]? {
var result: [T] = []
result.reserveCapacity(underestimatedCount)
for element in self {
if let transformed = transform(element) {
result.append(transformed)
} else {
extension Dictionary {
var storageID: UInt {
var copy = self
return withUnsafeBytes(of: &copy) {
$0.baseAddress!.load(as: UInt.self)
}
}
}
var a = [1: 1, 2: 2]
@natecook1000
natecook1000 / ContainmentSet.swift
Last active November 12, 2018 12:28
ContainmentSet & PredicateSet
protocol ContainmentSet {
associatedtype SetElement
func contains(_ element: SetElement) -> Bool
func intersection(_: Self) -> Self
func union(_: Self) -> Self
func subtracting(_: Self) -> Self
func symmetricDifference(_: Self) -> Self
}
struct LazySplitSequence<Base: Sequence> : Sequence, LazySequenceProtocol {
struct Iterator : IteratorProtocol {
mutating func next() -> [Base.Element]? {
var result: [Base.Element] = []
if splits == 0 {
while let element = iterator.next() {
result.append(element)
}
return result.isEmpty ? nil : result
@natecook1000
natecook1000 / open_current.script
Created October 8, 2017 20:39
Open new Terminal tab with frontmost Finder window
on run
tell application "Finder" to set myDir to POSIX path of (insertion location as alias)
tell application "Terminal"
activate
if not (exists window 1) then
reopen
else
tell application "System Events" to keystroke "t" using command down
end if
do script "cd " & quoted form of myDir in window 1
struct Zip2Collection<C1: Collection, C2: Collection> : Collection {
enum Index: Comparable {
case index(C1.Index, C2.Index)
case end
static func <(lhs: Index, rhs: Index) -> Bool {
switch (lhs, rhs) {
case (.end, _): return false
case (_, .end): return true
case let (.index(l, _), .index(r, _)):
extension Collection {
func nth(_ n: Int) -> Element? {
assert(n >= 0, "Can't get a negative-th element")
guard let i = index(startIndex, offsetBy: numericCast(n), limitedBy: endIndex),
i != endIndex
else { return nil }
return self[i]
}
}