Topic : « ecmascript fp »

Avatar de SirSeigneur SirSeigneur
je suis trop génial https://image.noelshack.com/minis/2017/04/1485268387-hackeur-v3.png

mwahahaaa :bwahah:

let curryed = (func) => {
const currying = (acc, ...args) => {
if (acc.length + args.length >= func.length)
return func(...acc, ...args);
else
return context(...acc, ...args);
}

const context = (...acc) => {
return (...args) => {
if (args.length >= 1 && acc.length + args.length <= func.length)
return currying(acc, ...args);
else
return undefined;
}
}

return context();
}

let add = curryed((a, b) => a + b);
let add5 = add(5);

console.log(add5(5) == 10, add5(5) == add(5, 5));
console.log(add5(3) == 8, add5(3) == add(5, 3));

function sayHello(firstname, lastname) {
console.log(`Hello ${firstname} ${lastname}`);
}

let sayHelloJesus = sayHello("Jesus"); // woops, Hello Jesus undefined

let sayHelloCurryed = curryed(sayHello);

let sayHelloChancla = sayHelloCurryed("Chancla"); // not enough argument to be called :P

sayHelloChancla("Goudja"); // Hello Chancla Goudja
sayHelloChancla("Banardor"); // Hello Chancla Banardor

FP https://image.noelshack.com/minis/2016/30/1469402337-pic30.png
Liste des sujets