• + 0 comments

    Scala, such that I am new to Scala and functional programming

    def getNumElements(s: Int, element: Int, result: List[Int]): List[Int] = {
        if (s > 0) {
            getNumElements(s - 1, element, result :+ element)
        } else {
            result
        }
    }
    
    def f(num: Int, arr: List[Int], result: List[Int] = List(),
          repeater: (Int, Int, List[Int]) => List[Int] = getNumElements
         ): List[Int] = {
        if (arr.size > 0) {
            f(num, arr.tail, result ::: repeater(num, arr.head, List()))
        } else {
            result
        }
    }