hegel
hegel copied to clipboard
Can we finally have type-safety for curry?
I tried this on the playground but it fails (Analyzation: SpreadElement
)
Curry is very hard to type correctly, this would be the ultimate test for your project:
const curry = (fn) => {
const fn0 = (...args) => {
if (args.length >= fn.length)
return fn(...args)
return (...argsn) => fn0(...args, ...argsn)
}
return fn0
}
console.log(curry<X>((a: X, b: string, c: X) => [a, b, c], [])('1')(2)('3')) // ['1', 2, '3']
Do you think this could be type safe?
Hi @pirix-gh. For now, I don't know how to implement it safely, because we need to have some special collection type which will be converted to applied arguments. But, I don't say that this is impossible. Thank you for the question ^_^.
Hey @JSMonk, great job, anyway!
I think that you could pull this out with generics only:
const curry = <P, R>(fn: (...args: P) => R) => {
const fn0 = <P0>(...args0: P0) => {
if (args0.length >= fn.length)
return fn(...args0)
return <PN>(...argsn: PN) => fn0(...args0, ...argsn)
}
return fn0
}
But that really depends on how you do your flow analysis.
I'd really love, and all my fellow functional programmers, to have this one day!
Okay, we will try to implement it :3 Thank you for the proposal.