Created
August 4, 2017 21:17
-
-
Save nesteruk/477fe42113ca2ca64a67193a442e0c88 to your computer and use it in GitHub Desktop.
Swift sequence-of-sequences mindfindfuck
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
import Foundation | |
import XCTest | |
class SingleValue : Sequence | |
{ | |
var value = 0 | |
init() {} | |
init(_ value: Int) | |
{ | |
self.value = value | |
} | |
func makeIterator() -> IndexingIterator<Array<SingleValue>> | |
{ | |
return IndexingIterator(_elements: [self]) | |
} | |
} | |
class ManyValues : Sequence | |
{ | |
var values = [Int]() | |
func makeIterator() -> IndexingIterator<Array<Int>> | |
{ | |
return IndexingIterator(_elements: values) | |
} | |
func add(_ value: Int) | |
{ | |
values.append(value) | |
} | |
} | |
extension Sequence where Iterator.Element : Sequence, | |
Iterator.Element.Iterator.Element == Int | |
{ | |
func sum() -> Int | |
{ | |
var result = 0 | |
for c in self | |
{ | |
for i in c | |
{ | |
result += i | |
} | |
} | |
return result | |
} | |
} | |
class UMBaseTestCase : XCTestCase {} | |
//@testable import Test | |
class Evaluate: UMBaseTestCase | |
{ | |
func simpleTest() | |
{ | |
let singleValue = SingleValue(11) | |
let otherValues = ManyValues() | |
otherValues.add(22) | |
otherValues.add(33) | |
let stuff = [singleValue, otherValues] | |
XCTAssertEqual(66, stuff.sum()) | |
} | |
} | |
extension Evaluate | |
{ | |
static var allTests : [(String, (Evaluate) -> () throws -> Void)] | |
{ | |
return [ | |
("simpleTest", simpleTest) | |
] | |
} | |
} | |
func main() | |
{ | |
XCTMain([testCase(Evaluate.allTests)]) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment