Skip to content
BENIBIX Learn

Higher-Order Function

Development🧭 Programming ParadigmsFunctional Programming

Also known as Higher Order Function, HOF

A function that takes another function as input, or gives one back. map is one.

Think of it like

Think of it like a boss who hands a task to a helper. The boss says what; the helper does it.

Example

Emma passes str.upper to map with a list of two letters. Without writing a loop, she gets both letters in capitals.

Why it matters

They remove a lot of copy-pasted loops. One line says what to do to every item.

Definition

A higher-order function takes another function as input, or gives one back. map is the best-known one: you hand it a function and a list, and it runs that function on every item. sorted with a key, and filter, work the same way. They let you reuse the looping part and swap only the small action. Most modern languages have them built in.

Example code

python
>>> list(map(str.upper, ["a", "b"]))
['A', 'B']
map takes a function and runs it on each item.

Common mistake

Calling the function while you pass it, like map(str.upper(), items). Pass just the name.

Key takeaways

  1. 1
    Takes a function. Or gives one back.
  2. 2
    Reuse the loop. Swap only the action.
  3. 3
    map, filter, sort. The common ones.

Compare