[{"data":1,"prerenderedAt":57},["ShallowReactive",2],{"course-en-imperative-vs-declarative":3},{"lesson":4,"number":51,"prev":52,"next":53},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":23,"exercises":32},"imperative-vs-declarative",{"ru":7,"en":8},"","Imperative vs declarative",{"ru":7,"en":10},"How imperative code differs from declarative in JavaScript: swapping a loop with push for filter and a predicate, then raising the abstraction with filterBy.",{"ru":7,"en":12},"Understand the difference between imperative code, which tells you how you do it, and declarative code, which tells you what the code does, and learn to rewrite a loop with filter and a predicate.",[14,17,19],{"type":15,"ru":7,"en":16},"text","Imperative code tells you \"how you do it\". In other words, you have to dig into the code itself.",{"type":15,"ru":7,"en":18},"Declarative code tells you what your code does; it is more often modular, and you do not need to dig into the whole codebase. If changes are needed, you make them in a specific section of the code in a modular way.",{"type":20,"code":21},"code",{"ru":7,"en":22},"\u002F\u002F for example, get all numbers greater than 3 from the array\n\n\u002F\u002F Imperative code\n\u002F\u002F ========\nlet result = [] \u002F\u002F the resulting array\n\nconst numbers = [1, 2, 3, 4, 5] \u002F\u002F the source array\n\nfor (let i = 0; i \u003C numbers.length; i++) {\n  if (numbers[i] > 3) {\n    result.push(numbers[i])\n  }\n}\n\nconsole.log(result) \u002F\u002F 4, 5\n\u002F\u002F =========\n\n\n\u002F\u002F Declarative code\n\u002F\u002F =========\n\u002F\u002F isGt3 :: Number -> Boolean\nconst isGt3 = number => number > 3\n\nconst declarativeResult = numbers.filter(isGt3) \u002F\u002F we don't dig into the implementation, we just read the name\n\nconsole.log(declarativeResult) \u002F\u002F 4, 5: the same result, but more declarative\n\u002F\u002F =========\n\n\u002F\u002F We can go further and raise the level of abstraction\n\u002F\u002F =========\n\u002F\u002F don't worry if it isn't clear right away, it will be later\n\n\u002F\u002F filter :: ((a -> Boolean), [a]) -> [a]\nconst filter = (predicat, arr) => arr.filter(predicat)\n\n\u002F\u002F filterBy :: (a -> Boolean) -> [a] -> [a]\nconst filterBy = (predicat) => (arr) => filter(predicat, arr)\n\n\u002F\u002F gt :: Number -> Number -> Boolean\nconst gt = pointer => number => number > pointer\n\n\u002F\u002F now we can create any predicate functions\n\u002F\u002F for filtering\n\u002F\u002F gt2 :: Number -> Boolean\nconst gt2 = gt(2) \u002F\u002F predicate that keeps numbers greater than 2\nconst gt3 = gt(3) \u002F\u002F predicate that keeps numbers greater than 3\n\n\u002F\u002F and now we can filter an array by any predicate\nconst arr = [1, 2, 3, 4, 5]\n\nconst more2 = filterBy (gt2) (arr) \u002F\u002F numbers greater than 2\nconst more3 = filterBy (gt3) (arr) \u002F\u002F numbers greater than 3\n\n\u002F\u002F we can also prepare the main filtering function for pipelines\n\u002F\u002F without calling it right away\n\u002F\u002F filterByGt2 :: [a] -> [a]\nconst filterByGt2 = filterBy (gt2) \u002F\u002F waits for an array\n\n\u002F\u002F filterByGt2 (arr): the name already tells us it keeps numbers greater than 2\nconst resultGt2 = filterByGt2 (arr) \u002F\u002F the final result: numbers greater than 2\n\u002F\u002F ======",[24,26,28,30],{"ru":7,"en":25},"Imperative code tells you how you do it: to understand it, you have to dig into the code itself.",{"ru":7,"en":27},"Declarative code tells you what it does. It is more often modular, and changes are made in a specific section of the code.",{"ru":7,"en":29},"filter with the isGt3 predicate replaces a loop with a condition and push: we don't dig into the implementation, we just read the name.",{"ru":7,"en":31},"The level of abstraction can be raised further: gt(2) creates a predicate, and filterBy(gt2) creates a filtering function that waits for an array. The name filterByGt2 already tells you what it does.",[33,42],{"task":34,"starter":36,"solution":38,"expected":40},{"ru":7,"en":35},"Rewrite the imperative code declaratively: get the words longer than four characters from the words array using filter and an isLonger4 predicate. The imperative version is already in the starter; both versions should print the same result.",{"ru":7,"en":37},"const words = ['map', 'filter', 'reduce', 'pipe']\n\n\u002F\u002F Imperative code\nconst imperativeResult = []\n\nfor (let i = 0; i \u003C words.length; i++) {\n  if (words[i].length > 4) {\n    imperativeResult.push(words[i])\n  }\n}\n\nconsole.log(imperativeResult)\n\n\u002F\u002F Declarative code\n\u002F\u002F isLonger4 :: String -> Boolean\n\u002F\u002F your code",{"ru":7,"en":39},"const words = ['map', 'filter', 'reduce', 'pipe']\n\n\u002F\u002F Imperative code\nconst imperativeResult = []\n\nfor (let i = 0; i \u003C words.length; i++) {\n  if (words[i].length > 4) {\n    imperativeResult.push(words[i])\n  }\n}\n\nconsole.log(imperativeResult)\n\n\u002F\u002F Declarative code\n\u002F\u002F isLonger4 :: String -> Boolean\nconst isLonger4 = word => word.length > 4\n\nconst declarativeResult = words.filter(isLonger4) \u002F\u002F we read the name, not the implementation\n\nconsole.log(declarativeResult) \u002F\u002F the same result",{"ru":7,"en":41},"[\n  \"filter\",\n  \"reduce\"\n]\n[\n  \"filter\",\n  \"reduce\"\n]",{"task":43,"starter":45,"solution":47,"expected":49},{"ru":7,"en":44},"Following the gt example from the lesson, write a divisibleBy function: it takes a divisor and returns a predicate \"the number is divisible by it with no remainder\". Use it with filterBy to get the filterByDivisibleBy3 and filterByDivisibleBy5 functions, and apply them to the numbers array.",{"ru":7,"en":46},"\u002F\u002F filterBy :: (a -> Boolean) -> [a] -> [a]\nconst filterBy = (predicate) => (arr) => arr.filter(predicate)\n\nconst numbers = [3, 5, 9, 10, 15]\n\n\u002F\u002F divisibleBy :: Number -> Number -> Boolean\n\u002F\u002F your code\n\n\u002F\u002F filterByDivisibleBy3 :: [Number] -> [Number]\n\u002F\u002F filterByDivisibleBy5 :: [Number] -> [Number]\n\u002F\u002F your code\n\nconsole.log(filterByDivisibleBy3(numbers))\nconsole.log(filterByDivisibleBy5(numbers))",{"ru":7,"en":48},"\u002F\u002F filterBy :: (a -> Boolean) -> [a] -> [a]\nconst filterBy = (predicate) => (arr) => arr.filter(predicate)\n\nconst numbers = [3, 5, 9, 10, 15]\n\n\u002F\u002F divisibleBy :: Number -> Number -> Boolean\nconst divisibleBy = (divisor) => (number) => number % divisor === 0\n\n\u002F\u002F filterByDivisibleBy3 :: [Number] -> [Number]\n\u002F\u002F filterByDivisibleBy5 :: [Number] -> [Number]\nconst filterByDivisibleBy3 = filterBy(divisibleBy(3)) \u002F\u002F waits for an array\nconst filterByDivisibleBy5 = filterBy(divisibleBy(5))\n\nconsole.log(filterByDivisibleBy3(numbers))\nconsole.log(filterByDivisibleBy5(numbers))",{"ru":7,"en":50},"[\n  3,\n  9,\n  15\n]\n[\n  5,\n  10,\n  15\n]",1,null,{"slug":54,"title":55},"functional-programming",{"ru":7,"en":56},"Functional programming",1792684724207]