Scala - Higher Order Functions Code Example

The code for Higher Order Functions

object MyClass {
    def main(args: Array[String]) {
        // list of inetgers
        val list = List(1, 3, 5)
        
        // Method for some operation
        def square_values = (num: Int) => num * num
        
        // Higher order function 
        val result = list.map(num => square_values(num))
        
        // Printing the result
        println("Square of numbers of the list is: " + result)
    }
}

/*
Output:
Square of numbers of the list is: List(1, 9, 25)
*/
Code by IncludeHelp, on August 15, 2022 22:45

Comments and Discussions!





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.