This file contains 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 version of the spelling checker described in http://norvig.com/spell-correct.html | |
import Foundation | |
let alphabet = "abcdefghijklmnopqrstuvwxyz" | |
/// Given a word, produce a set of possible edits with one character | |
/// transposed, deleted, replaced or a rogue character inserted | |
func edits(_ word: String) -> Set<String> { |
This file contains 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
indirect enum Tree<Element: Comparable> { | |
enum Color { case R, B } | |
case empty | |
case node(Color, Tree<Element>, Element, Tree<Element>) | |
init() { self = .empty } | |
init( |
This file contains 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
// To run this code on a Mac with Xcode installed: | |
// Download the latest toolchain from https://www.swift.org/download/#trunk-development-main and install it | |
// From a command line: | |
// export TOOLCHAINS=`plutil -extract CFBundleIdentifier raw -o - /Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist` | |
// xcrun swiftc -parse-as-library -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature MoveOnlyPartialConsumption -Xfrontend -disable-round-trip-debug-types -enable-experimental-feature BorrowingSwitch linkedlist.swift | |
struct Box<Wrapped: ~Copyable>: ~Copyable { | |
private let pointer: UnsafeMutablePointer<Wrapped> | |
This file contains 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
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, |
This file contains 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
////===--- 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 | |
//// |
This file contains 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
////===--- 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 | |
//// |
This file contains 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
/// 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 |
This file contains 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 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 |
This file contains 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 BoundedSequence<Base: Sequence> { | |
let _base: Base | |
} | |
extension BoundedSequence { | |
struct Iterator { | |
enum State { case starting, iterating, ended } | |
var _state: State | |
var _iterator: Base.Iterator | |
} |
This file contains 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 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! |
NewerOlder