Skip to content

Instantly share code, notes, and snippets.

Ben Cohen airspeedswift

View GitHub Profile
View EitherCollection.swift
////===--- EitherCollection.swift - A collection of two different types -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
View EitherSequence.swift
////===--- EitherSequence.swift - A sequence type-erasing two sequences -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
View cowed.swift
/// Conform references types for use in the COW wrapper to this protocol
protocol Cowable: class {
/// Make a new unique instance of `copied`
static func makeUnique(_ copied: Self) -> Self
}
/// A wrapper that turns a Cowable reference type into a value-semantic
/// type with access to all of its properties
@dynamicMemberLookup
View gist:f413beedc42efe76263466263bf80d24
extension Sequence {
func count(where p: (Element)->Bool)->Int {
return reduce(0) { p($1) ? $0 + 1 : $0 }
}
}
let fn: ((Int) -> Bool) -> Int = [1,2,3].count
let n = fn { $0<2 }
print(n) // 1
@airspeedswift
airspeedswift / Bounded.swift
Created Oct 26, 2018
Bound a sequence with a start and end marker
View Bounded.swift
struct BoundedSequence<Base: Sequence> {
let _base: Base
}
extension BoundedSequence {
struct Iterator {
enum State { case starting, iterating, ended }
var _state: State
var _iterator: Base.Iterator
}
@airspeedswift
airspeedswift / usort.swift
Created Aug 16, 2018
Unsafe stable mergesort
View usort.swift
extension UnsafeMutableBufferPointer {
public mutating func merge(
using aux: UnsafeMutableBufferPointer<Element>,
_ lo: Int, _ mid: Int, _ hi: Int,
by isOrderedBefore: (Element, Element) -> Bool
) {
assert(hi <= self.endIndex)
assert(self.count == aux.count)
let from = self.baseAddress!, to = aux.baseAddress!
@airspeedswift
airspeedswift / permute.swift
Last active Jul 26, 2018
Permutation for MutableCollection
View permute.swift
import QuartzCore
/// Reorder elements in-place into the next permutation, returning false when
/// the collection has been permuted into lexicographical order. To cycle
/// through all permutations, start with a lexicographically-sorted collection
/// and permute until it returns false.
///
/// - Complexity: O(*n*), where *n* is the length of the collection.
public extension MutableCollection where Self: BidirectionalCollection {
View MyReverse.swift
struct MyReversed<Base: BidirectionalCollection> {
let base: Base
}
extension MyReversed: Collection {
public enum Index: Comparable {
case element(Base.Index)
case end
View joke.swift
func tellJoke(name: String, character: Character) {
let punchline = name.filter { $0 != character }
let n = name.count - punchline.count
let joke = """
Q: Why does \(name) have \(n) \(character)'s in their name?
A: I don't know, why does \(name) have \(n) \(character)'s in their name?
Q: Because otherwise they'd be called \(punchline).
"""
print(joke)
@airspeedswift
airspeedswift / lazysplit.swift
Created Jun 6, 2017
Lazy split: Swift 3 vs Swift 4
View lazysplit.swift
// Swift 4.0 (and 3.2 as well!)
struct LazySplit<Base: Collection>: Collection
where Base.Element: Equatable,
Base.SubSequence: Collection
{
struct Index: Comparable {
let start, end: Base.Index
static func ==(lhs: Index, rhs: Index) -> Bool { return lhs.start == rhs.start }
static func < (lhs: Index, rhs: Index) -> Bool { return lhs.start < rhs.start }
You can’t perform that action at this time.