Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Zips two lists into the map.
- Size of the map is that of the shortest list.
- Items from the first list become keys and items from
- the second one become values
- */
- def zip(xs, ys) {
- if (xs && ys) {
- [(xs[0]):(ys[0])] + zip(xs.size() > 1 ? xs[1..-1] :[],
- ys.size() > 1 ? ys[1..-1] : [])
- } else {
- [:]
- }
- }
- assert zip([], []) == [:]
- assert zip([], [1]) == [:]
- assert zip([1], []) == [:]
- assert zip(['a'], [1]) == [a:1]
- assert zip(('a'..'b'), [1]) == [a:1]
- assert zip(['a'], [1,2]) == [a:1]
- assert zip(('a'..'c'), (1..3)) == [a:1, b:2, c:3]
- /*
- Unzips the map into list of lists
- where the first list will contain all the keys
- and the second list will contain all teh values
- */
- def unzip(zs) {
- def keys = zs.keySet().toList()
- if (keys) {
- def key = keys[0]
- def res = unzip(keys.size() > 1 ? zs.subMap(keys[1..-1]) : [:])
- [[key] + /*list of keys*/ res[0], [zs[key]] + /*list of values*/ res[1]]
- } else {
- [[],[]]
- }
- }
- assert unzip([:]) == [[],[]]
- assert unzip([a:1]) == [['a'],[1]]
- assert unzip([a:1, b:2, c:3]) == [['a', 'b', 'c'], [1, 2, 3]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement