Skip to content

Instantly share code, notes, and snippets.

@erdaldalkiran
Created August 27, 2016 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erdaldalkiran/d7afb6f8d133f9cbbd2ace3787760514 to your computer and use it in GitHub Desktop.
Save erdaldalkiran/d7afb6f8d133f9cbbd2ace3787760514 to your computer and use it in GitHub Desktop.
Bahmutov - Essence of functional programming
//----------------first
// function timedPrint(character, N, interval) {
// var counter = 0;
// var intervalId = setInterval(function () {
// console.log(character);
// counter += 1;
// if (counter >= N) {
// clearInterval(intervalId);
// console.log('done printing', character, N, 'times');
// }
// }, interval);
// }
// timedPrint('a', 3, 1000);
//----------------second
// const printA = console.log.bind(console, 'a');
// const timedPrint = (workload, N, interval) => {
// let counter = 0;
// const intervalId = setInterval(function(){
// workload();
// counter++;
// if(counter === N){
// console.log('printing is done');
// clearInterval(intervalId);
// }
// }, interval);
// }
// timedPrint(printA, 3, 1000);
//----------------third
const printCharacter = (c) => console.log(c);
const countToN = (n) => {
let start = 0;
return () =>{
start++;
return start === n;
};
};
const timedPrint = (workload, isDone, interval) => {
const intervalId = setInterval(function(){
workload();
if(isDone()){
console.log('printing is done');
clearInterval(intervalId);
}
}, interval);
}
timedPrint(printCharacter('a'), countToN(3), 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment