I am working with curried functions in TypeScript, and I want to figure out a way to automatically detect if a function is curried and, if it is, decurry it into a non-curried function.
Problem:
- A curried function is a function that takes arguments one at a time and returns a new function for each argument.
- I need a method to detect whether a function is curried (i.e., returns a function when called with one argument), and if it is, I want to decurry it into a function that can accept all arguments at once.
For example, given the following curried function:
FROM
const curriedAdd = (a: number) => (b: number) => (c: number) => a + b + c;
TO
const add = (a: number, b: number, c: number) => a + b + c;
I’ve experimented with some basic heuristics to detect currying, such as calling the function with one argument and checking if the result is another function. However, I’m unsure how to automatically decurry a function at runtime. Specifically, I’m struggling with how to handle functions that take multiple arguments or have a more complex curried structure. I've tried seeking help from AI also but unfortunately it keeps responding with total non-sense that doesn't work.
- I need this solution to work for simple curried functions as well as more complex ones (if possible).
- I am aware of currying and decurrying techniques, but I’m specifically looking for a runtime solution that works in TypeScript.