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
typealias TreePointer = UnsafeMutablePointer<Tree>? | |
struct Tree { | |
var left: TreePointer; | |
var right: TreePointer; | |
}; | |
func new_tree(_ left: TreePointer, _ right: TreePointer) -> TreePointer { | |
let new: TreePointer = UnsafeMutablePointer<Tree>.allocate(capacity: 1); | |
new!.initialize(to: Tree(left: left, right: right)) |
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
func compare<T: Comparable, U>(_ x: T, _ y: U) -> Bool { | |
return x < (y as! T) | |
} | |
extension Collection { | |
func sortedIfPossible() -> [Element] { | |
if isEmpty { return []} | |
if Element.self as Any.Type is any Comparable.Type { |
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
protocol NullaryFunction { | |
associatedtype Result | |
func callAsFunction() -> Result | |
} | |
protocol ExpressionResult { | |
associatedtype Expression: NullaryFunction where Expression.Result == Self; | |
} | |
typealias Expr<T: ExpressionResult> = T.Expression |
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 RangeReplaceableCollection { | |
public fun remove<E>(where exclude: [E](Element)->Bool) inout { | |
let end = end_position() | |
var i = first(where: exclude) | |
if i == end { return } | |
let include = | |
var j = self[self.position(after: i)...] | |
.first(where: x => !exclude(x) ) | |
var k = j |
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
// Everything but the slicing-related requirements of Collection | |
protocol CollectionCore { | |
associatedtype Element | |
associatedtype Index | |
func startIndex() -> Index | |
func endIndex() -> Index | |
func index(after i: Index) -> Index |
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
// This file demonstrates how memoization might work in the transformation of a `ScopedProgram` into | |
// a `TypedProgram`, which additionally represents type information. | |
public struct TypedProgram { | |
/// The prior representation of the program, without analysis of types. | |
public let base: ScopedProgram | |
/// A type. | |
public struct Type_: Hashable { | |
let id: Int |
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
// Just to give a sense of reality. | |
struct AST {} | |
/// The wrapper over a dictionary that maps keys onto non-optional | |
/// values, expecting them to have already been computed. | |
struct ImmutablePropertyMap<Key: Hashable, Value> { | |
let storage: Dictionary<Key, Value> | |
subscript(k: Key) -> Value { storage[k]! } | |
} |
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
➜ val git:(interpreter) ✗ swift repl | |
Welcome to Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51). | |
Type :help for assistance. | |
1> let x = [[1], [2, 3]] | |
x: [[Int]] = 2 values { | |
[0] = 1 value { | |
[0] = 1 | |
} | |
[1] = 2 values { |
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 CrossProduct2<Base0: Collection, Base1: Collection>: Collection { | |
public private(set) var base0: Base0 | |
public private(set) var base1: Base1 | |
init(_ base0: Base0, _ base1: Base1) { | |
self.base0 = base0 | |
self.base1 = base1 | |
} | |
struct Index: Comparable { |
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 Grammar { | |
/// Returns the set of nullable symbols (which sometimes derive 𝝐) and the subset of nulling | |
/// symbols (which always derive 𝝐) in a grammar that is not yet in nihilist normal form. | |
func nullSymbolSets(rulesByRHS: MultiMap<Symbol, Rule>) | |
-> (nullable: Set<Symbol>, nulling: Set<Symbol>) | |
{ | |
// - Common setup. | |
// Mapping from symbol to the set of ruleIDs having that symbol as a LHS. | |
let rulesByLHS = MultiMap(grouping: ruleIDs, by: lhs) |
NewerOlder