[{"data":1,"prerenderedAt":62},["ShallowReactive",2],{"course-en-hindley-milner-type-notation":3},{"lesson":4,"number":53,"prev":54,"next":58},{"slug":5,"title":6,"description":9,"goal":11,"blocks":13,"summary":25,"exercises":34},"hindley-milner-type-notation",{"ru":7,"en":8},"","Hindley–Milner type notation",{"ru":7,"en":10},"Hindley–Milner signatures in JavaScript: how to read a -> b -> c and (a, b) -> c, type variables, [a] and Maybe a, and write a function from its signature.",{"ru":7,"en":12},"Learn to read Hindley–Milner signatures — which arguments a function takes, what it returns and whether it is curried — and to write a function from its signature.",[14,17,19,21],{"type":15,"ru":7,"en":16},"text","You have already come across Hindley–Milner type notation in the code — above functions, in comment blocks.",{"type":15,"ru":7,"en":18},"This type notation is used in Haskell and other languages with a functional bent. In JS it helps us write more abstract code, and it also helps maintain that code.",{"type":15,"ru":7,"en":20},"It explains which argument types a function accepts and what it returns.",{"type":22,"code":23},"code",{"ru":7,"en":24},"\u002F\u002F without the notation\nconst foo = (a, b) => a + b\n\n\u002F\u002F from the function above we can't tell whether it adds or concatenates\n\u002F\u002F  of course, we could give foo a proper name\n\n\u002F\u002F of course, we could write the functions like this\nconst concat = (a, b) => a + b \u002F\u002F concatenation\nconst add = (a, b) => a + b \u002F\u002F addition\n\n\u002F\u002F with the notation\n\u002F\u002F functionName :: FirstArgumentType -> SecondArgumentType -> ResultType\n\u002F\u002F this is how a curried function is written: it takes its arguments one at a time\n\n\u002F\u002F addCurried :: Number -> Number -> Number\nconst addCurried = (a) => (b) => a + b \u002F\u002F addition\n\n\u002F\u002F for an uncurried function, the argument types are listed in parentheses, separated by commas\n\u002F\u002F add :: (Number, Number) -> Number\n\u002F\u002F concat :: (String, String) -> String\n\n\u002F\u002F map :: (a -> b) -> [a] -> [b]\nconst map = (mappable) => (arr) => arr.map(mappable)\n\n\u002F\u002F the same thing, but in the more general form used in category theory\n\u002F\u002F and the Fantasy-land specification; don't worry, we will get to that later\n\u002F\u002F Spoiler: a functor is any type that implements the map interface\n\u002F\u002F and obeys its laws (more on them in the \"Fantasy-land\" lesson),\n\u002F\u002F so this same map function works not only with an array but with any value that has a map method\n\u002F\u002F it reads like this: for any functor f, it takes a function a -> b, then f a, and returns f b\n\u002F\u002F map :: Functor f => (a -> b) -> f a -> f b\n\n\u002F\u002F How to read the notation\n\u002F\u002F a, b, c          type variables: any type, but within a single signature, the same letter always means the same type\n\u002F\u002F a -> b -> c      a curried function: takes a and returns a function b -> c\n\u002F\u002F (a, b) -> c      an uncurried function of two arguments\n\u002F\u002F *                any type\n\u002F\u002F a | b            a value of type a or of type b\n\u002F\u002F () -> a          a function with no arguments\n\u002F\u002F ...args          any number of arguments\n\u002F\u002F [a]              an array of values of type a\n\u002F\u002F Maybe a          a value of type a, or its absence\n\u002F\u002F Functor f =>     a constraint: f must be a functor\n\u002F\u002F f a ~> (a -> b) -> f b    ~> denotes a method: it is called on a value of type f a, e.g. u.map(fn)",[26,28,30,32],{"ru":7,"en":27},"Hindley–Milner notation in a comment above a function explains which argument types the function accepts and what it returns. In JS it helps write more abstract code and maintain it.",{"ru":7,"en":29},"From the code (a, b) => a + b you can't tell whether the function adds numbers or concatenates strings. The signatures add :: (Number, Number) -> Number and concat :: (String, String) -> String tell you.",{"ru":7,"en":31},"a -> b -> c is a curried function: it takes a and returns a function b -> c. For an uncurried function, the argument types are listed in parentheses, separated by commas: (a, b) -> c.",{"ru":7,"en":33},"a, b, c are type variables: any type, but within a single signature the same letter always means the same type. [a] is an array of values of type a, Maybe a is a value of type a or its absence.",[35,44],{"task":36,"starter":38,"solution":40,"expected":42},{"ru":7,"en":37},"Write the functions from the signatures in the starter. between takes a lower bound, an upper bound and a number, and tells whether the number lies between them, inclusive. countWhere takes a predicate and an array and returns how many elements match the predicate. The signature of between shows that between(18)(65) is already a Number -> Boolean predicate.",{"ru":7,"en":39},"\u002F\u002F between :: Number -> Number -> Number -> Boolean\n\u002F\u002F your code\n\n\u002F\u002F countWhere :: (a -> Boolean) -> [a] -> Number\n\u002F\u002F your code\n\nconst ages = [12, 17, 25, 33, 41, 70]\n\n\u002F\u002F isWorkingAge :: Number -> Boolean\nconst isWorkingAge = between(18)(65)\n\nconsole.log(countWhere(isWorkingAge)(ages))\nconsole.log(typeof isWorkingAge)",{"ru":7,"en":41},"\u002F\u002F between :: Number -> Number -> Number -> Boolean\nconst between = (min) => (max) => (x) => x >= min && x \u003C= max\n\n\u002F\u002F countWhere :: (a -> Boolean) -> [a] -> Number\nconst countWhere = (predicate) => (arr) => arr.filter(predicate).length\n\nconst ages = [12, 17, 25, 33, 41, 70]\n\n\u002F\u002F isWorkingAge :: Number -> Boolean\n\u002F\u002F two of the three arguments are passed: what is left is a function Number -> Boolean\nconst isWorkingAge = between(18)(65)\n\nconsole.log(countWhere(isWorkingAge)(ages))\nconsole.log(typeof isWorkingAge)",{"ru":7,"en":43},"3\nfunction",{"task":45,"starter":47,"solution":49,"expected":51},{"ru":7,"en":46},"The signature safeDivide :: Number -> Number -> Maybe Number says that there may be no result. Write safeDivide: it divides a by b and returns S.Just(quotient), or S.Nothing if b is zero. The last line of the starter takes the number out of the Maybe with S.fromMaybe and a fallback of 0, as in the \"Functional programming\" lesson.",{"ru":7,"en":48},"\u002F\u002F safeDivide :: Number -> Number -> Maybe Number\n\u002F\u002F your code\n\nconsole.log(safeDivide(10)(2))\nconsole.log(safeDivide(1)(0))\nconsole.log(S.fromMaybe(0)(safeDivide(1)(0)))",{"ru":7,"en":50},"\u002F\u002F safeDivide :: Number -> Number -> Maybe Number\n\u002F\u002F Maybe Number: a value of type Number, or its absence\nconst safeDivide = (a) => (b) => b === 0 ? S.Nothing : S.Just(a \u002F b)\n\nconsole.log(safeDivide(10)(2))\nconsole.log(safeDivide(1)(0))\nconsole.log(S.fromMaybe(0)(safeDivide(1)(0)))",{"ru":7,"en":52},"Just (5)\nNothing\n0",7,{"slug":55,"title":56},"immutability",{"ru":7,"en":57},"Immutability",{"slug":59,"title":60},"composition",{"ru":7,"en":61},"Function composition",1792684724207]