[{"data":1,"prerenderedAt":68},["ShallowReactive",2],{"course-en-partial-application":3},{"lesson":4,"number":59,"prev":60,"next":64},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":31,"exercises":40},"partial-application",{"ru":7,"en":8},"","Partial application",{"ru":7,"en":10},"How partial application in JavaScript fixes some arguments in advance, how it differs from currying, and how to write your own partial function.",{"ru":7,"en":12},"Understand how partial application differs from currying, and learn to fix some of a function's arguments in advance to derive new functions from it.",[14,17,19,27],{"type":15,"ru":7,"en":16},"text","Partial application is one of the techniques used in functional programming, in which some of a function's arguments are fixed in advance, producing a new function that waits for the remaining arguments.",{"type":15,"ru":7,"en":18},"Partial application is very similar to currying, but there are differences.",{"type":20,"ordered":21,"items":22},"list",true,[23,25],{"ru":7,"en":24},"Partial application fixes some of the arguments in one step: the result is a function that takes the remaining arguments in a single call and immediately computes the result. A curried function returns a new function after each call until it has received all of its arguments.",{"ru":7,"en":26},"The number of arguments passed to the function in partial application can be several, whereas in classic currying it is usually one.",{"type":28,"code":29},"code",{"ru":7,"en":30},"\u002F\u002F  example of functions with partial application\n\u002F\u002F partialSumA :: Number -> (Number, Number) -> Number\nconst partialSumA = (a) => (b, c) => a + b + c\n\u002F\u002F partialSumB :: (Number, Number) -> Number -> Number\nconst partialSumB = (a, b) => (c) => a + b + c\n\n\u002F\u002F now let's call these functions\nconsole.log ( partialSumA (1) (1, 1) ) \u002F\u002F 3\nconsole.log ( partialSumB (1, 1) (1) ) \u002F\u002F 3\n\n\u002F\u002F Now let's write a function that applies\n\u002F\u002F partial application to any regular function\n\n\u002F\u002F partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)\nconst partial = (fn) => {\n\n  \u002F\u002F number of parameters of the original function\n  const lengthArgs = fn.length\n\n  \u002F\u002F the returned function\n  const innerPartial = (...firstArgs) => {\n    \u002F\u002F if the number of incoming arguments\n    \u002F\u002F is greater than or equal to the number of fn's parameters,\n    \u002F\u002F we can call it right away\n    if (firstArgs.length >= lengthArgs) {\n      return fn (...firstArgs)\n    }\n\n    \u002F\u002F otherwise the second call\n    \u002F\u002F runs the function no matter what\n    return (...secondArgs) => fn (...firstArgs, ...secondArgs)\n  }\n\n  return innerPartial\n}\n\n\u002F\u002F we can use our function\n\u002F\u002F that applies partial application\n\u002F\u002F to any function\n\n\u002F\u002F sum :: (Number, Number, Number) -> Number\nconst sum = (a, b, c) => a + b + c\n\n\u002F\u002F we get a function whose arguments can be passed in two calls\nconst sumPartial = partial (sum)\n\n\u002F\u002F now we can call the returned function\nconsole.log ( sumPartial (1) (1, 1) ) \u002F\u002F 3\nconsole.log ( sumPartial (1, 1) (1) ) \u002F\u002F 3",[32,34,36,38],{"ru":7,"en":33},"Partial application fixes some of a function's arguments in advance, producing a new function that waits for the remaining arguments.",{"ru":7,"en":35},"The function produced by partial application takes the remaining arguments in a single call and immediately computes the result, whereas a curried function returns a new function after each call until it has received all of its arguments.",{"ru":7,"en":37},"In partial application several arguments can be passed to the function at once, whereas in classic currying it is usually one.",{"ru":7,"en":39},"The partial function from the lesson applies partial application to any regular function: if there are enough arguments, it calls the function right away; otherwise the second call runs it no matter what.",[41,50],{"task":42,"starter":44,"solution":46,"expected":48},{"ru":7,"en":43},"Use partial to fix two arguments of formatMoney(currency, digits, amount) at once — the currency and the number of decimal places — and get toUsd and toBtc. Then get toEur by fixing only the currency: pass it the number of decimal places and the amount in a single call. Print the amounts from the starter.",{"ru":7,"en":45},"\u002F\u002F partial from the lesson, written more compactly\n\u002F\u002F partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)\nconst partial = (fn) => (...firstArgs) =>\n  firstArgs.length >= fn.length\n    ? fn(...firstArgs)\n    : (...secondArgs) => fn(...firstArgs, ...secondArgs)\n\n\u002F\u002F formatMoney :: (String, Number, Number) -> String\nconst formatMoney = (currency, digits, amount) => `${amount.toFixed(digits)} ${currency}`\n\n\u002F\u002F your code\n\nconsole.log(toUsd(19.9))\nconsole.log(toUsd(5))\nconsole.log(toBtc(0.0015))\nconsole.log(toEur(2, 7.5))",{"ru":7,"en":47},"\u002F\u002F partial from the lesson, written more compactly\n\u002F\u002F partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)\nconst partial = (fn) => (...firstArgs) =>\n  firstArgs.length >= fn.length\n    ? fn(...firstArgs)\n    : (...secondArgs) => fn(...firstArgs, ...secondArgs)\n\n\u002F\u002F formatMoney :: (String, Number, Number) -> String\nconst formatMoney = (currency, digits, amount) => `${amount.toFixed(digits)} ${currency}`\n\n\u002F\u002F the currency and the number of decimal places are fixed in one call\nconst toUsd = partial(formatMoney)('USD', 2)\nconst toBtc = partial(formatMoney)('BTC', 6)\n\u002F\u002F only the currency is fixed: the decimal places and the amount come in one call\nconst toEur = partial(formatMoney)('EUR')\n\nconsole.log(toUsd(19.9))\nconsole.log(toUsd(5))\nconsole.log(toBtc(0.0015))\nconsole.log(toEur(2, 7.5))",{"ru":7,"en":49},"19.90 USD\n5.00 USD\n0.001500 BTC\n7.50 EUR",{"task":51,"starter":53,"solution":55,"expected":57},{"ru":7,"en":52},"Compare partial application and currying on volume(length, width, height). Get volumePartial with partial and volumeCurried with curry from Ramda. Print the volume 2 × 3 × 4 computed first with volumePartial and then with volumeCurried: pass 2 in the first call and the remaining arguments the way each function allows. Then print volumePartial(2)(3) and the type of volumeCurried(2)(3), and explain why the results differ.",{"ru":7,"en":54},"\u002F\u002F partial from the lesson, written more compactly\n\u002F\u002F partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)\nconst partial = (fn) => (...firstArgs) =>\n  firstArgs.length >= fn.length\n    ? fn(...firstArgs)\n    : (...secondArgs) => fn(...firstArgs, ...secondArgs)\n\n\u002F\u002F volume :: (Number, Number, Number) -> Number\nconst volume = (length, width, height) => length * width * height\n\n\u002F\u002F your code",{"ru":7,"en":56},"\u002F\u002F partial from the lesson, written more compactly\n\u002F\u002F partial :: ((a, b, c, ..., n) -> x) -> (a, b, c, ...) -> ((d, e, f, ..., n) -> x)\nconst partial = (fn) => (...firstArgs) =>\n  firstArgs.length >= fn.length\n    ? fn(...firstArgs)\n    : (...secondArgs) => fn(...firstArgs, ...secondArgs)\n\n\u002F\u002F volume :: (Number, Number, Number) -> Number\nconst volume = (length, width, height) => length * width * height\n\nconst volumePartial = partial(volume)\nconst volumeCurried = R.curry(volume)\n\n\u002F\u002F the remaining arguments in a single call\nconsole.log(volumePartial(2)(3, 4))\n\u002F\u002F one argument per call\nconsole.log(volumeCurried(2)(3)(4))\n\n\u002F\u002F the second call of partial runs volume no matter what:\n\u002F\u002F height = undefined, and 2 * 3 * undefined is NaN\nconsole.log(volumePartial(2)(3))\n\u002F\u002F the curried function waits for all arguments and returns a new function for now\nconsole.log(typeof volumeCurried(2)(3))",{"ru":7,"en":58},"24\n24\nNaN\nfunction",10,{"slug":61,"title":62},"currying",{"ru":7,"en":63},"Currying",{"slug":65,"title":66},"point-free",{"ru":7,"en":67},"Point-free style",1792684724207]