Last active
December 14, 2015 21:19
-
-
Save mesagie/5150358 to your computer and use it in GitHub Desktop.
Are all members of List a greater than corresponding members of list b.
This file contains 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
val a = List(2,3,4) | |
val b = List(1,2,3) | |
// O(n^2) and not very elegant. | |
(0 until a.size) forall (i => a(i) > b(i)) // true | |
// O(n) but creates tuples and a temporary list. Yet, more elegant. | |
a zip b forall (x=> x._1 > x._2) // true | |
// same as above but doesn't create a temporary list (lazy) | |
a.view zip b forall (x=> x._1 > x._2) // true | |
// O(n), without tuple or temporary list creation, and even more elegant. | |
(a corresponds b)(_ > _) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment