1
\$\begingroup\$

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)

  }
\$\endgroup\$
  • \$\begingroup\$ What are your concerns about the code? Or do you simply want general comments about your implementation? \$\endgroup\$ – MrSmith42 Mar 7 '14 at 11:50
  • \$\begingroup\$ Basically I want to know if it can be done better/more idiomatic - especially the map.flatmap cascades look odd to me. \$\endgroup\$ – DanielKhan Mar 7 '14 at 11:54
1
\$\begingroup\$
  • 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)
  })
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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