This file contains hidden or 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
func myDropFirst<S: SequenceType>(seq: S) -> SequenceOf<S.Generator.Element> { | |
return SequenceOf { ()->GeneratorOf<S.Generator.Element> in | |
var g = seq.generate() | |
let first = g.next() | |
// if you prefer your dropFirst to explodinate on empty | |
// sequences, add an assert(first != nil) here... | |
return GeneratorOf { | |
// shouldn't call GeneratorType.next() | |
// after it's returned nil the first time | |
first == nil ? nil : g.next() |
This file contains hidden or 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
func f()->(Int,Int) { | |
return (1,1) | |
} | |
func g(f: ()->(Int?,Int?)) -> (Int,Int) { | |
let (a,b) = f() | |
return (a ?? 0, b ?? 0) | |
} | |
// ()->(Int,Int) is substitutable for |
This file contains hidden or 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
infix operator |> { | |
associativity left | |
} | |
func |><T,U>(t: T, f: T->U) -> U { | |
return f(t) | |
} | |
// this ought to be enough... | |
func reverse<T: CollectionType>(source: LazyRandomAccessCollection<T>) -> LazyBidirectionalCollection<RandomAccessReverseView<T>> { |
This file contains hidden or 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
infix operator |> { | |
associativity left | |
} | |
func |><A,B>(a: A, f: A->B) -> B { | |
return f(a) | |
} | |
// this ought to be enough... | |
func reverse<D: CollectionType where D.Index : BidirectionalIndexType>(source: LazyRandomAccessCollection<D>) -> LazyBidirectionalCollection<RandomAccessReverseView<D>> { |
This file contains hidden or 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
public struct ZipOuter2<S1: SequenceType, S2: SequenceType>: SequenceType { | |
private let _s1: S1 | |
private let _s2: S2 | |
init(_ s1: S1, _ s2: S2) { | |
_s1 = s1 | |
_s2 = s2 | |
} | |
public typealias Generator = GeneratorOf<(S1.Generator.Element?, S2.Generator.Element?)> |
This file contains hidden or 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
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? { | |
for x in seq { | |
if predicate(x) { return x } | |
} | |
return nil | |
} | |
func not<T>(predicate: T -> Bool) -> (T -> Bool) { | |
return { x in !predicate(x) } | |
} |
This file contains hidden or 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
// from http://stackoverflow.com/questions/27635981/transform-from-dictionary-to-array-in-swift-without-a-for-loop | |
// | |
// Given [ | |
// "title" : ["abc", "def"], | |
// "time" : ["1234", "5678", "0123"], | |
// "content":["qwerty", "asdfg", "zxcvb"] | |
// ] | |
// | |
// how do you get to this: |
This file contains hidden or 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 | |
struct S { | |
var i: Int | |
} | |
func f(inout s: S) -> Int { | |
s.i += 1 | |
return s.i | |
} |
This file contains hidden or 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
func measure(title: String?, call: () -> Void) { | |
let startTime = CACurrentMediaTime() | |
call() | |
let endTime = CACurrentMediaTime() | |
if let title = title { | |
print("\(title): ") | |
} | |
println("Time - \(endTime - startTime)") | |
} |
This file contains hidden or 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
let count = 12 | |
// Simple class that prints when it is created and destroyed | |
class Whiner { | |
let _i: Int | |
init(_ i: Int) { _i = i; println("Init: \(_i)") } | |
deinit { println("Deinit: \(_i)") } | |
} |