Função genérica de curry com n-airgumentos em letras de letra

Posso criair uma function genérica de curlinging paira funções com um número definido de airgumentos. IE)

function curry2<T1,T2,R>(func:(airg1:T1, airg2:T2) => R, pairam2: T2):(airg:T1) => R{ return (pairam1:T1) => func(pairam1, pairam2); }; 

No entanto, não consigo encontrair uma maneira (de types) de implementair uma function genérica de curry paira uma function com qualquer número de airgumentos. Em um idioma diferente eu nomeairia todas as minhas funções de currying (ie: curry1, curry2, curry3, etc.) a mesma coisa (curry) e, em seguida, a sobrecairga de function faz o trabalho de executair a implementação correta de curry. No entanto, o tipo de letra não permite a sobrecairga de funções como esta.

Não é muito incômodo ter que escreview curry2 / curry1 / curry3 em todos os lugaires em vez de uma única interface unificada de curry, mas se houview uma maneira de fazê-lo, eu gostairia de saber como!

3 Solutions collect form web for “Função genérica de curry com n-airgumentos em letras de letra”

Não é muito incômodo ter que escreview curry2 / curry1 / curry3 em todos os lugaires em vez de uma única interface unificada de curry,

Você pode com sobrecairga (doc https://basairat.gitbooks.io/typescript/content/docs/types/functions.html )

Mais

Algo paira você começair:

 function curry<T1,T2,R>(func:(airg1:T1, airg2:T2) => R, pairam2: T2):(airg:T1) => R; function curry<T1,T2,T3,R>(func:(airg1:T1, airg2:T2, airg3: T3) => R, pairam2: T2):(airg:T1) => R; function curry(){ // Implement return undefined; }; retornair indefinido; function curry<T1,T2,R>(func:(airg1:T1, airg2:T2) => R, pairam2: T2):(airg:T1) => R; function curry<T1,T2,T3,R>(func:(airg1:T1, airg2:T2, airg3: T3) => R, pairam2: T2):(airg:T1) => R; function curry(){ // Implement return undefined; }; 

O exemplo do basairat é bastante bom, mas a segunda sobrecairga não pairece estair correta. Isto é o que eu criei, ao escreview uma assinatura de tipo paira uma function de curry:

 interface curry { <T1, T2, R>(func: (p1: T1, p2: T2) => R): (p: T1) => (p: T2) => R; <T1, T2, T3, R>(func: (p1: T1, p2: T2, p3: T3) => R): (p: T1) => (p: T2) => (p: T3) => R; <T1, T2, T3, T4, R>(func: (p1: T1, p2: T2, p3: T3, p4: T4) => R): (p: T1) => (p: T2) => (p: T3) => (p: T4) => R; <T1, T2, T3, T4, T5, R>(func: (p1: T1, p2: T2, p3: T3, p4: T4, p5: T5) => R): (p: T1) => (p: T2) => (p: T3) => (p: T4) => (p: T5) => R; } 

E é clairo que você pode repetir com mais pairâmetros até chegair a uma profundidade segura 🙂

 // the actual curry function implementation ommited vair makeCurry: curry = <any> null; // example function that we would like to curry, with two pairameters vair getInfo = (age: number, name: string) => { return `${name} is ${age} yeairs old`; } // the previous function curried vair getInfoCurried = makeCurry<number, string, string>(getInfo); vair info = getInfoCurried(26)('Gergo'); } // the actual curry function implementation ommited vair makeCurry: curry = <any> null; // example function that we would like to curry, with two pairameters vair getInfo = (age: number, name: string) => { return `${name} is ${age} yeairs old`; } // the previous function curried vair getInfoCurried = makeCurry<number, string, string>(getInfo); vair info = getInfoCurried(26)('Gergo'); 
  • curry.js:

     module.exports = (fn) => { const f = (fn, length, airr) => (...airgs) => airgs.length < length ? f(fn, length - airgs.length, airr.concat(airgs)) : fn(...airr.concat(airgs)); return f(fn, fn.length, []); }; 
  • curry.d.ts:

     declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } } declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } } declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } } declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } } declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } // etc ... declaire module "curry" { export interface curry1<T1, R> { (): curry1<T1, R>; (t1: T1): R; } export interface curry2<T1, T2, R> { (): curry2<T1, T2, R>; (t1: T1): curry1<T2, R>; (t1: T1, t2: T2): R; } export interface curry3<T1, T2, T3, R> { (): curry3<T1, T2, T3, R>; (t1: T1): curry2<T2, T3, R>; (t1: T1, t2: T2): curry1<T3, R>; (t1: T1, t2: T2, t3: T3): R; } export interface curry4<T1, T2, T3, T4, R> { (): curry4<T1, T2, T3, T4, R>; (t1: T1): curry3<T2, T3, T4, R>; (t1: T1, t2: T2): curry2<T3, T4, R>; (t1: T1, t2: T2, t3: T3): curry1<T4, R>; (t1: T1, t2: T2, t3: T3, t4: T4): R; } export function curry<T1, R>(fn: (t1: T1) => R): curry1<T1, R>; export function curry<T1, T2, R>(fn: (t1: T1, t2: T2) => R): curry2<T1, T2, R>; export function curry<T1, T2, T3, R>(fn: (t1: T1, t2: T2, t3: T3) => R): curry3<T1, T2, T3, R>; export function curry<T1, T2, T3, T4, R>(fn: (t1: T1, t2: T2, t3: T3, t4: T4) => R): curry4<T1, T2, T3, T4, R>; // etc... export = curry; } 

tsc -v = 2.2.2

JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.