Skip to content

Commit 7053058

Browse files
committed
Add solution #2629
1 parent 9f0c3be commit 7053058

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@
363363
2535|[Difference Between Element Sum and Digit Sum of an Array](./2535-difference-between-element-sum-and-digit-sum-of-an-array.js)|Easy|
364364
2618|[Check if Object Instance of Class](./2618-check-if-object-instance-of-class.js)|Medium|
365365
2619|[Array Prototype Last](./2619-array-prototype-last.js)|Easy|
366+
2629|[Function Composition](./2629-function-composition.js)|Easy|
366367
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
367368
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
368369
3397|[Maximum Number of Distinct Elements After Operations](./3397-maximum-number-of-distinct-elements-after-operations.js)|Medium|
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 2629. Function Composition
3+
* https://leetcode.com/problems/function-composition/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function
7+
* composition of the array of functions.
8+
*
9+
* The function composition of [f(x), g(x), h(x)] is fn(x) = f(g(h(x))).
10+
*
11+
* The function composition of an empty list of functions is the identity function f(x) = x.
12+
*
13+
* You may assume each function in the array accepts one integer as input and returns one integer
14+
* as output.
15+
*/
16+
17+
/**
18+
* @param {Function[]} functions
19+
* @return {Function}
20+
*/
21+
var compose = function(functions) {
22+
return x => functions.reduceRight((result, fn) => fn(result), x);
23+
};

0 commit comments

Comments
 (0)