Search and Replace Algorithm By Using Functional Programming Design Pattern

Search and Replace Algorithm By Using Functional Programming Design Pattern
0.0 0

#1

I finished this Algorithm for a longtime but now i come back to change a solution by using Functional Programming - Higher Order Function for practice myself to learning React and rxjs . So my question is can i change this solution to the currying function ? And my solution is closure or not ?

const myReplace = str => before => after => {
      const CheckUpperCase = (before,after) => {
       if(before[0] === before[0].toUpperCase()){
        after = after.split("");
        after[0] = after[0].toUpperCase();
        after = after.join("");
        return after;
        }else{
        return after;
        }
      };
        let a  = str.split(' ');
        let left = a.splice(0,a.indexOf(before));
        let right = a.splice(a.indexOf(before)+1,a.length);
        const result = (spliceLeft,spliceRight,checkCharacterCase) => {
        return spliceLeft.concat(checkCharacterCase(before,after),spliceRight).join(" ");
      };
         return result(left,right,CheckUpperCase);
};

myReplace("He is Sleeping on the couch")("Sleeping")("sitting");

#2

Yes, of course.

This doesn’t really make sense. Closures are how JavaScript works, not a thing that you make. This pattern of returning functions myReplace = str => before => after => is storing each parameter for the next function’s closure.