Computer Science

Higher Order Functions

Higher order functions are functions that take other functions as arguments or return functions as their results. They are a fundamental concept in functional programming and allow for more concise and modular code. Higher order functions can be used to implement powerful abstractions such as map, filter, and reduce.

Written by Perlego with AI-assistance

3 Key excerpts on "Higher Order Functions"

  • Scala and Spark for Big Data Analytics
    • Md. Rezaul Karim, Sridhar Alla(Authors)
    • 2017(Publication Date)
    • Packt Publishing
      (Publisher)
    The combination of OOP and FP with Spark can make a pretty hard problem easier in Barclays. For example, in Barclays, recently an application called Insights Engine has been developed to execute an arbitrary number N of near-arbitrary SQL-like queries. The application can execute them in a way that can scale with increasing N.
    Now let's talk about pure functions, Higher Order Functions, and anonymous functions, which are the three important concepts in the functional programming of Scala. Passage contains an image

    Pure functions and higher-order functions

    From the computer science perspective, functions can have many forms such as first order functions, higher-order functions, or pure functions. This is also true from the mathematics point of view. Using a higher-order function is a function one of the following can be performed:
    • Takes one or more functions as arguments to do some operations
    • Returns a function as its result
    All other functions except the higher-order functions are first-order functions. However, from the mathematics point of view, higher-order functions are also called operators or functionals . On the other hand, if the return value of a function is only determined by its input and of course without observable side effects, it is called a pure function .
    In this section, we will briefly discuss why and how to use different functional paradigms in Scala. Especially, pure functions, and higher-order functions will be discussed. At the end of this section, a brief overview of using anonymous functions will also be provided since this is used frequently while developing a Spark application using Scala.
    Passage contains an image

    Pure functions

    One of the most important principles of functional programming is pure functions. So what are pure functions and why do we care about them? In this section, we will address this important feature of functional programming. One of the best practices of functional programming is to implement your programs such that the core of your program/application is made from pure functions and all the I/O functions or side effects such as network overhead and exceptions are in an exposed external layer.
  • Learn Kotlin Programming
    eBook - ePub

    Learn Kotlin Programming

    A comprehensive guide to OOP, functions, concurrency, and coroutines in Kotlin 1.3, 2nd Edition

    • Stephen Samuel, Stefan Bocutiu(Authors)
    • 2019(Publication Date)
    • Packt Publishing
      (Publisher)

    Higher-Order Functions and Functional Programming

    In Chapter 4 , Functions in Kotlin , we introduced Kotlin's support for functions and the various features we can use while writing functions. In this chapter, we continue on that theme by discussing higher-order functions and how we can use them to write cleaner and more expressive code.
    In this chapter, we will cover the following topics:
    • Higher-order functions and closures
    • Anonymous functions
    • Function references
    • Functional programming idioms
    • Custom
      domain-specific language (
      DSLs )
    Passage contains an image

    Higher-order functions

    A higher-order function is a function that either accepts another function as a parameter, returns a function as its return value, or both. Let's consider the first example: fun foo(str: String, fn: (String) -> String): Unit { val applied = fn(str) println(applied) }
    Here, we have defined a foo function with two parameters. The first is a string, and the second is a function from string to string. When we say from string to string, we mean the function accepts a string input and returns another string as the output. Also, note the syntax used to define the function parameter. The input types are wrapped in parentheses, and the output type is separated by a thin arrow.
    To invoke this function, we can pass in a function literal (function literals were introduced in Chapter 4 , Functions in Kotlin ):
    foo("hello", { it.reversed() })
    As you can see, the string we pass in is hello. This value is then passed as the input to the next function, which returns the reversed value. This is then printed out, so the result of this invocation would be to output olleh. Remember that a function literal that only has one argument can use it as a shortcut to avoid naming the argument explicitly.
    At this stage, you may be wondering why this is useful. After all, we could have written code like the following:
  • Functional Programming in Go
    eBook - ePub

    Functional Programming in Go

    Apply functional techniques in Golang to improve the testability, readability, and security of your code

    3

    Higher-Order Functions

    In this chapter, we are going to explore the concept of function composition through higher-order functions. There are a variety of new concepts that we are introducing here, such as closures, partial application, and function currying. We will take a look at some practical examples and real-world use cases for these.
    First, we will cover the core concepts of composing functions from an abstract viewpoint, and then we will combine the concepts in a practical example. Everything that we will learn here leans heavily on the concepts introduced in Chapter 2 , where we learned what it means to treat functions as first-class citizens.
    In this chapter, we will cover the following:
    • An introduction to higher-order functions
    • Closures and variable scoping
    • Partial application
    • Function currying, or how to reduce n-ary functions to unary functions
    • Examples:

    Technical requirements

    All the examples for this chapter can be found at https://github.com/PacktPublishing/Functional-Programming-in-Go./tree/main/Chapter3 . For this example, any Go version will work.

    An introduction to higher-order functions

    In essence, a higher-order function is any function that either takes a function as the input or returns a function as the output. Recall from the previous chapter that both of these things are made possible through the support for functions as “first-class citizens.” Although it’s perhaps uncommon to call them “higher-order functions,” many programming languages do support these functions out of the box. For example, in Java and Python, the map , filter , and reduce functions are all examples of higher-order functions.
    Let’s create a simple example in Go. We’ll have a function, A , that returns hello, and a function, B , that takes A as an input parameter. This is a higher-order function, as the A function is used as input to B :
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.