Skip to content

Instantly share code, notes, and snippets.

View oisdk's full-sized avatar

Donnacha Oisín Kidney oisdk

View GitHub Profile
extension CollectionType where SubSequence: CollectionType, SubSequence.Index: BidirectionalIndexType, SubSequence.SubSequence == SubSequence {
var splits: LazyMapCollection<Range<Self.Index>, (Self.SubSequence, Self.SubSequence)> {
return indices.lazy.map { i in
(self.prefixUpTo(i), self.suffixFrom(i))
}
}
var deletes: LazyMapCollection<LazyMapCollection<Range<Self.Index>, (Self.SubSequence, Self.SubSequence)>, [Self.SubSequence.Generator.Element]> {
return splits.map { (f,b) in
Array(f) + b.dropFirst()
}
infix operator /% {associativity right precedence 100 }
public struct Rational {
private let num, den: IntMax
}
public func /%(lhs: IntMax, rhs: IntMax) -> Rational {
let g = gcd(lhs,rhs)
let (n,d) = (lhs / g, rhs / g)
if d < 0 { return Rational(num: n * -1, den: d * -1) }
// IntervalTypes
public protocol OpenIntervalType {
typealias Value
func contains(v: Value) -> Bool
}
public protocol OpenEndedIntervalType: OpenIntervalType {
typealias Value: Comparable
var val: Value { get }
}
@oisdk
oisdk / q8.sql
Last active December 13, 2015 19:38
SELECT hotel_name
FROM hotels
WHERE
city = 'Waterford' AND
hotel_num IN (
SELECT r.hotel_num
FROM rooms AS r
WHERE r.room_num NOT IN (
SELECT b.room_num
FROM bookings AS b
/*
Simple hotel-booking database
Kieran Herley, Dec 2012
*/
-- Populate hotels table
INSERT INTO hotels VALUES
(1,"Hotel Splendide","Cork");
INSERT INTO hotels VALUES
(2,"Seaview Hotel","Galway");
func ==<H: Hashable, E: Equatable>(lhs: [H:E]?, rhs: [H:E]?) -> Bool {
switch (lhs,rhs) {
case let (a?,b?): return a == b
case (nil,nil): return true
default: return false
}
}
enum TransformElement<T> { case Skip, Stop, Continue(T) }
extension TransformElement {
func map<U>(@noescape f: T -> U) -> TransformElement<U> {
switch self {
case .Skip: return .Skip
case .Stop: return .Stop
case let .Continue(x): return .Continue(f(x))
}
}
class Node {
let name: String
init(_ name: String) {
self.name = name
}
}
extension RangeReplaceableCollectionType where Generator.Element == Node {
mutating func node(name: String) -> Node {
if let firstMatch = self.lazy.filter({ n in n.name == name }).first {
if !myCondition, let myValue = getExpensiveComputationResult() {
} else {
}
public struct OrderedDictionary<Tk: Hashable, Tv> : MutableCollectionType {
var keys: [Tk] = []
var values: [Tk:Tv] = [:]
public typealias Index = Int
public var count: Int {
return self.keys.count
}