Higher Order Functions
Higher-Order Functions in Python
# 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))Higher-Order Functions in C#
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));
}
}Conclusion
Last updated
Was this helpful?