Created
January 31, 2017 15:47
-
-
Save olegchir/eb31290cf92cbc89b80a0f0ba2cf1b93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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