[{"data":1,"prerenderedAt":106},["ShallowReactive",2],{"course-en-point-free":3},{"lesson":4,"number":97,"prev":98,"next":102},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":67,"exercises":78},"point-free",{"ru":7,"en":8},"","Point-free style",{"ru":7,"en":10},"Point-free style in JavaScript: functions without named arguments via pipe, compose and curried Ramda functions, the map(parseInt) trap and debugging with tap.",{"ru":7,"en":12},"Learn to write functions in point-free style with pipe, compose and curried functions, understand why ['1', '2', '3'].map(parseInt) gives [1, NaN, NaN], debug such pipelines with tap, and see when a named argument reads better.",[14,17,19,21,25,27,30,32,35,37,39,41,44,46,49,51,53,65],{"type":15,"ru":7,"en":16},"text","Point-free style (also called tacit programming) is a way to define a function without mentioning its arguments. The new function is assembled from other functions through composition, currying and partial application. Instead of \"take a user and return their name\" we write \"this is R.prop('name')\".",{"type":15,"ru":7,"en":18},"A \"point\" here is not the dot in user.name but a function argument. The term comes from mathematics: in topology, functions act on spaces made of points, and a point-free definition of a function does not name the points, that is, the values it is applied to. So R.prop('name') is point-free even though it contains a dot.",{"type":15,"ru":7,"en":20},"The style relies on what we already know: a curried function returns a new function until it has all of its arguments, and pipe and compose chain functions into a pipeline. In Ramda functions are curried and, as a rule, take the data as their last argument, so R.filter(isOnline) is already a ready-made function waiting for a list.",{"type":22,"code":23},"code",{"ru":7,"en":24},"\u002F\u002F the course playground exposes Ramda as R\nconst users = [\n  { name: 'John', status: 'online', age: 25 },\n  { name: 'Alex', status: 'offline', age: 30 },\n  { name: 'Angel', status: 'online', age: 16 },\n]\n\n\u002F\u002F === with \"points\": the arguments are named explicitly ===\n\u002F\u002F getNameP :: Object -> String\nconst getNameP = (user) => user.name\n\u002F\u002F isOnlineP :: Object -> Boolean\nconst isOnlineP = (user) => user.status === 'online'\n\u002F\u002F onlineNamesP :: [Object] -> [String]\nconst onlineNamesP = (list) => list.filter((user) => isOnlineP(user)).map((user) => getNameP(user))\n\n\u002F\u002F === without \"points\": functions assembled from other functions ===\n\u002F\u002F getName :: Object -> String\nconst getName = R.prop('name')\n\u002F\u002F isOnline :: Object -> Boolean\n\u002F\u002F in current versions of Ramda propEq takes the value first and then the key,\n\u002F\u002F older versions had the opposite order\nconst isOnline = R.propEq('online', 'status')\n\u002F\u002F onlineNames :: [Object] -> [String]\nconst onlineNames = R.pipe(R.filter(isOnline), R.map(getName))\n\u002F\u002F the same with compose: the steps are written right to left\nconst onlineNamesC = R.compose(R.map(getName), R.filter(isOnline))\n\nconsole.log(onlineNamesP(users).join(', ')) \u002F\u002F John, Angel\nconsole.log(onlineNames(users).join(', ')) \u002F\u002F John, Angel\nconsole.log(onlineNamesC(users).join(', ')) \u002F\u002F John, Angel\n\n\u002F\u002F === your own curried functions work the same way ===\n\u002F\u002F add :: Number -> Number -> Number\nconst add = (a) => (b) => a + b\n\u002F\u002F with a point: the wrapper only passes n along\n\u002F\u002F incP :: Number -> Number\nconst incP = (n) => add(1)(n)\n\u002F\u002F without a point: add(1) is already a function waiting for the second number\n\u002F\u002F inc :: Number -> Number\nconst inc = add(1)\n\nconsole.log(incP(41)) \u002F\u002F 42\nconsole.log(inc(41)) \u002F\u002F 42",{"type":15,"ru":7,"en":26},"The simplest step towards point-free code is dropping a wrapper: (x) => f(x) does the same as f itself. That is also the main trap of the style. The wrapper passes exactly one argument to f, while f without the wrapper receives everything the calling code passes.",{"type":28,"ru":7,"en":29},"heading","The trap: extra arguments",{"type":15,"ru":7,"en":31},"Array.prototype.map calls its callback with three arguments: the element, its index and the whole array. parseInt takes two: a string and a radix. So ['1', '2', '3'].map(parseInt) actually calls parseInt('1', 0), parseInt('2', 1) and parseInt('3', 2), and returns [1, NaN, NaN].",{"type":22,"code":33},{"ru":7,"en":34},"\u002F\u002F map passes the element, the index and the array to its callback,\n\u002F\u002F and the second parameter of parseInt is the radix\nconsole.log(['1', '2', '3'].map(parseInt)) \u002F\u002F [1, NaN, NaN]\n\n\u002F\u002F what is actually called\n\u002F\u002F radix 0 means \"not set\": a string without the 0x prefix is read as a decimal number\nconsole.log(parseInt('1', 0)) \u002F\u002F 1\n\u002F\u002F radix 1 is invalid: parseInt accepts radixes from 2 to 36\nconsole.log(parseInt('2', 1)) \u002F\u002F NaN\n\u002F\u002F there is no digit 3 in binary\nconsole.log(parseInt('3', 2)) \u002F\u002F NaN\n\n\u002F\u002F === how to fix it ===\n\u002F\u002F Number uses only its first argument\n\u002F\u002F it is stricter than parseInt: Number('12px') is NaN, while parseInt('12px', 10) is 12\nconsole.log(['1', '2', '3'].map(Number)) \u002F\u002F [1, 2, 3]\n\u002F\u002F bring the \"point\" back and pass the radix explicitly\nconsole.log(['1', '2', '3'].map((str) => parseInt(str, 10))) \u002F\u002F [1, 2, 3]\n\u002F\u002F R.map passes only the element to its callback\nconsole.log(R.map(parseInt, ['1', '2', '3'])) \u002F\u002F [1, 2, 3]\n\u002F\u002F R.unary keeps one argument of a function and drops the rest\nconsole.log(['1', '2', '3'].map(R.unary(parseInt))) \u002F\u002F [1, 2, 3]",{"type":15,"ru":7,"en":36},"forEach, filter and other array methods behave the same way. With object methods the trap is different: arr.map(obj.method) calls the method without its object, and this inside it will be wrong. There is also a delayed version: if a function passed to map without a wrapper later gains an optional second parameter, the index will silently start arriving in it.",{"type":28,"ru":7,"en":38},"Readability and debugging",{"type":15,"ru":7,"en":40},"Point-free code is shorter but not always clearer: to read it you have to remember the signature of every function. R.lte(18) is the function age => 18 \u003C= age, and R.gt(18) is age => 18 > age. So R.filter(R.gt(18)) keeps those younger than 18, even though it reads as \"greater than 18\".",{"type":22,"code":42},{"ru":7,"en":43},"const users = [\n  { name: 'John', status: 'online', age: 25 },\n  { name: 'Alex', status: 'offline', age: 30 },\n  { name: 'Angel', status: 'online', age: 16 },\n]\n\n\u002F\u002F point-free: you need to know that R.lte(18) is age => 18 \u003C= age\n\u002F\u002F isAdultOnline :: Object -> Boolean\nconst isAdultOnline = R.both(R.propSatisfies(R.lte(18), 'age'), R.propEq('online', 'status'))\n\n\u002F\u002F with a point: the condition is visible at once\n\u002F\u002F isAdultOnlineP :: Object -> Boolean\nconst isAdultOnlineP = (user) => user.age >= 18 && user.status === 'online'\n\nconsole.log(users.filter(isAdultOnline).length) \u002F\u002F 1\nconsole.log(users.filter(isAdultOnlineP).length) \u002F\u002F 1\n\n\u002F\u002F R.gt(18) reads as \"greater than 18\", but it means age => 18 > age\n\u002F\u002F ages :: [Number]\nconst ages = [10, 18, 20, 30]\nconsole.log(R.filter(R.gt(18), ages).join(', ')) \u002F\u002F 10\n\u002F\u002F with a named argument you can't get it wrong\nconsole.log(ages.filter((age) => age > 18).join(', ')) \u002F\u002F 20, 30",{"type":15,"ru":7,"en":45},"The second cost is debugging. A point-free function has no body to put a breakpoint or a console.log in. You can't put console.log itself into the pipeline either: it returns undefined, and the next step receives that instead of the data. tap helps here: it calls the given function for its side effect and returns the argument unchanged. There is more about it in the \"Side effects in function composition\" lesson, and Ramda has a ready-made R.tap.",{"type":22,"code":47},{"ru":7,"en":48},"const users = [\n  { name: 'John', status: 'online', age: 25 },\n  { name: 'Alex', status: 'offline', age: 30 },\n  { name: 'Angel', status: 'online', age: 16 },\n]\n\n\u002F\u002F tap :: (a -> *) -> a -> a\n\u002F\u002F calls fn for its side effect and returns x unchanged\nconst tap = (fn) => (x) => {\n  fn(x)\n  return x\n}\n\n\u002F\u002F log :: String -> a -> a\nconst log = (label) => tap((value) => console.log(label + ': ' + JSON.stringify(value)))\n\n\u002F\u002F onlineNames :: [Object] -> String\nconst onlineNames = R.pipe(\n  R.filter(R.propEq('online', 'status')),\n  log('after filter'), \u002F\u002F prints the intermediate result\n  R.map(R.prop('name')),\n  log('after map'),\n  R.join(', '),\n)\n\nconsole.log(onlineNames(users))\n\u002F\u002F after filter: [{\"name\":\"John\",\"status\":\"online\",\"age\":25},{\"name\":\"Angel\",\"status\":\"online\",\"age\":16}]\n\u002F\u002F after map: [\"John\",\"Angel\"]\n\u002F\u002F John, Angel",{"type":28,"ru":7,"en":50},"When not to use it",{"type":15,"ru":7,"en":52},"Point-free style is a tool, not a goal. It works well where a pipeline reads as a list of steps: \"keep those who are online, take their names\". A named argument is better in these cases:",{"type":54,"ordered":55,"items":56},"list",false,[57,59,61,63],{"ru":7,"en":58},"the argument is needed in several places: (user) => user.firstName + ' ' + user.lastName is simpler than the same function built with R.converge;",{"ru":7,"en":60},"a point-free version requires reordering arguments with R.flip or the R.__ placeholder;",{"ru":7,"en":62},"the function you pass without a wrapper takes optional parameters or depends on this, like parseInt and object methods;",{"ru":7,"en":64},"the argument's name tells the reader what data the function works with.",{"type":15,"ru":7,"en":66},"A practical rule: build a pipeline out of ready-made functions without \"points\", and if that takes flip, converge or placeholders, name the argument.",[68,70,72,74,76],{"ru":7,"en":69},"Point-free style defines a function without mentioning its arguments: it is assembled from other functions through composition and currying, for example R.pipe(R.filter(isOnline), R.map(R.prop('name'))).",{"ru":7,"en":71},"The wrapper (x) => f(x) can be replaced with f only if f won't take extra arguments: ['1', '2', '3'].map(parseInt) gives [1, NaN, NaN] because map also passes the index and the array.",{"ru":7,"en":73},"The trap is avoided with Number, an explicit arrow function with a radix, R.map, which passes only the element, or R.unary.",{"ru":7,"en":75},"A point-free pipeline is debugged with tap: it performs a side effect and returns the argument unchanged.",{"ru":7,"en":77},"If a point-free version needs flip, converge or placeholders, or the argument's name explains something, a named argument reads better.",[79,88],{"task":80,"starter":82,"solution":84,"expected":86},{"ru":7,"en":81},"Rewrite getTitleP, isInStockP, toUpperP and availableTitlesP in point-free style: getTitle, isInStock, toUpper and availableTitles must not mention their arguments. R.prop, R.propEq (value first, then key), R.toUpper, R.filter, R.map and R.pipe will come in handy. Print the result of both versions and make sure it is the same.",{"ru":7,"en":83},"const products = [\n  { title: 'Book', price: 400, inStock: true },\n  { title: 'Pen', price: 50, inStock: false },\n  { title: 'Lamp', price: 1200, inStock: true },\n  { title: 'Mug', price: 300, inStock: true },\n]\n\n\u002F\u002F versions with \"points\"\n\u002F\u002F getTitleP :: Object -> String\nconst getTitleP = (product) => product.title\n\u002F\u002F isInStockP :: Object -> Boolean\nconst isInStockP = (product) => product.inStock === true\n\u002F\u002F toUpperP :: String -> String\nconst toUpperP = (str) => str.toUpperCase()\n\u002F\u002F availableTitlesP :: [Object] -> [String]\nconst availableTitlesP = (list) => list.filter((product) => isInStockP(product)).map((product) => toUpperP(getTitleP(product)))\n\n\u002F\u002F your code: getTitle, isInStock, toUpper, availableTitles\n\nconsole.log(availableTitlesP(products).join(', '))\nconsole.log(availableTitles(products).join(', '))",{"ru":7,"en":85},"const products = [\n  { title: 'Book', price: 400, inStock: true },\n  { title: 'Pen', price: 50, inStock: false },\n  { title: 'Lamp', price: 1200, inStock: true },\n  { title: 'Mug', price: 300, inStock: true },\n]\n\n\u002F\u002F versions with \"points\"\n\u002F\u002F getTitleP :: Object -> String\nconst getTitleP = (product) => product.title\n\u002F\u002F isInStockP :: Object -> Boolean\nconst isInStockP = (product) => product.inStock === true\n\u002F\u002F toUpperP :: String -> String\nconst toUpperP = (str) => str.toUpperCase()\n\u002F\u002F availableTitlesP :: [Object] -> [String]\nconst availableTitlesP = (list) => list.filter((product) => isInStockP(product)).map((product) => toUpperP(getTitleP(product)))\n\n\u002F\u002F getTitle :: Object -> String\nconst getTitle = R.prop('title')\n\u002F\u002F isInStock :: Object -> Boolean\nconst isInStock = R.propEq(true, 'inStock')\n\u002F\u002F a string method can't be passed without a wrapper: Ramda has a matching function\n\u002F\u002F toUpper :: String -> String\nconst toUpper = R.toUpper\n\u002F\u002F availableTitles :: [Object] -> [String]\n\u002F\u002F two steps in a row for each product are a pipeline too\nconst availableTitles = R.pipe(R.filter(isInStock), R.map(R.pipe(getTitle, toUpper)))\n\nconsole.log(availableTitlesP(products).join(', '))\nconsole.log(availableTitles(products).join(', '))",{"ru":7,"en":87},"BOOK, LAMP, MUG\nBOOK, LAMP, MUG",{"task":89,"starter":91,"solution":93,"expected":95},{"ru":7,"en":90},"sumCsv should add up the numbers in the string '10,20,30' and print 60, but it prints NaN. The starter has tap and a log helper. Build a debug version sumCsvDebug: the same pipeline with log after each step. Find the step where the data goes wrong, explain why, and fix sumCsv so that it stays point-free. Print the result of sumCsvDebug('10,20,30'), then that of the fixed sumCsv.",{"ru":7,"en":92},"\u002F\u002F tap :: (a -> *) -> a -> a\nconst tap = (fn) => (x) => {\n  fn(x)\n  return x\n}\n\u002F\u002F log :: String -> a -> a\n\u002F\u002F R.toString shows NaN, while JSON.stringify would turn it into null\nconst log = (label) => tap((value) => console.log(label + ': ' + R.toString(value)))\n\n\u002F\u002F map in the spirit of the \"Currying\" lesson: passes fn to the array method as is\n\u002F\u002F map :: (a -> b) -> [a] -> [b]\nconst map = (fn) => (arr) => arr.map(fn)\n\u002F\u002F split :: String -> String -> [String]\nconst split = (separator) => (str) => str.split(separator)\n\u002F\u002F sum :: [Number] -> Number\nconst sum = (arr) => arr.reduce((acc, n) => acc + n, 0)\n\n\u002F\u002F sumCsv :: String -> Number\nconst sumCsv = R.pipe(split(','), map(parseInt), sum)\n\n\u002F\u002F your code: sumCsvDebug and the fixed sumCsv\n\nconsole.log(sumCsv('10,20,30'))",{"ru":7,"en":94},"\u002F\u002F tap :: (a -> *) -> a -> a\nconst tap = (fn) => (x) => {\n  fn(x)\n  return x\n}\n\u002F\u002F log :: String -> a -> a\n\u002F\u002F R.toString shows NaN, while JSON.stringify would turn it into null\nconst log = (label) => tap((value) => console.log(label + ': ' + R.toString(value)))\n\n\u002F\u002F map in the spirit of the \"Currying\" lesson: passes fn to the array method as is\n\u002F\u002F map :: (a -> b) -> [a] -> [b]\nconst map = (fn) => (arr) => arr.map(fn)\n\u002F\u002F split :: String -> String -> [String]\nconst split = (separator) => (str) => str.split(separator)\n\u002F\u002F sum :: [Number] -> Number\nconst sum = (arr) => arr.reduce((acc, n) => acc + n, 0)\n\n\u002F\u002F the debug version: the same pipeline with log after each step\nconst sumCsvDebug = R.pipe(split(','), log('split'), map(parseInt), log('map'), sum)\nconsole.log(sumCsvDebug('10,20,30'))\n\n\u002F\u002F the data breaks after map: the array method also passes the index to parseInt,\n\u002F\u002F and it becomes the radix: parseInt('20', 1) and parseInt('30', 2) give NaN\n\u002F\u002F Number uses only its first argument, and the pipeline stays point-free\n\u002F\u002F sumCsv :: String -> Number\nconst sumCsv = R.pipe(split(','), map(Number), sum)\n\nconsole.log(sumCsv('10,20,30'))",{"ru":7,"en":96},"split: [\"10\", \"20\", \"30\"]\nmap: [10, NaN, NaN]\nNaN\n60",11,{"slug":99,"title":100},"partial-application",{"ru":7,"en":101},"Partial application",{"slug":103,"title":104},"currying-and-asynchrony",{"ru":7,"en":105},"Currying and asynchrony (evolution)",1792684724207]