Monday, December 16, 2013

difference between get and nth

1. get is more general, nth works with numerical collections only, like lists, sequence, array
2. whereas nth throws an IndexOutOfBoundException, get returns nil.
3. get can be used work with unsupported object types, nth does not allow this. e.g.
(get 42 0)
;= nil
(nth 42 0)
;= java.lang.UnsupportedOperationException: nth not supported on this type: Long

The same is that they treat ‘default’ value retrieval the same way
(nth [:a :b :c] -1 :not-found)
;= :not-found
(get [:a :b :c] -1 :not-found)
;= :not-found

No comments:

Post a Comment