This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct MyReversed<Base: BidirectionalCollection> { | |
let base: Base | |
} | |
extension MyReversed: Collection { | |
public enum Index: Comparable { | |
case element(Base.Index) | |
case end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private enum ListNode<Element> { | |
case End | |
indirect case Node(Element, tag: Int, next: ListNode<Element>) | |
/// Computed property to fetch the tag. .End has an | |
/// implicit tag of zero. | |
var tag: Int { | |
switch self { | |
case .End: return 0 | |
case let .Node(_, tag: n, _): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class MyArrayBuffer<Element>: ManagedBuffer<Int,Element> { | |
func clone() -> MyArrayBuffer<Element> { | |
return self.withUnsafeMutablePointerToElements { elements -> MyArrayBuffer<Element> in | |
return MyArrayBuffer<Element>.create(self.allocatedElementCount) { newBuf in | |
newBuf.withUnsafeMutablePointerToElements { newElems->Void in | |
newElems.initializeFrom(elements, count: self.value) | |
} | |
return self.value |