hegel icon indicating copy to clipboard operation
hegel copied to clipboard

Can we finally have type-safety for curry?

Open millsp opened this issue 4 years ago • 3 comments

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?

millsp avatar May 18 '20 22:05 millsp

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 ^_^.

JSMonk avatar May 19 '20 09:05 JSMonk

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!

millsp avatar May 19 '20 09:05 millsp

Okay, we will try to implement it :3 Thank you for the proposal.

JSMonk avatar May 19 '20 13:05 JSMonk