Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / TupleOfOptionals.swift
Last active August 29, 2015 14:10
Upconverted function returning tuple of optionals corrupts on destructuring return value
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
@airspeedswift
airspeedswift / WeirdInference.swift
Last active August 29, 2015 14:10
Weird 1-liner type inference behavior
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>> {
@airspeedswift
airspeedswift / WeirdInferenceNarrowed.swift
Created November 29, 2014 01:37
Weird Inference – narrowed down a bit
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>> {
@airspeedswift
airspeedswift / Zip2Equals.swift
Last active August 29, 2015 14:10
Zip2 that uses optionals for unequal lengths, plus using it to implement equal for sequences
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?)>
@airspeedswift
airspeedswift / SlidingWindowZipGenerator.swift
Created December 9, 2014 16:24
Re-using ZipGenerator2 to create a sliding window
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) }
}
@airspeedswift
airspeedswift / DictOfArraysToArrayOfDicts
Last active August 29, 2015 14:12
Converting a dictionary of [key : [values]] to a an array of dictionaries [ [key:value] ] using zipWith
// 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:
@airspeedswift
airspeedswift / optbug.swift
Created December 30, 2014 12:43
Different calculation results between -Onone and -O with inout
// import Foundation
struct S {
var i: Int
}
func f(inout s: S) -> Int {
s.i += 1
return s.i
}
func measure(title: String?, call: () -> Void) {
let startTime = CACurrentMediaTime()
call()
let endTime = CACurrentMediaTime()
if let title = title {
print("\(title): ")
}
println("Time - \(endTime - startTime)")
}
@airspeedswift
airspeedswift / moveInitializeFrom.swift
Last active August 29, 2015 14:12
Runtime error when using moveInitializeFrom
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)") }
}
@airspeedswift
airspeedswift / UnsafeCollection.swift
Last active August 29, 2015 14:12
Minimal example of a collection using UnsafeMutablePointer
// Note this is a class not a struct, so it does NOT have value semantics
public class UnsafeCollection<T> {
private var _len: Int = 0
private var _buflen: Int = 0
private var _buf: UnsafeMutablePointer<T> = nil
public func removeAll(keepCapacity: Bool = false) {
_buf.destroy(_len)
_len = 0
if !keepCapacity {