β‘
Pipe Function
Pipe Functionμ΄λ €μ ν¨μ +40pts
Problem
Write a pipe function that composes functions from left to right.
Examples
Input:
pipe(x => x + 1, x => x * 2)(5)Output:
12π‘ (5 + 1) * 2 = 12
Explanation
μ΄ λ¬Έμ λ **ν¨μ ν©μ±(function composition)**μ νμ΄ν ν¨ν΄μ ꡬννλ λ°©λ²μ νμ΅ν©λλ€. ## ν΅μ¬ κ°λ : νμ΄ν (Pipe) νμ΄νλ μ¬λ¬ ν¨μλ₯Ό μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ μμ°¨μ μΌλ‘ μ μ©ν©λλ€. ### μ κ·Ό λ°©λ² ```javascript function pipe(...fns) { return x => fns.reduce((acc, fn) => fn(acc), x); } ``` ### λμ μ리 ```javascript pipe(f, g, h)(x) = h(g(f(x))) ``` - f(x)μ κ²°κ³Όλ₯Ό g...
View detailed explanation βKey Concepts
ν¨μ ν©μ± pipe reduce ν¨μν νλ‘κ·Έλλ°
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.