Skip to content

Instantly share code, notes, and snippets.

Ben Cohen airspeedswift

View GitHub Profile
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 }
View dropPrefix.swift
// this particular API/implementation for demonstration only,
// not necessarily quite what will be proposed
extension Collection where SubSequence == Self {
/// Drop n elements from the front of `self` in-place,
/// returning the dropped prefix.
mutating func dropPrefix(_ n: IndexDistance) -> SubSequence {
// nature of error handling/swallowing/trapping/optional
// returning here TBD...
let newStart = index(startIndex, offsetBy: n)
defer { self = self[newStart..<endIndex] }
@airspeedswift
airspeedswift / NonemptyCollection.swift
Created Dec 17, 2016
Non-empty collection in Swift
View NonemptyCollection.swift
protocol NonemptyCollection: Collection {
var first: Iterator.Element { get }
}
enum NonemptyIndex<Base: Collection>: Comparable {
case head
case tail(Base.Index)
static func ==(lhs: NonemptyIndex, rhs: NonemptyIndex) -> Bool {
switch (lhs,rhs) {
@airspeedswift
airspeedswift / mergesort.swift
Last active Jan 3, 2017
Swift 3 Merge Sort
View mergesort.swift
extension RangeReplaceableCollection
where
// Index: RandomAccessIndexType → Self: RandomAccessCollection
Self: RandomAccessCollection,
// still need this, until generics allow constraints to guarantee
// this is the case for all collections...
SubSequence.Iterator.Element == Iterator.Element {
// mutating in-place operations should be imperative verb phrases,
// so let's rename this stablySort
You can’t perform that action at this time.