Curries a function.

Use recursion.
If the number of provided arguments (args) is sufficient, call the passed function fn.
Otherwise, return a curried function fn that expects the rest of the arguments.
If you want to curry a function that accepts a variable number of arguments (a variadic function, e.g. Math.min()), you can optionally pass the number of arguments to the second parameter arity.

继续阅读 30秒学会 JavaScript 片段 – curry

Returns the target value in a nested JSON object, based on the given key.

Use the in operator to check if target exists in obj.
If found, return the value of obj[target], otherwise use Object.values(obj) and Array.prototype.reduce() to recursively call dig on each nested object until the first matching key/value pair is found.

继续阅读 30秒学会 JavaScript 片段 – dig

Returns the least common multiple of two or more numbers.

Define a gcd() function that determines the greatest common divisor, using recursion.
Use gcd() and the fact that LCM(x, y) = x * y / GCD(x,y) to determine the least common multiple.
Use range to apply the calculation to all given numbers.

继续阅读 30秒学会 Golang 片段 – LCM

Generates all permutations of a string (contains duplicates).

⚠️ WARNING: This function’s execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.

Use recursion.
For each letter in the given string, create all the partial permutations for the rest of its letters.
Use to combine the letter with each partial permutation, then Array.prototype.reduce() to combine all permutations in one array.
Base cases are for string length equal to 2 or 1.

继续阅读 30秒学会 JavaScript 片段 – stringPermutations

Deep freezes an object.

Use Object.keys() to get all the properties of the passed object, Array.prototype.forEach() to iterate over them.
Call Object.freeze(obj) recursively on all properties, checking if each one is frozen using Object.isFrozen() and applying deepFreeze() as necessary.
Finally, use Object.freeze() to freeze the given object.

继续阅读 30秒学会 JavaScript 片段 – deepFreeze

Calculates the greatest common divisor between two or more numbers.

Define a gcd() function for two numbers, which uses recursion.
Base case is when y equals 0, which returns x.
Otherwise the GCD of y and the remainder of the division x/y is returned.
Use gcd() and range to apply the calculation to all given numbers.

继续阅读 30秒学会 Golang 片段 – GCD