Skip to content

Instantly share code, notes, and snippets.

Ben Cohen airspeedswift

Block or report user

Report or block airspeedswift

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
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 / 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 / rbt.swift
Created Jul 22, 2015
Red Black Tree with indirect and pattern matching for Swift 2.0b4
View rbt.swift
enum Color { case R, B }
indirect enum Tree<Element: Comparable> {
case Empty
case Node(Color,Tree<Element>,Element,Tree<Element>)
init() { self = .Empty }
init(_ x: Element, color: Color = .B,
left: Tree<Element> = .Empty, right: Tree<Element> = .Empty)
@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 {
@airspeedswift
airspeedswift / list.swift
Last active Jun 18, 2018
indirect enum List as CollectionType
View list.swift
// A simple linked list using enums:
enum List<Element> {
case End
indirect case Node(Element, List<Element>)
func cons(x: Element) -> List<Element> {
return .Node(x, self)
}
}
@airspeedswift
airspeedswift / spelling.swift
Last active May 16, 2018
Norvig Spellchecker in Swift
View spelling.swift
/// Translation of [Peter Norvig's spell checker](http://norvig.com/spell-correct.html) into Swift.
/// Sample input corpus [here](http://norvig.com/big.txt)
import Foundation.NSString // purely for IO, most things done with Swift.String
// pythony slicing
postfix operator ..< { }
prefix operator ..< { }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I> { return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I> { return RangeEnd(end: rhs) }
@airspeedswift
airspeedswift / RBTree.swift
Created Oct 4, 2015
red black tree updated for 2.1b2
View RBTree.swift
private enum ListNode<Element> {
case End
indirect case Node(Element, next: ListNode<Element>)
func cons(x: Element) -> ListNode<Element> {
return .Node(x, next: self)
}
}
public struct ListIndex<Element> {
@airspeedswift
airspeedswift / OrderedSet.swift
Created May 11, 2015
Swift Ordered Set based on Red/Black tree
View OrderedSet.swift
enum Color {
case Red, Black
}
class Tree<T: Comparable> {
let value: T
let left: Tree<T>?
let right: Tree<T>?
let color: Color
You can’t perform that action at this time.