Scala - Higher-Order Functions


Advertisements

Scala allows the definition of higher-order functions. These are functions that take other functions as parameters, or whose result is a function.

Try the following example program, apply() function takes another function f and a value v and applies function f to v.

Example

object Demo {
   def main(args: Array[String]) {
      println( apply( layout, 10) )
   }

   def apply(f: Int => String, v: Int) = f(v)

   def layout[A](x: A) = "[" + x.toString() + "]"
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

[10]
scala_functions.htm

Useful Video Courses


Video

Scala Online Training

82 Lectures 7 hours

Arnab Chakraborty

Video

Apache Spark with Scala - Hands On with Big Data

23 Lectures 1.5 hours

Mukund Kumar Mishra

Video

Delta Lake with Apache Spark using Scala

52 Lectures 1.5 hours

Bigdata Engineer

Video

Apache Spark with Scala useful for Databricks Certification

76 Lectures 5.5 hours

Bigdata Engineer

Video

Machine Learning with Apache Spark 3.0 using Scala

69 Lectures 7.5 hours

Bigdata Engineer

Advertisements