Skip to content

Instantly share code, notes, and snippets.

@akaDPR
Last active October 24, 2021 17:19
Show Gist options
  • Save akaDPR/950c5715c9b80744204a12488c91b209 to your computer and use it in GitHub Desktop.
Save akaDPR/950c5715c9b80744204a12488c91b209 to your computer and use it in GitHub Desktop.
Challenge 1
R.compose(R.flip(R.prepend)(acc), R.sum,R.map(R.add(1)))([x,...acc])
R.reduce => R.compose(/*[15,0]*/ R.flip(R.prepend)(acc), /*[15]*/ R.sum, /*[14, 1]*/ R.map(R.add(1)))([13, ...0])
R.reduce => R.compose(/*[46,15,0]*/ R.flip(R.prepend)(acc), /*[46]*/ R.sum, /*[29,16,1]*/ R.map(R.add(1)))([28, ...15,0])
/*
[46,15,0]
R.reduce - a higher-order function that will return a single value from an array.
R.compose - right to left function composition – the result of one function will be passed to next
R.add - adds what we pass
R.map - executes the provided function to every element in the array
R.prepend - prepends the value before the passed argument (accumulator)
R.flip - flips the position of the first two elements
--------------------------------------------------------------------------
* Initially we have the accumulator = 0 and the current value = 13
* The current value and the accumulator are passed as an array to the R.compose function - [13, 0]
* The passed array is looped using R.map and every element in the array is incremented to one - [14, 1]
* The output array from the previous function is summed into one value - [15]
* The summed value prepended with the accumulator and flipped - [15,0]
* The above process will be repeated again but with accumulator = [15,0] and current value = 28
*
* The acc and current value are passed as an array to the R.compose function - [28, 15, 0 ]
* The passed array is looped using R.map and every element in the array is incremented to one - [29, 16, 1]
* The output array from the previous function is summed into one value - [46]
* The summed value is prepended with the accumulator and flipped - [46,15,0]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment