Last active
September 27, 2018 15:36
-
-
Save johnwcothran/4032e4192179ea0898ca2005caed9e00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import R from 'ramda'; | |
const sma = (num, data, arr = []) => { | |
if (R.length(data) === 0) { | |
console.log(`arr: [${arr}] data: [${data}]`); | |
return arr; | |
} else if (R.length(data) < num) { | |
console.log(`arr: [${arr}] data: [${data}]`); | |
return sma(num, R.tail(data), R.prepend('not yet!', arr)); | |
} | |
const newData = R.tail(data); | |
const movingMean = R.mean( | |
R.slice(0, num, data) | |
); | |
console.log(`arr: [${arr}] data: [${data}]`); | |
return sma(num, newData, R.append(movingMean, arr)); | |
}; | |
console.log(sma(3, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); | |
// arr= [] data= [1,2,3,4,5,6,7,8,9,10] | |
// arr= [2] data= [2,3,4,5,6,7,8,9,10] | |
// arr= [2,3] data= [3,4,5,6,7,8,9,10] | |
// arr= [2,3,4] data= [4,5,6,7,8,9,10] | |
// arr= [2,3,4,5] data= [5,6,7,8,9,10] | |
// arr= [2,3,4,5,6] data= [6,7,8,9,10] | |
// arr= [2,3,4,5,6,7] data= [7,8,9,10] | |
// arr= [2,3,4,5,6,7,8] data= [8,9,10] | |
// arr= [2,3,4,5,6,7,8,9] data= [9,10] | |
// arr= [not yet!,2,3,4,5,6,7,8,9] data= [10] | |
// arr= [not yet!,not yet!,2,3,4,5,6,7,8,9] data= [] | |
// [ 'not yet!', 'not yet!', 2, 3, 4, 5, 6, 7, 8, 9 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment