General and Web Programming Fundamentals
  • Introduction
  • Program creation and design
    • Program design
      • Algorithms
      • Pseudocode
    • Programming conventions
    • Writing programs
      • Source code editors
      • Integrated Development Environments
      • Code repositories/Version control
      • Compilers/Interpreters
  • Programming Fundamentals
    • Operators
      • Arithmetic
      • Logical
      • Assignment
    • Constants and Variables
    • Datatypes
      • Primitive Datatypes
        • Character
        • Integer
        • Boolean
        • Floating point
        • Nothing (Null)
      • Composite Datatypes
        • Arrays
        • Strings
        • Classes
        • Structs
      • Literals
    • Data structures
      • Lists
      • Queues
      • Stacks
      • Map/dictionary
      • Trees
      • Graphs
    • Control structures
      • Selection (Conditional)
        • If/Else
        • Ternary
        • Switch
      • Iteration (Loops)
        • For loops
        • While loops
        • Do-While loops
        • For-Each loops
    • Functions
      • Parameters and arguments
      • Lambda expressions
      • Higher Order Functions
    • Space and Time
    • Scope
    • Standard libraries
  • Programming Paradigms
    • Procedural (Imperative) Programming
    • Object-oriented programming
    • Functional Programming
    • Declarative Programming
    • Event Driven programming
  • Programming Languages
    • Short history of programming
    • Low-level programming languages
    • High-level programming languages
  • Web Development
    • What is the web?
      • Web browsers (clients)
      • Webservers (serving web pages)
      • W3C
    • Markup languages
      • HTML
        • HTML Tags
      • Cascading Style Sheets (CSS)
        • CSS Properties
      • XML
      • Markdown
    • Scripting Languages
      • JavaScript
      • TypeScript
    • JSON
    • JavaScript Frameworks
  • Acknowledgements
    • About the author(s)
  • License
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Programming Fundamentals
  2. Functions

Higher Order Functions

Higher-order functions are functions that can take one or more functions as arguments and/or return a function as their result. These are a key feature of functional programming but are available in many programming languages, including those not purely functional like Python and C#. Higher-order functions are powerful because they allow for functional abstractions and operations, such as map, filter, and reduce, which can operate on other functions.

Higher-Order Functions in Python

In Python, functions are first-class citizens, meaning they can be passed around and used as arguments just like any other object (e.g., string, int). A classic example of a higher-order function in Python is the map function, which takes a function and an iterable (like a list) and applies the function to each item in the iterable, returning a map object (which is an iterator).

# Define a simple function to double a number
def double(x):
    return x * 2

# Use the map function to apply 'double' to each element in the list
numbers = [1, 2, 3, 4, 5]
result = map(double, numbers)

# Convert the result to a list and print it
print(list(result))

This example demonstrates how map takes the double function and a list of numbers and applies double to each number.

Higher-Order Functions in C#

C# supports higher-order functions through delegates and lambda expressions. Delegates are type-safe function pointers that can reference methods with a particular parameter list and return type. Lambda expressions are a concise way to represent anonymous methods using a clear and concise syntax.

An example of a higher-order function in C# is using the List<T>.ForEach method, which performs a specified action on each element of the List<T>.

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Define a list of integers
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

        // Use ForEach to apply an action to each element in the list
        // Here, the action is defined by a lambda expression that prints double the value of each element
        numbers.ForEach(number => Console.WriteLine(number * 2));
    }
}

In this example, ForEach is a higher-order function because it takes a lambda expression (an anonymous function) as an argument. The lambda expression number => Console.WriteLine(number * 2) doubles each number and prints it.

Conclusion

Higher-order functions are a powerful concept that allow for functional programming techniques, such as operations on collections that are independent of the specific elements of those collections. They enable writing more generic, reusable code and can lead to elegant solutions for complex problems.

PreviousLambda expressionsNextSpace and Time

Last updated 1 year ago

Was this helpful?