Question presented to candidate: "What is a higher-order function, and can you show me one that both takes and returns a function?"
What a strong answer should cover:
- A higher-order function is any function that does at least one of two things: accepts a function as an argument, or returns a function as its result. Either one qualifies -- it does not need to do both.
- Built-in array methods like map, filter, reduce, sort, and forEach are all higher-order functions -- they take a callback and use it to process the array.
- A function that returns a function -- a factory, a decorator, or a composition helper like compose/pipe -- is a higher-order function in the other direction.
- Higher-order functions are what enables function composition: combining small, single-purpose functions into a larger pipeline without writing custom glue code for every combination.
- Being a higher-order function is about a function's SHAPE (what it accepts or returns), not about what the function does internally -- it is a structural classification, not a specific algorithm.
Code / implementation expected: Yes -- a runnable snippet showing HOFs that take a function (map/filter/reduce) and one that returns a function (a compose helper), with real chained output.