Created
January 22, 2018 21:28
-
-
Save Beraliv/39c69262e635388aea9d241d1d0ae916 to your computer and use it in GitHub Desktop.
Generator of Curried function types for index.js.flow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const g = n => { | |
const array = Array.from(Array(n), (_, i) => i + 1); | |
const indices = Array.from(Array(n + 1), (_, i) => i); | |
const returnF = k => { | |
const types = array.slice(k).map(t => `T${t}`); | |
return types.length === 0 | |
? 'R' | |
: `CurriedFunction${n - k}<${types.join(', ')}, R>`; | |
} | |
const params = k => { | |
const types = array.slice(0, k).map(t => `T${t}`); | |
return types.length === 0 | |
? '()' | |
: `(...params: [${types.join(', ')}])`; | |
} | |
return ` | |
export type ${returnF(0)} = | |
${indices.map(i => { | |
return `(${params(i)} => ${returnF(i)})`; | |
}).join(' &\n ')};` | |
} | |
for (let i = 1; i <= 10; i++) { | |
console.log(g(i)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment