Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

// Monoid
precedencegroup MonoidPrecedence {
associativity: left
}
infix operator <> : MonoidPrecedence
protocol Monoid {
static var empty: Self { get }
extension Sequence {
func min(_ n: Int, by areInIncreasingOrder: (Element, Element) -> Bool) -> [Element] {
var iterator = makeIterator()
guard let first = iterator.next() else { return [] }
return withoutActuallyEscaping(areInIncreasingOrder) { areInIncreasingOrder in
var heap = NonEmptyMaxHeap(root: first, by: areInIncreasingOrder)
var heapSize = 1
while heapSize < n, let element = iterator.next() {
extension String {
init<T>(dumping x: T) {
self.init()
dump(x, to: &self)
}
}
func assertDumpsEqual<T>(_ lhs: @autoclosure () -> T, _ rhs: @autoclosure () -> T, file: StaticString = #file, line: UInt = #line) {
assert(String(dumping: lhs()) == String(dumping: rhs()), "Expected dumps to be equal.", file: file, line: line)
}
func product<A: Collection, B: Collection>(_ a: A, _ b: B) -> CartesianProductCollection<A, B> {
return CartesianProductCollection(a, b)
}
struct CartesianProductCollection<A: Collection, B: Collection> {
let a: A
let b: B
init(_ a: A, _ b: B) {
self.a = a