Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Last active March 28, 2019 02:54
Show Gist options
  • Save LucianoPAlmeida/b0f3c12e74f303b4caf8f73e5b5eafe1 to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/b0f3c12e74f303b4caf8f73e5b5eafe1 to your computer and use it in GitHub Desktop.
let array = [1, 2, 3, 4, 5]
let r1: [Int] = array.reversed() // Eager reverse implementation is O(n) when call the function
let r2: ReversedCollection<[Int]> = array.reversed() // Lazy reversed collection is O(1) when you call the method.
r1.first { $0 == 3 } // Interate 3 times + O(n) complexity of eager call.
r2.first { $0 == 3 } // Interate 3 times + O(1) of lazy call. For a large array, this is an example where using a lazy type is more performant than an eager one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment