From ff29eb4abde5fc978a401eacc30a0442b7efa56c Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Sun, 25 Sep 2016 12:17:37 -0700 Subject: [PATCH 1/2] Update the Purity example code Reading through this, the example code for the Purity information was a bit confusing. The original example tried to show both using and changing global state in one function, but because of that the "pure" example and "unpure" example didn't actually do the same thing. I think it's a little more clear if the two concepts are broken up, instead showing the "pure" version, one that uses global state, and one that modifies global state. --- readme.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 7a28a12..7b91f4a 100644 --- a/readme.md +++ b/readme.md @@ -182,23 +182,35 @@ input values, and does not produce side effects. const greet = (name) => 'Hi, ' + name greet('Brianne') // 'Hi, Brianne' - ``` -As opposed to: +As opposed to each of the following: ```js - -let greeting +window.name = 'Brianne' const greet = () => { - greeting = 'Hi, ' + window.name + return 'Hi, ' + window.name } greet() // "Hi, Brianne" - ``` +The above example's output is based on data stored outside of the function... + +```js +let greeting + +const greet = (name) => { + greeting = 'Hi, ' + name +} + +greet() +greeting // "Hi, Brianne" +``` + +... and this one modifies state outside of the function. + ## Side effects A function or expression is said to have a side effect if apart from returning a value, it interacts with (reads from or writes to) external mutable state. From f55916cac09caa85b8ed685edad27bc0716b9d25 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Tue, 25 Oct 2016 18:22:00 -0700 Subject: [PATCH 2/2] Update Purity code examples to use interpolation Also, add in the missing argument to `greet` in the changing-global-state example code. --- readme.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 7b91f4a..dc4ecdc 100644 --- a/readme.md +++ b/readme.md @@ -179,7 +179,7 @@ A function is pure if the return value is only determined by its input values, and does not produce side effects. ```js -const greet = (name) => 'Hi, ' + name +const greet = (name) => `Hi, ${name}` greet('Brianne') // 'Hi, Brianne' ``` @@ -189,9 +189,7 @@ As opposed to each of the following: ```js window.name = 'Brianne' -const greet = () => { - return 'Hi, ' + window.name -} +const greet = () => `Hi, ${window.name}` greet() // "Hi, Brianne" ``` @@ -202,10 +200,10 @@ The above example's output is based on data stored outside of the function... let greeting const greet = (name) => { - greeting = 'Hi, ' + name + greeting = `Hi, ${name}` } -greet() +greet('Brianne') greeting // "Hi, Brianne" ```