Scala inplementation of arowana algorithm