Hegel: Can we finally have type-safety for curry?

Created on 19 May 2020  路  3Comments  路  Source: JSMonk/hegel

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?

question

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Finesse picture Finesse  路  5Comments

antonkatz picture antonkatz  路  3Comments

Mathspy picture Mathspy  路  5Comments

mohsen1 picture mohsen1  路  5Comments

thecotne picture thecotne  路  5Comments