Skip to content

Instantly share code, notes, and snippets.

@HichamBenjelloun
Last active June 17, 2024 09:12
Show Gist options
  • Save HichamBenjelloun/be142d1b58461b3c8d7acce8525f6ef0 to your computer and use it in GitHub Desktop.
Save HichamBenjelloun/be142d1b58461b3c8d7acce8525f6ef0 to your computer and use it in GitHub Desktop.
Generic sum function
const sum = (...args) =>
Object.assign(
sum.bind(null, ...args),
{valueOf: () => args.reduce((acc, cur) => acc + cur, 0)}
);
export default sum;
@HichamBenjelloun
Copy link
Author

Usage :

+sum(1); // 1
+sum(1, 2); // 3
+sum(1, 2)(3); // 6
+sum(1)(2)(3); // 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment