Skip to content

Instantly share code, notes, and snippets.

View adelatorrefoss's full-sized avatar

Toño de la Torre adelatorrefoss

View GitHub Profile
@adelatorrefoss
adelatorrefoss / gist:631fdcb2ca481d072546
Created December 26, 2014 11:29
Awesomeness in Sets - separate the unique and duplicating items in a list
// Examples with Lists and Sets
// For small numbers of items, it's common in Groovy to use a list for set processing, and only convert it to a set when necessary, eg, for output.
// Though the uniqueness of set items is useful for some processing, for example, if we want to separate the unique and duplicating items in a list:
list=[1,2,7,2,2,4,7,11,5,2,5]
def uniques= [] as Set, dups= [] as Set
list.each{ uniques.add(it) || dups.add(it) }
uniques.removeAll(dups)
assert uniques == [1,4,11] as Set && dups == [2,5,7] as Set
@adelatorrefoss
adelatorrefoss / RepositoryWithGenerics.groovy
Last active August 29, 2015 14:03
Repository implementation with generics
interface Repository<T> {
T save(T t)
}
interface IntegerRepository {
Integer findByIntegers(String name)
}
abstract class InMemoryRepository<A> implements Repository<A> {
A save(A a) {