int groups = 7 //divide list up into groups by % (their remainder) Map a = (1..50).groupBy({ it % groups }) as TreeMap assert a.keySet() == (0..<groups) as Set assert a.keySet() == [0, 1, 2, 3, 4, 5, 6] as Set //pull out specific keys (plus vals) from a Map assert a.subMap(1) == [1:[1, 8, 15, 22, 29, 36, 43, 50]] assert a.subMap([1,2]) == [1:[1, 8, 15, 22, 29, 36, 43, 50], 2:[2, 9, 16, 23, 30, 37, 44]] assert a.subMap([1,2,99]) == [1:[1, 8, 15, 22, 29, 36, 43, 50], 2:[2, 9, 16, 23, 30, 37, 44], 99:null] assert a.subMap([1,2,99]).findAll({k,v->!!v}) == [1:[1, 8, 15, 22, 29, 36, 43, 50], 2:[2, 9, 16, 23, 30, 37, 44]] assert a.subMap((1..3)) == [1:[1, 8, 15, 22, 29, 36, 43, 50], 2:[2, 9, 16, 23, 30, 37, 44], 3:[3, 10, 17, 24, 31, 38, 45]]
Monday, July 29, 2013
GroupBy and SubMap
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment