[{"data":1,"prerenderedAt":69},["ShallowReactive",2],{"course-en-first-class-and-higher-order-functions":3},{"lesson":4,"number":60,"prev":61,"next":65},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":32,"exercises":41},"first-class-and-higher-order-functions",{"ru":7,"en":8},"","First-class and higher-order functions",{"ru":7,"en":10},"First-class functions as a property of JavaScript, and higher-order functions that take or return functions: map, React HOCs and Vue slots.",{"ru":7,"en":12},"Understand the difference between first-class functions, a property of the language, and higher-order functions, which take or return functions, and learn to write your own higher-order functions.",[14,17,19,21,25,27,29],{"type":15,"ru":7,"en":16},"text","Functions in JavaScript are first-class, and that is what makes higher-order functions possible.",{"type":15,"ru":7,"en":18},"First-class functions are a property of the language: in it, functions are values just like numbers or strings. A function can be assigned to a variable, passed as an argument to another function, and returned from a function.",{"type":15,"ru":7,"en":20},"Higher-order functions is the term commonly used for functions that accept other functions as arguments or return a function.",{"type":22,"code":23},"code",{"ru":7,"en":24},"\u002F\u002F A simple example\n\u002F\u002F ===============================\nconst data = [1, 2, 3, 4]\n\n\u002F\u002F multiply can be passed as an argument to another function,\n\u002F\u002F because functions in JavaScript are first-class:\n\u002F\u002F they are values just like numbers or strings\n\u002F\u002F multiply :: Number -> Number\nconst multiply = (v) => v * 2\n\n\u002F\u002F map is a\n\u002F\u002F higher-order function,\n\u002F\u002F because it takes another function\n\u002F\u002F as an argument\nconst result = data.map(multiply)\n\nconsole.log (result) \u002F\u002F [2, 4, 6, 8]",{"type":15,"ru":7,"en":26},"In short, a function that takes or returns a function is a higher-order function. \"First-class\", on the other hand, is not the role a function plays in a call but a property of the language: in JavaScript any function can be passed or returned like an ordinary value.",{"type":15,"ru":7,"en":28},"In React a component is a value (a function or a class), so it can be passed to a function: a higher-order component (HOC) is a function that takes a component and returns a new component. In Vue a component is usually described by an options object rather than a function (only a functional component is a plain function), but the idea is the same: components and slots are passed around as values, and in render functions slots are functions that receive data and return virtual nodes (vnodes).",{"type":22,"code":30},{"ru":7,"en":31},"\u002F\u002F A simple example\n\u002F\u002F ===============================\n\n\u002F\u002F hof :: (* -> *) -> * -> *\nconst hof = (fc) => (...args) => fc (...args)\n\n\u002F\u002F hof is a higher-order function:\n\u002F\u002F it takes the function fc and returns a new function\n\n\u002F\u002F fc can be passed to hof because functions\n\u002F\u002F in JavaScript are first-class\n\n\u002F\u002F An example in the spirit of Vue slots (and render props in React)\n\u002F\u002F MyComponent is a higher-order function: it takes slot functions\n\u002F\u002F and calls them, passing data to them, like a scoped slot\n\u002F\u002F This is not a higher-order component (HOC): a HOC takes a component and returns a new component\n\u002F\u002F A simplified model: real Vue returns virtual nodes, not strings\nfunction MyComponent({ main, default: def } = {}) {\n  const greetingMessage = 'hello'\n\n  if (main) {\n    return `\u003Cdiv>main - ${ main (greetingMessage)}\u003C\u002Fdiv>`\n  }\n\n  if (def) {\n    return `default - ${def(greetingMessage)}`\n  }\n\n  return 'no components'\n}\n\n\u002F\u002F calling the higher-order function: we pass slot functions to it\nconst templateMain = MyComponent({\n  main: (text) => `XXX${text}XXX`\n})\n\nconst templateDefault = MyComponent({\n  default: (text) => `${text}`\n})\n\nconsole.log(templateMain)    \u002F\u002F \u003Cdiv>main - XXXhelloXXX\u003C\u002Fdiv>\nconsole.log(templateDefault) \u002F\u002F default - hello",[33,35,37,39],{"ru":7,"en":34},"First-class functions are a property of the language: in JavaScript, functions are values just like numbers or strings. A function can be assigned to a variable, passed as an argument to another function, and returned from a function.",{"ru":7,"en":36},"A higher-order function takes another function as an argument or returns a function. map is a higher-order function because it takes another function.",{"ru":7,"en":38},"\"First-class\" is not the role a function plays in a call but a property of the language. It is what makes higher-order functions possible in JavaScript.",{"ru":7,"en":40},"A higher-order component (HOC) in React is a function that takes a component and returns a new component. In Vue render functions, slots are functions that receive data and return virtual nodes.",[42,51],{"task":43,"starter":45,"solution":47,"expected":49},{"ru":7,"en":44},"Write a higher-order function twice: it takes a function fn and returns a new function that applies fn to a value twice. Check it with add3 and double, then pass the result of twice back into twice.",{"ru":7,"en":46},"\u002F\u002F add3 :: Number -> Number\nconst add3 = (x) => x + 3\n\n\u002F\u002F double :: Number -> Number\nconst double = (x) => x * 2\n\n\u002F\u002F twice :: (a -> a) -> a -> a\n\u002F\u002F your code\n\nconsole.log(twice(add3)(10))\nconsole.log(twice(double)(5))\nconsole.log(twice(twice(double))(3)) \u002F\u002F twice returned a function, so it can be passed to twice again",{"ru":7,"en":48},"\u002F\u002F add3 :: Number -> Number\nconst add3 = (x) => x + 3\n\n\u002F\u002F double :: Number -> Number\nconst double = (x) => x * 2\n\n\u002F\u002F twice :: (a -> a) -> a -> a\n\u002F\u002F takes the function fn and returns a new function\nconst twice = (fn) => (x) => fn(fn(x))\n\nconsole.log(twice(add3)(10))\nconsole.log(twice(double)(5))\nconsole.log(twice(twice(double))(3)) \u002F\u002F twice returned a function, so it can be passed to twice again",{"ru":7,"en":50},"16\n20\n48",{"task":52,"starter":54,"solution":56,"expected":58},{"ru":7,"en":53},"Write a higher-order function withLog: it takes a name, then a function, and returns a new function that returns the same result as the original one and prints a line like \"double(1) = 2\". Pass the wrapped function to map, as multiply is passed in the lesson.",{"ru":7,"en":55},"\u002F\u002F double :: Number -> Number\nconst double = (x) => x * 2\n\n\u002F\u002F withLog :: String -> (Number -> Number) -> Number -> Number\n\u002F\u002F your code\n\nconst loggedDouble = withLog('double')(double)\n\nconst doubled = [1, 2, 3].map(loggedDouble)\n\nconsole.log(doubled)",{"ru":7,"en":57},"\u002F\u002F double :: Number -> Number\nconst double = (x) => x * 2\n\n\u002F\u002F withLog :: String -> (Number -> Number) -> Number -> Number\n\u002F\u002F withLog(name) takes the function fn and returns a new one: the same result plus a line in the console\nconst withLog = (name) => (fn) => (x) => {\n  const value = fn(x)\n  console.log(`${name}(${x}) = ${value}`)\n  return value\n}\n\nconst loggedDouble = withLog('double')(double)\n\nconst doubled = [1, 2, 3].map(loggedDouble)\n\nconsole.log(doubled)",{"ru":7,"en":59},"double(1) = 2\ndouble(2) = 4\ndouble(3) = 6\n[\n  2,\n  4,\n  6\n]",3,{"slug":62,"title":63},"functional-programming",{"ru":7,"en":64},"Functional programming",{"slug":66,"title":67},"closures",{"ru":7,"en":68},"Closures",1792684724207]