1

Question

Is there a way to define the insert function inside the makeOrderedLeafList match block?

Problem

Since insert function is only used in makeOrderedLeafList, I would like to define it inside it. However, if placed at the bottom, an error "Unit required". Cannot place at the top as "case" is being expected.

def makeOrderedLeafList(freqs: List[(Char, Int)]): List[Leaf] = freqs match {
    case List() => List()
    case h :: t => insert(h, makeOrderedLeafList(t))
    case _      => throw new IllegalStateException
}

//--------------------------------------------------------------------------------
// Insert Leaf in the sorted list of Leaf.
//--------------------------------------------------------------------------------
def insert(c: (Char, Int), list: List[Leaf]): List[Leaf] = list match {
    case List() => List(new Leaf(c._1, c._2))
    case h :: t => {
        //--------------------------------------------------------------------------------
        // If C:[char, counter] is smaller, prepend it to the List[Leaf].
        // Otherwise, recurse call insert to find a position in the tail of the list.
        //--------------------------------------------------------------------------------
        if (c._2 <= h.weight) new Leaf(c._1, c._2) :: list
        else h :: insert(c, t)
    }
}
mon
  • 18,789
  • 22
  • 112
  • 205

2 Answers2

3

Just place it inside the case before calling it:

scala> "hello" match {
 | case "hel" => 1
 | case "hello" =>
 |   def f(i: Int): Int = {
 |     i * 2
 |   }
 |   f(3) // don't forget to call the function...
 | }
res0: Int = 6

The right-hand side of a case expression can be any code block that returns the expected type. A code block can contain function definitions.

Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
0

If you define you function inside the outer function call, it will create a new instance of inner function on every call of outer function.

val getInnerFuncHash = () => {
  val func = (x: Int) => x + 1
  func.hashCode().toString
}

println(getInnerFuncHash())
println(getInnerFuncHash())
println(getInnerFuncHash())

this will print the hashCodes for the inner functions, and will output something like,

1136974570
1030088901
2115208183

which means every call to outer function will create a new instanc eof inner function.

The same thing happens in case of defs

def getInnerFuncHash(): String = {
  val func = (x: Int) => x + 1
  func.hashCode().toString()
}

println(getInnerFuncHash())
println(getInnerFuncHash())
println(getInnerFuncHash())
sarveshseri
  • 13,738
  • 28
  • 47