Skip to content

Instantly share code, notes, and snippets.

@mesagie
Last active December 14, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mesagie/5150358 to your computer and use it in GitHub Desktop.
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.
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