Skip to content

Instantly share code, notes, and snippets.

@stukennedy
Created May 23, 2017 22:39
Show Gist options
  • Save stukennedy/78d65935e783c0e65ebdd531c0df1430 to your computer and use it in GitHub Desktop.
Save stukennedy/78d65935e783c0e65ebdd531c0df1430 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/tojadivupo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>
</head>
<body>
<script id="jsbin-javascript">
// jshint esnext: true
'use strict';
var P = 5000;
var G = 20;
var population = _.flow(_.range(0), _.map(function (x) {
return 'B';
}), _.set([0], 'A'))(P);
var pick = _.random(0, 1);
var generation = _.flow(_.shuffle, _.chunk(2), _.map(function (parents) {
return [parents[pick], parents[pick]];
}), _.flatten);
var mutantCount = _.sumBy(_.isEqual('A'));
console.log('0: ' + mutantCount(population));
_.reduce(function (prev, step) {
var next = generation(prev);
console.log(step + ': ' + mutantCount(next));
return next;
}, population, _.range(1, G + 1));
</script>
<script id="jsbin-source-javascript" type="text/javascript">// jshint esnext: true
const P = 5000
const G = 20
const population = _.flow(
_.range(0),
_.map(x => 'B'),
_.set([0], 'A')
)(P)
const pick = _.random(0, 1)
const generation = _.flow(
_.shuffle,
_.chunk(2),
_.map(parents => ([ parents[pick], parents[pick] ])),
_.flatten
)
const mutantCount = _.sumBy(_.isEqual('A'))
console.log('0: ' + mutantCount(population))
_.reduce((prev, step) => {
const next = generation(prev)
console.log(step + ': ' + mutantCount(next))
return next
}, population, _.range(1, G + 1))
</script></body>
</html>
// jshint esnext: true
'use strict';
var P = 5000;
var G = 20;
var population = _.flow(_.range(0), _.map(function (x) {
return 'B';
}), _.set([0], 'A'))(P);
var pick = _.random(0, 1);
var generation = _.flow(_.shuffle, _.chunk(2), _.map(function (parents) {
return [parents[pick], parents[pick]];
}), _.flatten);
var mutantCount = _.sumBy(_.isEqual('A'));
console.log('0: ' + mutantCount(population));
_.reduce(function (prev, step) {
var next = generation(prev);
console.log(step + ': ' + mutantCount(next));
return next;
}, population, _.range(1, G + 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment