Skip to main content

higher order function in javascript a little and neat implementation of higher order function in javascript

working example

https://stackblitz.com/edit/js-in2ztw



//higher order function is like
let arr1=[11,10,7,8,9,51];
let arr2=[5,3,89,7,6,4];
let arr3=[1,2,3,4,5,6];
let arr4=[5,6,7,8,1008,11];
//get min from first array and max from second
const minMax=(...args)=>{
  let fns=args;
  return (...args)=>{
    const result=0;
    for(let i=0;i<fns.length;i++)
    {
      let func=fns[i];
      result+=func(...args[i]);
    }
    return result;
  }
}
const opArray=[arr1,arr2,arr3,arr4];
const mathOp=[Math.min,Math.max,Math.min,Math.max];
console.log(minMax(...mathOp)(...opArray));

//so what you mean by a hoc (higher order function a function thats accept another function as a parameter and return a manipulated function )

//thats whats minMax function doing

//let take a simple example 

//a higher order function which accept a function as a parameter
const fullName=(fn)=>{
  return (...args)=>{
    const lastName="chauhan";
    return fn(args[0])+" "+lastName;
  }
}

//hoc parameter function
const name=(name)=>{
  return name;
}

console.log(fullName(name)("manish"));
console.log(fullName(name)("sachin"));
console.log(fullName(name)("deepak"));

//a real life example marks and percentage based
let student1Marks=[33,56,33,67,54];
let student2Marks=[89,99,78,67,66];
let student3marks=[90,87,43,52,67];
//get student percentage 
const getPercenTage=(fn)=>{
  return (args)=>{
      const sum=fn(args);
      const total=500;
      return Math.round(sum*100/total);
  }
}
const sum=(arr)=>{
   let sum=0;
   for(let i=0;i<arr.length;i++)
   {
      sum+=arr[i];
   }
   return sum;
}

console.log(getPercenTage(sum)(student1Marks))
console.log(getPercenTage(sum)(student2Marks))
console.log(getPercenTage(sum)(student3marks))



Comments

Popular posts from this blog

Better Memory management with PixiJS or How to manage cpu and cpu memory in PixiJS.

PixiJS is my favorite framework when i am looking for a web games specially for mobile or desktop  PixiJS is fast blazing fast and you can get a decent FPS even on older device.   so here is my optimization techniques for PixiJs 1. manage your sprites in a better way use spritesheet to reduce the draw calls create big sprite sheet which contain multiple sprites can be draw in gpu with a single draw call. use TexturePacker  https://www.codeandweb.com/texturepacker  best tool when its comes to spritesheet 2. for floating point calculation round off calculation for example let  speed = 0.75 ; let  position = 100 ; console . log ( Math . round ( speed * position )) 3. don't create very big canvas when u need a big canvas size game just try to create a small canvas and translate it. 4. its very important one managing TextureCache in memory you can get all TextureCache list by using  Object.entries(PIXI.utils.TextureCache); so even you use ap...

adding particles Effect in pixijs using https://pixijs.io/pixi-particles-editor/

adding particle in pixijs is very easy using the below tool more information can be found below https://github.com/pixijs/pixi-particles https://pixijs.io/pixi-particles-editor/ required packages  /// < reference path = "node_modules/pixi-particles/ambient.d.ts" /> import 'pixi-particles' code of particle delcare a     global variable   private emitter ?: Emitter ; const img = PIXI . Texture . from ( "./assets/images/particle.png" ); this . emitter = new Emitter ( this ,[ img ],{ "alpha" : { "start" : 0.62 , "end" : 0.39 }, "scale" : { "start" : 0.1 , "end" : 0.9 , "minimumScaleMultiplier" : 1.25 }, "color" : { "start" : "#ffff8f" , "end" : ...