Function composition là tạo hàm mới bằng cách kết hợp nhiều hàm, output của hàm này là input của hàm kia: compose(f, g)(x) = f(g(x)).
Ví dụ: const process = compose(trim, toLowerCase, removeSpaces) tạo pipeline xử lý string. Implement bằng reduce: const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x). pipe là ngược chiều (trái sang phải, dễ đọc hơn). Hữu ích để tạo data transformation pipelines không cần biến trung gian, code theo functional style.
Function composition creates a new function by combining multiple functions where the output of one becomes the input of another: compose(f, g)(x) = f(g(x)).
Example: const process = compose(trim, toLowerCase, removeSpaces) creates a string processing pipeline. Implemented with reduce: const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x). pipe is the reverse direction (left to right, easier to read). Useful for creating data transformation pipelines without intermediate variables in a functional style.