This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let heightRequirement = 46; | |
| function canRide(height) { | |
| return height >= heightRequirement; | |
| } | |
| // Every half second, set heightRequirement to a random number between 0 and 200. | |
| setInterval(() => heightRequirement = Math.floor(Math.random() * 201), 500); | |
| const mySonsHeight = 47; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let heightRequirement = 46; | |
| // Impure because it relies on a mutable (reassignable) variable. | |
| function canRide(height) { | |
| return height >= heightRequirement; | |
| } | |
| // Impure because it causes a side-effect by logging to the console. | |
| function multiply(a, b) { | |
| console.log('Arguments: ', a, b); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function multiply(a, b) { | |
| return a * b; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function giveMe3(item1, item2, item3) { | |
| return ` | |
| 1: ${item1} | |
| 2: ${item2} | |
| 3: ${item3} | |
| `; | |
| } | |
| const giveMe2 = giveMe3.bind(null, 'rock'); | |
| const giveMe1 = giveMe2.bind(null, 'paper'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const giveMe3 = R.curry(function(item1, item2, item3) { | |
| return ` | |
| 1: ${item1} | |
| 2: ${item2} | |
| 3: ${item3} | |
| `; | |
| }); | |
| const giveMe2 = giveMe3(R.__, R.__, 'French Hens'); // Specify the third argument. | |
| const giveMe1 = giveMe2('Partridge in a Pear Tree'); // This will go in the first slot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function dot(vector1, vector2) { | |
| return vector1.reduce((sum, element, index) => sum += element * vector2[index], 0); | |
| } | |
| const v1 = [1, 3, -5]; | |
| const v2 = [4, -2, -1]; | |
| // Use Ramda to do the currying for us! | |
| const curriedDot = R.curry(dot); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function curriedDot(vector1) { | |
| return function(vector2) { | |
| return vector1.reduce((sum, element, index) => sum += element * vector2[index], 0); | |
| } | |
| } | |
| // Taking the dot product of any vector with [1, 1, 1] | |
| // is equivalent to summing up the elements of the other vector. | |
| const sumElements = curriedDot([1, 1, 1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function dot(vector1, vector2) { | |
| return vector1.reduce((sum, element, index) => sum += element * vector2[index], 0); | |
| } | |
| const v1 = [1, 3, -5]; | |
| const v2 = [4, -2, -1]; | |
| console.log(dot(v1, v2)); // 1(4) + 3(-2) + (-5)(-1) = 4 - 6 + 5 = 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const vehicles = [ | |
| { make: 'Honda', model: 'CR-V', type: 'suv', price: 24045 }, | |
| { make: 'Honda', model: 'Accord', type: 'sedan', price: 22455 }, | |
| { make: 'Mazda', model: 'Mazda 6', type: 'sedan', price: 24195 }, | |
| { make: 'Mazda', model: 'CX-9', type: 'suv', price: 31520 }, | |
| { make: 'Toyota', model: '4Runner', type: 'suv', price: 34210 }, | |
| { make: 'Toyota', model: 'Sequoia', type: 'suv', price: 45560 }, | |
| { make: 'Toyota', model: 'Tacoma', type: 'truck', price: 24320 }, | |
| { make: 'Ford', model: 'F-150', type: 'truck', price: 27110 }, | |
| { make: 'Ford', model: 'Fusion', type: 'sedan', price: 22120 }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const vehicles = [ | |
| { make: 'Honda', model: 'CR-V', type: 'suv', price: 24045 }, | |
| { make: 'Honda', model: 'Accord', type: 'sedan', price: 22455 }, | |
| { make: 'Mazda', model: 'Mazda 6', type: 'sedan', price: 24195 }, | |
| { make: 'Mazda', model: 'CX-9', type: 'suv', price: 31520 }, | |
| { make: 'Toyota', model: '4Runner', type: 'suv', price: 34210 }, | |
| { make: 'Toyota', model: 'Sequoia', type: 'suv', price: 45560 }, | |
| { make: 'Toyota', model: 'Tacoma', type: 'truck', price: 24320 }, | |
| { make: 'Ford', model: 'F-150', type: 'truck', price: 27110 }, | |
| { make: 'Ford', model: 'Fusion', type: 'sedan', price: 22120 }, |