Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Given a mongodb collection that stores trees as nodes with parent references this methods returns a deep nested tree where every child nodes are stored in a property childs[Seq]

  def getTree(rootId: BSONObjectID): Future[CategoryTreeNode] = {

    def collect(parent: CategoryTreeNode): Future[CategoryTreeNode] = {

      // Query the database - returns a Future[Seq[CategoryTreeNode]]
      getChilds(parent._id.get).map {

        // Seq[CategoryTreeNode]
        childs =>
          Future.sequence(
            childs.map(child => collect(child)) // Recursion
          ).map(childSeq => parent.copy(childs = Some(childSeq)))

      }.flatMap(x => x).map(y => y) 

    }

    // Find the root node and start the recursion
    findOne[CategoryTreeNode](Json.obj("_id" -> rootId)).map(maybeNode => maybeNode match {
      case None => throw new RuntimeException("Current node not found by id!")
      case Some(node) => collect(node)
    }).flatMap(x => x)

  }
share|improve this question
    
What are your concerns about the code? Or do you simply want general comments about your implementation? –  MrSmith42 Mar 7 '14 at 11:50
    
Basically I want to know if it can be done better/more idiomatic - especially the map.flatmap cascades look odd to me. –  Daniel Khan Mar 7 '14 at 11:54

1 Answer 1

up vote 1 down vote accepted
  • flatMap (x => x) would probably better be written as flatten.

  • map (x => x) is essentially a no-op, and could be removed.

  • map ( ... ) flatten is equivalent to flatMap (...)

  • It doesn't seem useful to put a type into a comment – you can specify the type for parameters of lambdas as well:

    { (childs: Seq[CategoryTreeNode]) => ...
    

    However, this style is generally discouraged. It would be more useful to store the future from the DB in a variable which gets typed explicitly.

  • Higher-order method like map should not be invoked with a leading .: do collection map (x => ...) instead of collection.map(x => ...) (source).

  • (foo => foo match { ... }) can be simplified to (_ match { ... })

  • (child => collect(child)) can be simplified to (collect(_)) or even just collect

Together, I'd clean up your code to this:

def getTree(rootId: BSONObjectID): Future[CategoryTreeNode] = {
  def collect(parent: CategoryTreeNode): Future[CategoryTreeNode] = {
    // Query the database
    val ourChilds: Future[Seq[CategoryTreeNode]] = getChilds(parent._id.get)
    ourChilds flatMap { childs =>
      Future.sequence(childs map collect) map { childSeq =>
        parent.copy(childs = Some(childSeq))
      }
    }
  }

  // Find the root node and start the recursion
  findOne[CategoryTreeNode](Json.obj("_id" -> rootId)) flatMap (_ match {
    case None => throw new RuntimeException("Current node not found by id!")
    case Some(node) => collect(node)
  })
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.