Skip to content

Instantly share code, notes, and snippets.

@stevendborrelli
Created November 2, 2012 21:10
Show Gist options
  • Save stevendborrelli/4004350 to your computer and use it in GitHub Desktop.
Save stevendborrelli/4004350 to your computer and use it in GitHub Desktop.
val a = ('a', 2) //> a : (Char, Int) = (a,2)
val b = ('b', 2) //> b : (Char, Int) = (b,2)
val c = ('c', 3) //> c : (Char, Int) = (c,3)
val d = ('d', 1) //> d : (Char, Int) = (d,1)
val list = List(a, b, c, d) //> list : List[(Char, Int)] = List((a,2), (b,2), (c,3), (d,1))
val sub = List(('a', 1), ('b', 4), ('c', 3)) //> sub : List[(Char, Int)] = List((a,1), (b,4), (c,3))
def combo(sub: Occurrences): List[Occurrences] = {
val acc: List[Occurrences] = List(List())
sub match {
case Nil => acc
case (char, freq) :: Nil =>
acc ::: (for {
f <- 1 to freq
} yield List((char, f))).toList
case occ :: rest => for {
rst <- combo(rest)
f <- 1 to occ._2
} yield (occ._1, f) :: rst
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment