Skip to content
loading...

re: Build a PowerSet VIEW POST

TOP OF THREAD FULL DISCUSSION
re: depends on what you want to log I guess (seems doubtful, that you want to log anything here - indeed I never had the need to log anything inside a ...

cool, adding the scala functional :: recursive :: declarative :: concise way to the post :)) .

  def powerSet(set: Set[Int]): Set[Set[Int]] = {

    set.toList match {
      case Nil => Set(Set.empty[Int])
      case x :: xs => powerSet(xs.toSet).map(xsi => xsi + x) ++ powerSet(xs.toSet)
    }
  }

  println(powerSet(Set(1,2,3))) // Set(Set(), Set(3, 1), Set(2), Set(2, 1), Set(3, 2), Set(3), Set(3, 2, 1), Set(1))
code of conduct - report abuse