Front-end_dev

함수형 프로그래밍 첫예제 본문

ES6/Functional Programming

함수형 프로그래밍 첫예제

Eat2go 2017. 9. 8. 12:12

function trimStr(str,num) { 

  return str.substr(0,str.length - num)

}


function extractLength(str,ret = 0) {

  if(str.length === 0) return ret;

  

  const s = trimStr(str,1);

  return extractLength(s , ret+1); // Tail call optimization (for only ES6)

}


function createRandomChar() {

  return Math.random()*26 | 0 + 1;

}


function convertNumToStr(num) {

  return num.toString(36)

}


function genRandomStr(size) {

  const arr = R.map(R.compose(convertNumToStr,createRandomChar) , R.range(0,size));

  return arr.join('')

}


const password = genRandomStr(10);

console.log(`str : ${password}

length : ${extractLength(password)}`);



str : j99b1l739j
length : 10




'ES6 > Functional Programming' 카테고리의 다른 글

Lazy Evalulation 구현  (0) 2017.10.31