Skip to content

Instantly share code, notes, and snippets.

@oisdk
Created September 4, 2015 12:17
Show Gist options
  • Save oisdk/50c84bb59fb71f306cf0 to your computer and use it in GitHub Desktop.
Save oisdk/50c84bb59fb71f306cf0 to your computer and use it in GitHub Desktop.
protocol NListType : SequenceType {
typealias Element
init()
var ar: [Element] { get }
}
extension NListType {
func generate() -> IndexingGenerator<[Element]> {
return ar.generate()
}
var count: Int { return ar.count }
}
struct Empty<Element> : NListType {
init() {}
let ar: [Element] = []
}
struct NList<Element, Tail: NListType where Tail.Element == Element> : NListType {
init(_ head: Element, _ tail: Tail = Tail()) {
self.tail = tail
self.head = head
}
init() { fatalError() }
var ar: [Element] { return [head] + tail.ar }
let tail: Tail
let head: Element
}
infix operator |> { associativity right precedence 90 }
func |><E, L : NListType where L.Element == E>(lhs: E, rhs: L) -> NList<E, L> {
return NList(lhs, rhs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment