Skip to content

Instantly share code, notes, and snippets.

@bkase
Created January 5, 2018 06:10
Show Gist options
  • Save bkase/c6ac72f9294abc3610c9a75f2534142e to your computer and use it in GitHub Desktop.
Save bkase/c6ac72f9294abc3610c9a75f2534142e to your computer and use it in GitHub Desktop.
Unary-indexed Collection (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
import Foundation
struct SampleCollection: LazyCollectionProtocol, RandomAccessCollection {
typealias Indices = DefaultRandomAccessIndices<SampleCollection>
let xs = ["a", "b", "c"]
var startIndex: String {
return ""
}
var endIndex: String {
return "xxx"
}
func index(after i: String) -> String {
return i + "x"
}
func index(before i: String) -> String {
return String((0..<(i.count-1)).map{ _ in "x" })
}
subscript(i: String) -> String {
return xs[i.count]
}
}
/*struct LazyShuffleRandomAccessCollection<Base: RandomAccessCollection>: LazyCollectionProtocol {
/*typealias Index = Base.Index
typealias IndexDistance = Base.IndexDistance
typealias SubSequence = Base.SubSequence
typealias Indices = Base.Indices
typealias Iterator = Base.Iterator*/
let base: RandomAccessCollection
var startIndex: Index {
self.base.startIndex
}
var endIndex: Index {
self.base.endIndex
}
func makeIterator() -> Iterator {
return self.base.makeIterator()
}
func index(after: IndexDistance) -> Index {
return self.base.index(after: after)
}
/*subscript(bounds: Range<Index>) -> SubSequence {
return self.base[bounds]
}*/
} */
let x = [1,2,3]
let y = sequence(first: 0, next: { i in i + 1 }).lazy.map{ x in x + 1 }.filter{ x in x % 2 == 0 }
print(SampleCollection().map{ x in x + "!" }["xx"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment