[{"data":1,"prerenderedAt":53},["ShallowReactive",2],{"course-en-functional-programming":3},{"lesson":4,"number":44,"prev":45,"next":49},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":23,"exercises":34},"functional-programming",{"ru":7,"en":8},"","Functional programming",{"ru":7,"en":10},"What functional programming means in JavaScript: pure functions, a pipeline of small functions with pipe, and Maybe from head and fromMaybe in Sanctuary.",{"ru":7,"en":12},"Understand what functional programming is: declarative development based on pure functions, in which a task is solved by a pipeline of small functions.",[14,17,19],{"type":15,"ru":7,"en":16},"text","Functional programming is high-level, declarative development based on pure functions. Functional programming is a programming paradigm that relies on functions as its primary building blocks.",{"type":15,"ru":7,"en":18},"Pure functions are functions that return the same result for the same arguments and produce no side effects.",{"type":20,"code":21},"code",{"ru":7,"en":22},"\u002F\u002F ==== Note ===\n\u002F\u002F These examples may seem very confusing and unclear to you for now\n\u002F\u002F Don't worry, just keep reading\n\n\u002F\u002F our goal\n\u002F\u002F 1. Get the users whose status is online\n\u002F\u002F 2. Who are older than 20\n\u002F\u002F 3. Take the first user from the list\n\u002F\u002F 4. Get their name\n\n\u002F\u002F An example of functional code\n\u002F\u002F ready-made helpers from the Sanctuary library: in the course playground it is available as S,\n\u002F\u002F in your own project: import S from 'sanctuary'\nconst { pipe, prop, equals, filter, head, gt, map, fromMaybe } = S\n\n\u002F\u002F fetchAllUsers :: () -> Promise [Object]\nconst fetchAllUsers = () => new Promise(resolve => resolve([\n  {\n    name: 'John',\n    status: 'online',\n    age: 25\n  },\n  {\n    name: 'Alex',\n    status: 'offline',\n    age: 18\n  },\n  {\n    name: 'Angel',\n    status: 'online',\n    age: 18\n  }\n]))\n\nconst ONLINE = 'online'\n\n\u002F\u002F isStatus :: String -> (a -> Boolean)\nconst isStatus = status => pipe([\n  prop('status'), \u002F\u002F take the user's status field\n  equals(status), \u002F\u002F compare it with the status given above\n])\n\n\u002F\u002F moreAge :: Number -> (a -> Boolean)\nconst moreAge = age => pipe([\n  prop('age'), \u002F\u002F take the age field\n  gt(age), \u002F\u002F check that the user's field is greater than age\n])\n\n\u002F\u002F isStatusOnline :: a -> Boolean\nconst isStatusOnline = isStatus(ONLINE)\n\n\u002F\u002F  moreAge20 :: a -> Boolean\nconst moreAge20 = moreAge(20)\n\n\u002F\u002F getName :: a -> String\nconst getName = prop('name')\n\n\u002F\u002F Note: you don't have to build excessive abstractions,\n\u002F\u002F they can sometimes confuse your colleagues\n\u002F\u002F Ideally, of course, you would wrap the data in a monad so the composition doesn't crash\n\u002F\u002F when the server sends back something unexpected. That is protection at run time:\n\u002F\u002F TypeScript types on their own are checked only at compile time and don't protect against such data\n\n\u002F\u002F searchOnlineUsers :: [a] -> String\nconst searchOnlineUsers= pipe([\n  filter(isStatusOnline), \u002F\u002F keep only the users who are online\n  filter(moreAge20), \u002F\u002F keep the users older than 20\n  head, \u002F\u002F take the first user wrapped in Maybe: Just (user), or Nothing if the list is empty\n  map(getName), \u002F\u002F get that user's name inside the Maybe\n  fromMaybe('') \u002F\u002F take the name out of the Maybe; for Nothing it is ''\n])\n\n\u002F\u002F program start\nfetchAllUsers()\n  .then(searchOnlineUsers)\n  .then(name => console.log(name)) \u002F\u002F 'John'",[24,26,28,30,32],{"ru":7,"en":25},"Functional programming is high-level, declarative development based on pure functions. Functions are its primary building blocks.",{"ru":7,"en":27},"Pure functions return the same result for the same arguments and produce no side effects.",{"ru":7,"en":29},"In the example, the task is solved by a pipeline of small functions built with pipe: keep the users who are online, then those older than 20, take the first one and get their name.",{"ru":7,"en":31},"head returns a Maybe: Just (user), or Nothing if the list is empty. fromMaybe takes the value out of the Maybe and substitutes a fallback for Nothing.",{"ru":7,"en":33},"You don't have to build excessive abstractions: they can sometimes confuse your colleagues.",[35],{"task":36,"starter":38,"solution":40,"expected":42},{"ru":7,"en":37},"Build a firstOfflineName pipeline out of small functions: it returns the name of the first user whose status is offline, or the string \"nobody\" if there is no such user. Use isStatus and getName from the starter and the Sanctuary functions filter, head, map and fromMaybe, as in the lesson. Check the pipeline on both lists.",{"ru":7,"en":39},"\u002F\u002F ready-made Sanctuary helpers: in the course playground it is available as S\nconst { pipe, prop, equals, filter, head, map, fromMaybe } = S\n\nconst users = [\n  { name: 'John', status: 'online' },\n  { name: 'Alex', status: 'offline' },\n  { name: 'Angel', status: 'online' },\n]\n\n\u002F\u002F here every user is online\nconst onlineUsers = [\n  { name: 'John', status: 'online' },\n  { name: 'Angel', status: 'online' },\n]\n\n\u002F\u002F isStatus :: String -> (a -> Boolean)\nconst isStatus = status => pipe([\n  prop('status'),\n  equals(status),\n])\n\n\u002F\u002F getName :: a -> String\nconst getName = prop('name')\n\n\u002F\u002F firstOfflineName :: [a] -> String\n\u002F\u002F your code\n\nconsole.log(firstOfflineName(users))\nconsole.log(firstOfflineName(onlineUsers))",{"ru":7,"en":41},"\u002F\u002F ready-made Sanctuary helpers: in the course playground it is available as S\nconst { pipe, prop, equals, filter, head, map, fromMaybe } = S\n\nconst users = [\n  { name: 'John', status: 'online' },\n  { name: 'Alex', status: 'offline' },\n  { name: 'Angel', status: 'online' },\n]\n\n\u002F\u002F here every user is online\nconst onlineUsers = [\n  { name: 'John', status: 'online' },\n  { name: 'Angel', status: 'online' },\n]\n\n\u002F\u002F isStatus :: String -> (a -> Boolean)\nconst isStatus = status => pipe([\n  prop('status'),\n  equals(status),\n])\n\n\u002F\u002F getName :: a -> String\nconst getName = prop('name')\n\n\u002F\u002F firstOfflineName :: [a] -> String\nconst firstOfflineName = pipe([\n  filter(isStatus('offline')), \u002F\u002F only the users who are offline\n  head, \u002F\u002F the first of them in a Maybe: Just (user) or Nothing\n  map(getName), \u002F\u002F the name inside the Maybe\n  fromMaybe('nobody'), \u002F\u002F a fallback string for Nothing\n])\n\nconsole.log(firstOfflineName(users))\nconsole.log(firstOfflineName(onlineUsers))",{"ru":7,"en":43},"Alex\nnobody",2,{"slug":46,"title":47},"imperative-vs-declarative",{"ru":7,"en":48},"Imperative vs declarative",{"slug":50,"title":51},"first-class-and-higher-order-functions",{"ru":7,"en":52},"First-class and higher-order functions",1792684724207]