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
| // Returns a sex at random. | |
| const getRandomSex = () => Math.random() > 0.5 ? 'm' : 'f'; | |
| // Returns a frog of random sex. | |
| const getRandomFrog = () => ({sex: getRandomSex()}); | |
| // Returns random item from a list. | |
| const randomArrayElement = array => array[Math.floor(Math.random() * array.length)]; | |
| /** |
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
| // Returns a frog of random sex. | |
| const getRandomFrog = () => Math.random() > 0.5 ? 'm' : 'f'; | |
| // Returns a random pair of frogs. | |
| // Never returns 'ff'. | |
| const getRandomFrogPair = () => { | |
| const pair = getRandomFrog() + getRandomFrog(); | |
| // If both frogs are female, toss this pair and get a new one. | |
| if (pair === 'ff') { | |
| return getRandomFrogPair(); |
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
| // Returns a frog of random sex. | |
| const getRandomFrog = () => Math.random() > 0.5 ? 'm' : 'f'; | |
| // Returns a random pair of frogs. | |
| const getRandomFrogPair = () => getRandomFrog() + getRandomFrog(); | |
| // Get a million pairs. | |
| const population = [...Array(1000000)].map(getRandomFrogPair); | |
| // Calculate the distribution. |
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
| /** | |
| * Based on http://burakkanber.com/blog/machine-learning-in-js-k-nearest-neighbor-part-1/ | |
| */ | |
| /** | |
| * Data can have an arbitrary number of numeric properties, and must have a type (unless we're | |
| * guessing what the type is). Currently, all properties are equally weighted. | |
| */ | |
| const data = [ | |
| {rooms: 1, area: 350, type: 'apartment'}, |
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
| fib(0, 1). | |
| fib(B, C) :- fib(A, B), C is A + B. | |
| % fib(0, X) -> X = 1. | |
| % fib(1, X) -> X = 1, X = 2. | |
| % fib(144, X) -> X = 233. |