Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
Last active August 29, 2015 14:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airspeedswift/091cbba5d75d94f3acc7 to your computer and use it in GitHub Desktop.
Save airspeedswift/091cbba5d75d94f3acc7 to your computer and use it in GitHub Desktop.
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>> {
return source.reverse()
}
let double = { $0*2 }
// y is a lazy bi-directional collection
let y = 1...10 |> lazy |> reverse
// and y.map returns a lazy bi-directional collection
let z = y.map(double)
// yet this results in x: [Int]
let x = 1...10 |> lazy |> reverse |> { $0.map(double) }
// i could force it to choose the types it does with the two-line version but
// that is not, uhm, ideal...
let l = 1...10 |> lazy |> reverse |> { (c: LazyBidirectionalCollection<RandomAccessReverseView<Range<Int>>>) -> LazyBidirectionalCollection<MapCollectionView<RandomAccessReverseView<Range<Int>>, Int>> in c.map(double) }
// something about doing it on 1 line makes the version of reverse that returns an array kick in
// the root cause is partly that LazyRandomAccessCollection.reverse returns a
// LazyBidirectionalCollection not a random-access one for some reason
// this fixes it, but I still don't quite understand why it's happening
// in the first place...
//func reverse<C: CollectionType where C.Index: BidirectionalIndexType>(source: LazyBidirectionalCollection<C>) -> LazyBidirectionalCollection<BidirectionalReverseView<C>> {
// return source.reverse()
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment