DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Scala: HOF (Higher-Order Function)

Scala: HOF (Higher-Order Function)

Check out this quick read on high-order function in Scala, with examples.

Balamanikandan T user avatar by
Balamanikandan T
·
Jun. 11, 16 · Java Zone · Opinion
Like (10)
Save
Tweet
13.34K Views

Join the DZone community and get the full member experience.

Join For Free

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

A higher-order function, as opposed to a first-order function, can have one of three forms:

  • One or more of its parameters is a function, and it returns some value.
  • It returns a function, but none of its parameters is a function.
  • Both of the above: One or more of its parameters is a function, and it returns a function.

If you have followed this series, you have seen a lot of usages of higher-order functions of the first type: We called methods like map, filter, or flatMap and passed a function to it that was used to transform or filter a collection in some way. Very often, the functions we passed to these methods were anonymous functions.

Example: [map]

object HOF {

    def main(args: Array[String]) {
      val list = List(
        ("Bala", "S"),
        ("Balamanikandan", "T"),
        ("Karthik", "P"),
        ("Karthik", "S"),
        ("Maharajothi", "T"),
        ("Meenatchi", "T"))

      // Higher order function
      val nameList = list.map(n => getName(firstName = n._1, lastName = n._2))

      println("Result: " + nameList)

    }

  def getName(firstName: String, lastName: String): String = firstName + "." + lastName

}


Here the map function takes a getName function as a parameter. This is called HOF (Higher order function).

Output:

Image title


Scala (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Dynamic Rendering Works Using HTML and CSS?
  • A Simple Guide to Heaps, Stacks, References, and Values in JavaScript
  • Ultra-Fast Microservices in Java: When Microstream Meets Open Liberty
  • Package and Deploy a Lambda Function as a Docker Container With AWS CDK

Comments

Java Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo