Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = ( fn, n) => ( ... args) => fn ( ... args. slice ( 0 , n));
+ } 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less. Search for snippet...
Adapter ary call collectInto flip over pipeFunctions promisify spreadOver unary Array chunk compact countBy countOccurrences deepFlatten difference differenceBy differenceWith dropElements dropRight everyNth filterNonUnique findLast findLastIndex flatten forEachRight groupBy head indexOfAll initial initialize2DArray initializeArrayWithRange initializeArrayWithRangeRight initializeArrayWithValues intersection intersectionBy intersectionWith isSorted join last longestItem mapObject maxN minN nthElement partition pull pullAtIndex pullAtValue reducedFilter reduceSuccessive remove sample sampleSize shuffle similarity sortedIndex sortedLastIndex symmetricDifference symmetricDifferenceBy symmetricDifferenceWith tail take takeRight union unionBy unionWith uniqueElements unzip unzipWith without xProd zip zipObject zipWith Browser arrayToHtmlList bottomVisible copyToClipboard createElement createEventHub currentURL detectDeviceType elementIsVisibleInViewport getScrollPosition getStyle hasClass hashBrowser hide httpsRedirect observeMutations off on onUserInputChange redirect runAsync scrollToTop setStyle show toggleClass UUIDGeneratorBrowser Date formatDuration getDaysDiffBetweenDates tomorrow Function bind bindKey chainAsync compose composeRight curry defer delay functionName memoize negate once partial partialRight runPromisesInSeries sleep times unfold Math average averageBy clampNumber digitize distance elo factorial fibonacci gcd geometricProgression hammingDistance inRange isDivisible isEven isPrime lcm luhnCheck maxBy median minBy percentile powerset primes randomIntArrayInRange randomIntegerInRange randomNumberInRange round sdbm standardDeviation sum sumBy sumPower toSafeInteger Node atob btoa colorize hasFlags hashNode isTravisCI JSONToFile readFileLines untildify UUIDGeneratorNode Object deepClone defaults equals findKey findLastKey forOwn forOwnRight functions get invertKeyValues lowercaseKeys mapKeys mapValues matches matchesWith merge objectFromPairs objectToPairs omit omitBy orderBy pick pickBy shallowClone size transform truthCheckCollection String anagrams byteSize capitalize capitalizeEveryWord decapitalize escapeHTML escapeRegExp fromCamelCase isAbsoluteURL isLowerCase isUpperCase mask palindrome pluralize reverseString sortCharactersInString splitLines toCamelCase toKebabCase toSnakeCase truncateString unescapeHTML URLJoin words Type getType is isArrayLike isBoolean isEmpty isFunction isNil isNull isNumber isObject isObjectLike isPlainObject isPrimitive isPromiseLike isString isSymbol isUndefined isValidJSON Utility castArray cloneRegExp coalesce coalesceFactory extendHex getURLParameters hexToRGB httpGet httpPost nthArg parseCookie prettyBytes randomHexColorCode RGBToHex serializeCookie timeTaken toDecimalMark toOrdinalSuffix validateNumber yesNo Adapter ary Creates a function that accepts up to n arguments, ignoring any additional arguments.
Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).
const ary = ( fn, n) => ( ... args) => fn ( ... args. slice ( 0 , n));
Show examples const firstTwoMax = ary ( Math. max, 2 );
[[ 2 , 6 , 'a' ], [ 8 , 4 , 6 ], [ 10 ]]. map ( x => firstTwoMax ( ... x));
📋 Copy to clipboard call Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = ( key, ... args) => context => context[ key]( ... args);
@@ -308,6 +308,9 @@ Object. assig
];
reducedFilter ( data, [ 'id' , 'name' ], item => item. age > 24 );
+📋 Copy to clipboard reduceSuccessive Applies a function against an accumulator and each element in the array (from left to right), returning an array of successively reduced values.
Use Array.reduce() to apply the given function to the given array, storing each new result.
const reduceSuccessive = ( arr, fn, acc) =>
+ arr. reduce (( res, val, i, arr) => ( res. push ( fn ( res. slice ( - 1 )[ 0 ], val, i, arr)), res), [ acc]);
+Show examples reduceSuccessive ([ 1 , 2 , 3 , 4 , 5 , 6 ], ( acc, val) => acc + val, 0 );
📋 Copy to clipboard remove Removes elements from an array for which the given function returns false.
Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). The func is invoked with three arguments (value, index, array).
const remove = ( arr, func) =>
Array. isArray ( arr)
? arr. filter ( func). reduce (( acc, val) => {
diff --git a/snippets/reduceSuccessive.md b/snippets/reduceSuccessive.md
index ce7879696..dc4b154ed 100644
--- a/snippets/reduceSuccessive.md
+++ b/snippets/reduceSuccessive.md
@@ -6,9 +6,7 @@ Use `Array.reduce()` to apply the given function to the given array, storing eac
```js
const reduceSuccessive = (arr, fn, acc) =>
- arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)),res), [
- acc,
- ]);
+ arr.reduce((res, val, i, arr) => (res.push(fn(res.slice(-1)[0], val, i, arr)), res), [acc]);
```
```js