Last active
August 29, 2015 14:08
-
-
Save bka9/77e35d786b93ba4d6b30 to your computer and use it in GitHub Desktop.
Using futures to count children with name
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
case class Node(name: String, children: List[Node]) | |
def count(node: Node, name: String) = { | |
if(node.children == null && node.name == name) future{1} | |
else if(node.children == null) future{0} | |
else if(node.name == name{ | |
future{ | |
1 + node.children.foldLeft(0)((total,child) => total + count(child,name)) | |
} | |
} | |
else{ | |
future{ | |
0 + node.children.foldLeft(0)((total,child) => total + count(child,name)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment