Skip to content

Instantly share code, notes, and snippets.

extension Dictionary {
var storageID: UInt {
var copy = self
return withUnsafeBytes(of: &copy) {
$0.baseAddress!.load(as: UInt.self)
}
}
}
var a = [1: 1, 2: 2]
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 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])
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
@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
/// A sequence of weakly held elements.
///
/// Iterating over this sequence yields the elements that still exist at the
/// moment of iteration. Subsequent iterations may not yield the same elements,
/// as some elements may have been removed from memory.
public struct WeakSequence<Element: AnyObject> : Sequence {
var _contents: [Box] = []
/// A box for weakly holding a class instance.
class Box {
// Additions to make the unsafe pointer APIs more consistent
extension UnsafePointer {
func deallocate(capacity: Int) {
UnsafeMutablePointer(mutating: self).deallocate(capacity: capacity)
}
}
extension UnsafeBufferPointer {
func deallocate() {
@natecook1000
natecook1000 / 0000-dict.md
Last active March 31, 2017 16:13
Dictionary & Set Improvements

Dictionary & Set Enhancements

  • Proposal: SE-0000
  • Author: Nate Cook
  • Review Manager: TBD
  • Status: Awaiting review

Introduction

This proposal comprises a variety of commonly (and less commonly) suggested improvements to the standard library's Dictionary type, from merging initializers to dictionary-specific filter and mapValues methods. The proposed additions to Dictionary, and the corresponding changes to Set, are detailed in the sections below.

@natecook1000
natecook1000 / SwiftCoRoutine.swift
Created March 15, 2017 16:04 — forked from mzaks/SwiftCoRoutine.swift
A simple coroutine for swift
import Foundation
public class CoRoutine<T> {
private var index = 0
private var sequence : [T]
private var routine : (T)->()
private let step : Int?
private let deltaTime : TimeInterval?
private(set) public var isCanceled : Bool = false
private(set) public var isDone : Bool = false
@natecook1000
natecook1000 / LazyFilteredCollectionToArrayBenchmark.swift
Last active March 14, 2017 17:48 — forked from dabrahams/LazyFilteredCollectionToArrayBenchmark.swift
Measure the cost of pre-counting lazy filtered collections when converting to Array, WIP
import Darwin
import CoreFoundation
// A variable we can use with exit() to ensure that the optimizer
// doesn't remove code we want to time
var undead = 0
// A filtered collection that can be more efficiently counted than the stock one.
struct LazyFilterBidirectionalCollection2<Base : BidirectionalCollection> : Collection {
typealias Impl = LazyFilterBidirectionalCollection<Base>