[{"data":1,"prerenderedAt":70},["ShallowReactive",2],{"course-en-category-theory":3},{"lesson":4,"number":61,"prev":62,"next":66},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":33,"exercises":42},"category-theory",{"ru":7,"en":8},"","Category theory",{"ru":7,"en":10},"Category theory basics for JavaScript developers: types as objects, functions as morphisms, and the composition laws of identity and associativity.",{"ru":7,"en":12},"Understand what a category is in programming, with types as objects and functions as morphisms, and which two laws their composition obeys.",[14,17,19,21,25,27,29],{"type":15,"ru":7,"en":16},"text","There is a close connection between functional programming and category theory. In functional programming many abstractions, laws and principles are taken from category theory.",{"type":15,"ru":7,"en":18},"So what do we know about category theory? We will not dive into the fundamentals and academic terms. We will try to explain all of this without academic terminology.",{"type":15,"ru":7,"en":20},"A category, in the context of programming, is a collection of objects (data types: Array, Object, Number, String, ...) and morphisms between them (arrows, that is, functions). Morphisms f :: a -> b and g :: b -> c can be composed into g . f :: a -> c (read as \"g after f\": first f, then g). Composition obeys two laws. Identity: every object has an identity morphism id :: a -> a, and composing with it changes nothing (f . id = f, id . f = f). Associativity: h . (g . f) = (h . g) . f, so it does not matter where the parentheses go.",{"type":22,"code":23},"code",{"ru":7,"en":24},"\u002F\u002F  Example\n\u002F\u002F f :: a -> b\n\u002F\u002F g :: b -> c\n\u002F\u002F g . f :: a -> c\n\n\u002F\u002F a, b, c are objects (types)\n\u002F\u002F f, g are morphisms (arrows), that is, functions\n\u002F\u002F g . f is composition: first f, then g\n\n\u002F\u002F A practical example\n\n\u002F\u002F concatX :: Number -> String\nconst concatX = num => `${num} X`\n\n\u002F\u002F toArray:: a -> [a]\nconst toArray = word => [word]\n\n\u002F\u002F compose :: ((b -> c), (a -> b)) -> a -> c\nconst compose = (f2, f1) => (...args) => f2(f1(...args))\n\nconst pipeline = compose(\n  toArray, \u002F\u002F turn the string into an array\n  concatX \u002F\u002F append X and turn it into a string\n  )\n\n\u002F\u002F start\npipeline(5) \u002F\u002F ['5 X'] is the result\n\n\u002F\u002F So what happened here\n\u002F\u002F the input argument 5 is a value of type \"a\" (Number)\n\u002F\u002F we concatenated the letter X and got '5 X', a value of type \"b\" (String)\n\u002F\u002F we put it into an array and got ['5 X'], a value of type \"c\" (Array)\n\u002F\u002F concatX :: Number -> String is the morphism f :: a -> b\n\u002F\u002F toArray :: String -> [String] is the morphism g :: b -> c\n\u002F\u002F pipeline = compose(toArray, concatX) :: Number -> [String] is the composition g . f :: a -> c",{"type":15,"ru":7,"en":26},"Let's use our imagination to understand category theory more deeply. We will draw analogies from the world around us.",{"type":15,"ru":7,"en":28},"We will use nature and write pseudocode that illustrates the ideas of category theory.",{"type":22,"pseudo":30,"code":31},true,{"ru":7,"en":32},"\u002F\u002F  Example\nsolid -> liquid -> gas\nice -> water -> steam\n\nice, water, steam \u002F\u002F these are objects\n-> \u002F\u002F morphisms: processes applied to them (heating and so on)\nturnIntoSteam . turnIntoWater :: ice -> steam \u002F\u002F composition of processes\n\n\u002F\u002F A practical example\n\n\u002F\u002F turnIntoWater :: ice -> water\nconst turnIntoWater = ice => (keep at room temperature, water)\n\n\u002F\u002F turnIntoSteam:: water -> steam\nconst turnIntoSteam = water=> (apply strong heat, steam)\n\n\u002F\u002F composeProcesses :: ((b -> c), (a -> b)) -> a -> c\nconst composeProcesses = (f2, f1) => (...args) => f2(f1(...args))\n\nconst pipeline = composeProcesses(\n  turnIntoSteam,\n  turnIntoWater\n  )\n\n\u002F\u002F start\npipeline(ice) \u002F\u002F steam is the result",[34,36,38,40],{"ru":7,"en":35},"In functional programming many abstractions, laws and principles are taken from category theory.",{"ru":7,"en":37},"A category in programming is a collection of objects (data types) and morphisms between them (arrows, that is, functions).",{"ru":7,"en":39},"Morphisms f :: a -> b and g :: b -> c compose into g . f :: a -> c, read as \"g after f\": first f, then g.",{"ru":7,"en":41},"Composition obeys two laws: identity (f . id = f, id . f = f) and associativity (h . (g . f) = (h . g) . f).",[43,52],{"task":44,"starter":46,"solution":48,"expected":50},{"ru":7,"en":45},"There are three morphisms: words :: String -> [String] splits a text into words, count :: [a] -> Number returns the length of an array, and isLong :: Number -> Boolean checks that a number is greater than three. Using compose from the lesson, build the morphism isLongText :: String -> Boolean out of them. Match the functions by type: the output of each function must be accepted by the next one. Remember that compose(g, f) is \"g after f\".",{"ru":7,"en":47},"\u002F\u002F compose :: ((b -> c), (a -> b)) -> a -> c\nconst compose = (f2, f1) => (...args) => f2(f1(...args))\n\n\u002F\u002F words :: String -> [String]\nconst words = (text) => text.split(' ')\n\n\u002F\u002F count :: [a] -> Number\nconst count = (list) => list.length\n\n\u002F\u002F isLong :: Number -> Boolean\nconst isLong = (n) => n > 3\n\n\u002F\u002F your code: isLongText :: String -> Boolean\n\nconsole.log(isLongText('map filter reduce'))\nconsole.log(isLongText('pipe compose curry tap lens'))",{"ru":7,"en":49},"\u002F\u002F compose :: ((b -> c), (a -> b)) -> a -> c\nconst compose = (f2, f1) => (...args) => f2(f1(...args))\n\n\u002F\u002F words :: String -> [String]\nconst words = (text) => text.split(' ')\n\n\u002F\u002F count :: [a] -> Number\nconst count = (list) => list.length\n\n\u002F\u002F isLong :: Number -> Boolean\nconst isLong = (n) => n > 3\n\n\u002F\u002F String -> [String] -> Number -> Boolean:\n\u002F\u002F first words, then count, then isLong\n\u002F\u002F isLongText :: String -> Boolean\nconst isLongText = compose(isLong, compose(count, words))\n\nconsole.log(isLongText('map filter reduce')) \u002F\u002F 3 words\nconsole.log(isLongText('pipe compose curry tap lens')) \u002F\u002F 5 words",{"ru":7,"en":51},"false\ntrue",{"task":53,"starter":55,"solution":57,"expected":59},{"ru":7,"en":54},"Check both category laws on the same morphisms. Write the identity morphism id :: a -> a and a function sameOn that treats two functions as equal if they give the same result on every string in the inputs list. The checks in the starter compare compose(id, countWords) and compose(countWords, id) with countWords, and then compose(isLong, compose(count, words)) with compose(compose(isLong, count), words), printing one line per check.",{"ru":7,"en":56},"\u002F\u002F compose :: ((b -> c), (a -> b)) -> a -> c\nconst compose = (f2, f1) => (...args) => f2(f1(...args))\n\n\u002F\u002F words :: String -> [String]\nconst words = (text) => text.split(' ')\n\u002F\u002F count :: [a] -> Number\nconst count = (list) => list.length\n\u002F\u002F isLong :: Number -> Boolean\nconst isLong = (n) => n > 3\n\n\u002F\u002F the identity morphism\n\u002F\u002F id :: a -> a\n\u002F\u002F your code\n\n\u002F\u002F the inputs on which we compare functions\nconst inputs = ['map filter reduce', 'pipe compose curry tap lens', 'functor']\n\n\u002F\u002F two functions are equal if they give the same result on every input\n\u002F\u002F sameOn :: ((a -> b), (a -> b)) -> Boolean\n\u002F\u002F your code\n\n\u002F\u002F countWords :: String -> Number\nconst countWords = compose(count, words)\n\n\u002F\u002F identity: composing with id changes nothing\nconsole.log(`id . countWords = countWords: ${sameOn(compose(id, countWords), countWords)}`)\nconsole.log(`countWords . id = countWords: ${sameOn(compose(countWords, id), countWords)}`)\n\n\u002F\u002F associativity: it does not matter where the parentheses go\nconst left = compose(isLong, compose(count, words))\nconst right = compose(compose(isLong, count), words)\nconsole.log(`isLong . (count . words) = (isLong . count) . words: ${sameOn(left, right)}`)",{"ru":7,"en":58},"\u002F\u002F compose :: ((b -> c), (a -> b)) -> a -> c\nconst compose = (f2, f1) => (...args) => f2(f1(...args))\n\n\u002F\u002F words :: String -> [String]\nconst words = (text) => text.split(' ')\n\u002F\u002F count :: [a] -> Number\nconst count = (list) => list.length\n\u002F\u002F isLong :: Number -> Boolean\nconst isLong = (n) => n > 3\n\n\u002F\u002F the identity morphism\n\u002F\u002F id :: a -> a\nconst id = (x) => x\n\n\u002F\u002F the inputs on which we compare functions\nconst inputs = ['map filter reduce', 'pipe compose curry tap lens', 'functor']\n\n\u002F\u002F two functions are equal if they give the same result on every input\n\u002F\u002F sameOn :: ((a -> b), (a -> b)) -> Boolean\nconst sameOn = (f, g) => inputs.every((x) => f(x) === g(x))\n\n\u002F\u002F countWords :: String -> Number\nconst countWords = compose(count, words)\n\n\u002F\u002F identity: composing with id changes nothing\nconsole.log(`id . countWords = countWords: ${sameOn(compose(id, countWords), countWords)}`)\nconsole.log(`countWords . id = countWords: ${sameOn(compose(countWords, id), countWords)}`)\n\n\u002F\u002F associativity: it does not matter where the parentheses go\nconst left = compose(isLong, compose(count, words))\nconst right = compose(compose(isLong, count), words)\nconsole.log(`isLong . (count . words) = (isLong . count) . words: ${sameOn(left, right)}`)",{"ru":7,"en":60},"id . countWords = countWords: true\ncountWords . id = countWords: true\nisLong . (count . words) = (isLong . count) . words: true",20,{"slug":63,"title":64},"side-effects-in-composition-functions",{"ru":7,"en":65},"Side effects in function composition",{"slug":67,"title":68},"functors",{"ru":7,"en":69},"Functors and their laws",1792684725069]