Last active
March 28, 2019 02:54
-
-
Save LucianoPAlmeida/b0f3c12e74f303b4caf8f73e5b5eafe1 to your computer and use it in GitHub Desktop.
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 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