Skip to content

Instantly share code, notes, and snippets.

@olegchir
Created January 31, 2017 15:47
Show Gist options
  • Save olegchir/eb31290cf92cbc89b80a0f0ba2cf1b93 to your computer and use it in GitHub Desktop.
Save olegchir/eb31290cf92cbc89b80a0f0ba2cf1b93 to your computer and use it in GitHub Desktop.
public static Object sum(Object args) {
//Вначале есть нода, в которой ничего нет
Node result = new Node();
result.childList = new ArrayList<>();
result.numberList = new ArrayList<>();
//В ходе развертки всегда остается пара
while (args instanceof Pair) {
//Берем голову
Object firstArg = PairUtils.first(args);
//И проверяем - является ли
//голова переходом на другой уровень вложенности,
//или значением-листом
if (firstArg instanceof Integer) {
//Если лист - добавляем в список номеров этого уровня вложенности
result.numberList.add((Integer) firstArg);
} else if (firstArg instanceof Node) {
//Если переход на более глубокий уровень - добавляем в список детей
result.childList.add((Node) firstArg);
}
//Отщипываем голову и отправляем дальше крутиться в while
args = PairUtils.rest(args);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment