Skip to content

Instantly share code, notes, and snippets.

@joshareed
Created April 16, 2011 03:12
Show Gist options
  • Save joshareed/922828 to your computer and use it in GitHub Desktop.
Save joshareed/922828 to your computer and use it in GitHub Desktop.
List.metaClass.safeFirst = { -> delegate[0] }
List.metaClass.safeLast = { -> delegate.size() > 0 ? delegate[-1] : null }
def a = []
assert null == a.safeFirst()
assert null == a.safeLast()
a += [1,2]
assert 1 == a.safeFirst()
assert 2 == a.safeLast()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment