Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (25 sloc) 990 Bytes
package scalaBasics.functionalProgramming
object HOFsCurries extends App {
val superFunction: (Int, (String, (Int => Boolean)) => Int) => (Int => Int) = null
def nTimes(f: Int => Int, n: Int, x: Int): Int = {
if (n <= 0) x
else nTimes(f, n-1, f(x))
}
val plusOne = (x: Int) => x + 1
println(nTimes(plusOne, 10, 1))
def nTimesBetter(f: Int => Int, n: Int): (Int => Int) = {
if (n <= 0) (x: Int) => x
else (x: Int) => nTimesBetter(f, n-1)(f(x))
}
val plus10 = nTimesBetter(plusOne, 10)
println(plus10(1))
val superAdder = (x: Int) => (y: Int) => x + y
val add3 = superAdder(3)
println(add3(10))
println(superAdder(3)(10))
def curriedFormatter(c: String)(x: Double): String = c.format(x)
val standardFormat: (Double => String) = curriedFormatter("%4.2f")
val preciseFormat: (Double => String) = curriedFormatter("%10.8f")
println(standardFormat(Math.PI)) // prints 3.14
println(preciseFormat(Math.PI)) // prints 3.14159265
}
You can’t perform that action at this time.