Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def flatten(xs) {
- xs = xs?:[]
- def orig_list = new LinkedList(xs)
- def flat_list = []
- while (orig_list) {
- def elem = orig_list.pollFirst()
- switch (elem) {
- case List:
- elem.reverseEach { orig_list.offerFirst(it) }
- break
- default:
- flat_list << elem
- }
- }
- flat_list
- }
- def original = [[1,2,[3]],4]
- assert flatten(original) == [1, 2, 3, 4]
- assert flatten([]) == []
- assert flatten([[[]],[[[]]],[]]) == []
- assert flatten([0]) == [0]
- assert flatten([0, [null]]) == [0, null]
- assert flatten(null) == []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement