⚑

Function Composition

Function Composition
어렀움 ν•¨μˆ˜ +40pts

Problem

Write a function that composes multiple functions from right to left.

Examples

Input: compose(x => x * 2, x => x + 1)(5)
Output: 12
πŸ’‘ (5 + 1) * 2 = 12

Explanation

이 λ¬Έμ œλŠ” **ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°**의 핡심 κ°œλ…μΈ **ν•¨μˆ˜ ν•©μ„±(composition)**을 ν•™μŠ΅ν•©λ‹ˆλ‹€. **ν•¨μˆ˜ ν•©μ„±μ΄λž€?** μ—¬λŸ¬ ν•¨μˆ˜λ₯Ό μ—°κ²°ν•˜μ—¬ ν•˜λ‚˜μ˜ μƒˆλ‘œμš΄ ν•¨μˆ˜λ₯Ό λ§Œλ“œλŠ” κ²ƒμž…λ‹ˆλ‹€. μˆ˜ν•™μ˜ f(g(x))와 κ°™μŠ΅λ‹ˆλ‹€. **compose의 λ™μž‘ 방식** 였λ₯Έμͺ½μ—μ„œ μ™Όμͺ½μœΌλ‘œ ν•¨μˆ˜λ₯Ό μ μš©ν•©λ‹ˆλ‹€: compose(f, g, h)(x) = f(g(h(x))) **compose(x => x * 2, x => x + 1)(5) μ‹€ν–‰** 1. 였λ₯Έμͺ½ ν•¨μˆ˜ λ¨Όμ €: 5 + 1 = 6 2. μ™Όμͺ½ ν•¨μˆ˜ 적용: 6 * 2 = 12 **reduc...

View detailed explanation β†’

Key Concepts

ν•¨μˆ˜ ν•©μ„± reduceRight λ©”μ„œλ“œ κ³ μ°¨ ν•¨μˆ˜ ν΄λ‘œμ €
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.