From 69d5dc87bf1c6eb54fd6f55b0b29b5ce0ff07499 Mon Sep 17 00:00:00 2001 From: Boris Marinov Date: Wed, 23 Mar 2016 17:14:35 +0200 Subject: [PATCH 1/2] Add definitions: Value, Constant, Composition --- readme.md | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index c2b302a..d648a91 100644 --- a/readme.md +++ b/readme.md @@ -70,6 +70,21 @@ let sum = (a, b) => a + b; let curriedSum = (a) => (b) => a + b; curriedSum(40)(2) // 42. +``` +--- + +## Composition + +> A function which combines two values of a given type (usually also some kind of functions) to a third value of the same type. + +The most straightforward type of composition, is called "normal function composition". +It allows you to combines functions which accept and return a single value. + +```js +const compose = (f, g) => a => f(g(a)) // Definition +const floorAndToString = compose((val)=> val.toString(), Math.floor)(222.44) //Usage +floorAndToString(121.212121) // "121" + ``` --- @@ -77,7 +92,7 @@ curriedSum(40)(2) // 42. ## Purity > A function is said to be pure if the return value is only determined by its -input values, without any side effects. +input values, without any side effects and mutations. ```js let greet = "yo"; @@ -157,6 +172,37 @@ Points-free function definitions look just like normal assignments without `func --- +## Value + +> Any complex or primitive value that is used in the computation, including functions. Values in functional programming are assumed to be immutable. + +```js +5 +Object.freeze({name: 'John', age: 30}) // The `freeze` function enforces immutability. +(a) => a +``` +Note that the value-containing structures defined below such as [Functor](#functor), [Monad](#monad) etc. are themselves values. This means, among other things, that they can be nested within each other. + +--- + +## Constant + +> An immutable reference to a value. Not to be confused with `Variable` - a reference to a value which can at any point be updated to point to a different value. +```js +const five = 5 +const john = {name: 'John', age: 30} +``` +Constants are referentially transparent. That is, they can be replaced with the values that they represent without affecting the result. +In other words with the above two constants the expression: + +```js +john.age + five === ({name: 'John', age: 30}).age + (5) + +``` +Should always return `true`. + +--- + ## Functor > An object with a `map` function that adhere to certains rules. `Map` runs a function on values in an object and returns a new object. @@ -296,6 +342,8 @@ The identity value is empty array `[]` ```js [1, 2].concat([]); // [1, 2] ``` +Functions also form a monoid with the normal functional compositon as an operation and the function which returns its input `(a) => a` + --- From 33d40dc24d21216cddfa16a04a6c6c24745b04a2 Mon Sep 17 00:00:00 2001 From: Boris Marinov Date: Fri, 25 Mar 2016 11:31:04 +0200 Subject: [PATCH 2/2] fixes --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index d648a91..8accb4b 100644 --- a/readme.md +++ b/readme.md @@ -75,14 +75,14 @@ curriedSum(40)(2) // 42. ## Composition -> A function which combines two values of a given type (usually also some kind of functions) to a third value of the same type. +> A function which combines two values of a given type (usually also some kind of functions) into a third value of the same type. -The most straightforward type of composition, is called "normal function composition". -It allows you to combines functions which accept and return a single value. +The most straightforward type of composition is called "normal function composition". +It allows you to combines functions that accept and return a single value. ```js const compose = (f, g) => a => f(g(a)) // Definition -const floorAndToString = compose((val)=> val.toString(), Math.floor)(222.44) //Usage +const floorAndToString = compose((val)=> val.toString(), Math.floor) //Usage floorAndToString(121.212121) // "121" ``` @@ -92,7 +92,7 @@ floorAndToString(121.212121) // "121" ## Purity > A function is said to be pure if the return value is only determined by its -input values, without any side effects and mutations. +input values, without any side effects. ```js let greet = "yo"; @@ -181,7 +181,7 @@ Points-free function definitions look just like normal assignments without `func Object.freeze({name: 'John', age: 30}) // The `freeze` function enforces immutability. (a) => a ``` -Note that the value-containing structures defined below such as [Functor](#functor), [Monad](#monad) etc. are themselves values. This means, among other things, that they can be nested within each other. +Note that value-containing structures such as [Functor](#functor), [Monad](#monad) etc. are themselves values. This means, among other things, that they can be nested within each other. ---