Skip to content

Instantly share code, notes, and snippets.

@nesteruk
Created August 4, 2017 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesteruk/477fe42113ca2ca64a67193a442e0c88 to your computer and use it in GitHub Desktop.
Save nesteruk/477fe42113ca2ca64a67193a442e0c88 to your computer and use it in GitHub Desktop.
Swift sequence-of-sequences mindfindfuck
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