Recursive set union: how does it work really?

前端 未结 6 2049
梦谈多话
梦谈多话 2020-12-13 14:29

I am currently taking the Scala course on Coursera on my free time after work, in an attempt to finally give a try to functional programming. I am currently working on an as

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-13 15:04

    I'm doing the same course, and the above implementation of union did turn out to be extremely inefficient.

    I came up with the following not-so-functional solution to creating a union of binary-tree sets, which is WAY more efficient:

    def union(that: BTSet): BTSet = {
      var result:BTSet = this
      that.foreach(element => result = result.incl(element))
      result
    }
    

提交回复
热议问题