DEV Community

Harsh Joshi
Harsh Joshi

Posted on

2 2

Fun Experiment: Bringing lazy execution to JS!

Bringing lazy execution to higher order functions in JS!
Teaser
How to Use

  • Install the package
    npm i lazy-hofs

  • Add to your project
    require("lazy-hofs");

lazySome()

let arr = [1, 2, 3, 4, 5, 6];
let ref = arr.lazySome((it) => {
  return it === 9;
});
arr.push(9);
let result = ref.lazyEvaluate();
console.log(result);
Enter fullscreen mode Exit fullscreen mode

lazyMap()

let arr = [1, 2, 3, 4, 5, 6];
let ref = arr.lazyMap((it) => {
  return it * 9;
});
arr.push(9);
let result = ref.lazyEvaluate();
console.log(result);
Enter fullscreen mode Exit fullscreen mode

lazyFilter()

let arr = [1, 2, 3, 4, 5, 6];
let ref = arr.lazyFilter((it) => {
  return it % 2 === 0;
});
arr.push(9);
let result = ref.lazyEvaluate();
console.log(result);
Enter fullscreen mode Exit fullscreen mode

lazyReduce()

let arr = [1, 2, 3, 4, 5, 6];
let ref = arr.lazyReduce((it, acc) => {
  return (acc += it);
}, 0);
arr.push(9);
let result = ref.lazyEvaluate();
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Join me here: https://npmjs.com/package/lazy-hofs
Contributions welcome!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Maybe you could consider using my Metho library to do the monkey patching of the array prototype in a safe way that will not risk conflicts with other libraries

Collapse
 
josharsh profile image
Harsh Joshi

Thanks Jon, will give it a read

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay