Created
June 16, 2015 17:24
-
-
Save oisdk/e5d42984f2faebfb68cb to your computer and use it in GitHub Desktop.
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 SimpleGen<T> : GeneratorType { | |
typealias Element = T | |
private let inc: T -> T | |
private var cur: T | |
mutating func next() -> Element? { | |
defer { cur = inc(cur) } | |
return cur | |
} | |
} | |
struct StartRange<I : ForwardIndexType> { private let start: I } | |
struct EndRange <I : ForwardIndexType> { private let end: I } | |
struct From<I : ForwardIndexType> : SequenceType { | |
typealias Generator = SimpleGen<I> | |
private let f: I | |
func generate() -> Generator { return SimpleGen(inc: {$0.successor()}, cur: f) } | |
} | |
prefix operator ..< {} | |
postfix operator ..< {} | |
postfix operator ... {} | |
postfix func ..<<I : ForwardIndexType>(from: I) -> StartRange<I> { | |
return StartRange(start: from) | |
} | |
prefix func ..<<I : ForwardIndexType>(to: I) -> EndRange<I> { | |
return EndRange(end: to) | |
} | |
postfix func ...<I : ForwardIndexType>(from: I) -> From<I> { | |
return From(f: from) | |
} | |
extension Sliceable { | |
subscript(i : StartRange<Index>) -> SubSlice { return self[i.start..<self.endIndex] } | |
subscript(i : EndRange<Index> ) -> SubSlice { return self[self.startIndex..<i.end] } | |
} | |
extension CollectionType { | |
subscript(r: Range<Index>, by: Index.Distance) -> [Generator.Element] { | |
var ret: [Generator.Element] = [] | |
for (var i = r.startIndex; r.contains(i); i = advance(i, by)) { ret.append(self[i]) } | |
return ret | |
} | |
subscript(r: StartRange<Index>, by: Index.Distance) -> [Generator.Element] { | |
return self[r.start..<self.endIndex, by] | |
} | |
subscript(r: EndRange<Index>, by: Index.Distance) -> [Generator.Element] { | |
return self[self.startIndex..<r.end, by] | |
} | |
subscript(by by: Index.Distance) -> [Generator.Element] { | |
return self[self.startIndex..<self.endIndex, by] | |
} | |
} | |
extension CollectionType where Index : Strideable { | |
subscript(r: Range<Index>, by: Index.Stride) -> [Generator.Element] { | |
return stride(from: r.startIndex, to: r.endIndex, by: by).map{self[$0]} | |
} | |
subscript(r: StartRange<Index>, by: Index.Stride) -> [Generator.Element] { | |
return self[r.start..<self.endIndex, by] | |
} | |
subscript (r: EndRange<Index>, by: Index.Stride) -> [Generator.Element] { | |
return self[self.startIndex..<r.end, by] | |
} | |
subscript(by by: Index.Stride) -> [Generator.Element] { | |
return self[self.startIndex..<self.endIndex, by] | |
} | |
} | |
let ar = [1, 2, 3, 4, 5, 6, 7, 8] | |
ar[4..<] // [5, 6, 7, 8] | |
ar[..<4] // [1, 2, 3, 4] | |
ar[by: 2] // [1, 3, 5, 7] | |
ar[4..<, 2] // [5, 7] | |
ar[..<4, 2] // [1, 3] | |
ar[2..<7, 3] // [3, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment