

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Functions As Values
incomplete
2: Anonymous Functions
incomplete
3: First-Class and Higher-Order Functions
incomplete
4: Map
incomplete
5: Filter
incomplete
6: Reduce
incomplete
7: Map, Filter, and Reduce Review
incomplete
8: Zip
incomplete
9: Higher-Order Functions Practice
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
A programming language "supports first-class functions" when functions are treated like any other variable. That means functions can be passed as arguments to other functions, can be returned by other functions, and can be assigned to variables.
Python does support first-class and higher-order functions.
from collections.abc import Callable
def square(x: int) -> int:
return x * x
# Assign function to a variable
f: Callable[[int], int] = square
print(f(5))
# 25
def square(x: int) -> int:
return x * x
def my_map(func: Callable[[int], int], arg_list: list[int]) -> list[int]:
result: list[int] = []
for i in arg_list:
result.append(func(i))
return result
squares: list[int] = my_map(square, [1, 2, 3, 4, 5])
print(squares)
# [1, 4, 9, 16, 25]