Skip to content

Instantly share code, notes, and snippets.

@DKunin
Last active June 29, 2017 08:15
Show Gist options
  • Save DKunin/ac07290eafac41fec1270ce18a6225a6 to your computer and use it in GitHub Desktop.
Save DKunin/ac07290eafac41fec1270ce18a6225a6 to your computer and use it in GitHub Desktop.
Generator Snippets and examples
'use strict';
var stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
let sentence = '';
let sentenceTimeout;
function* personSimulator(words) {
yield 'Hi!';
while (true) {
if (sentence.search(/bye/) !== -1) {
break;
}
yield 'Wat?';
}
return 'bye';
}
const simulation = personSimulator();
stdin.on('data', function(key) {
if (key === '\u0003') {
process.exit();
}
clearTimeout(sentenceTimeout);
sentenceTimeout = setTimeout(() => {
process.stdout.write('\u001b[2J\u001b[0;0H');
const response = simulation.next().value;
console.log(response);
if (response.includes('bye')) {
process.exit();
};
sentence = '';
}, 1000);
sentence += key;
process.stdout.write(key);
});
'use strict';
function asynchronously(generator) {
var g = generator();
(function go(err, result) {
var step;
if (err) {
step = g.throw(err);
} else {
step = g.next(result);
}
if (!step.done) {
var promise = step.value;
promise
.then(function(resolvedValue) {
go(null, resolvedValue);
})
.catch(function(e) {
go(e);
});
}
})();
}
function getStatus(condition = '') {
return new Promise((resolve, reject) => {
resolve(`${condition} stuff`);
});
}
function* processGenerator(options) {
let content = yield getStatus();
console.log(content);
content = yield getStatus('other ');
console.log(content);
return 'done';
}
asynchronously(processGenerator);
'use strict';
const shapeTypes = [
'O',
'L',
'J',
'I',
'S',
'Z',
'T'
];
const rotations = [
0, 90, 180, 270
]
class SingleShape {
constructor({ type = 'O', rotation = 0 }) {
this.type = type;
this.rotation = rotation;
}
status() {
const { type, rotation } = this;
return {
type,
rotation
};
}
rotateClockWise() {
this.rotation+=90;
if (this.rotation > 270) {
this.rotation = 0;
}
}
rotateCounterClockWise() {
this.rotation-=90;
if (this.rotation < -270) {
this.rotation = 0;
}
}
}
class shapeFactory {
*getShape() {
while (true) {
const shapeType = shapeTypes[Math.floor(Math.random() * shapeTypes.length)];
const rotation = rotations[Math.floor(Math.random() * rotations.length)];
yield new SingleShape({ type: shapeType, rotation });
}
}
}
const factory = new shapeFactory();
const game = factory.getShape();
const field = Array.from({ length: 10 }, () => game.next());
console.log(field);
'use strict';
const names = [
'Cannibal',
'Shambler',
'Melter',
'Hunter',
'Pincher',
'Stinger',
'Crier',
'Stinker',
'Creeper',
'Crawler'
];
const conditions = ['fresh', 'slightly-decayed', 'decayed', 'horribly-decayed'];
class Zombie {
constructor(name, condition) {
this.name = name;
this.condition = condition;
}
sayHi() {
return 'BRAAAAAAAINS';
}
showCondition() {
return `This is ${this.name}, he is ${this.condition}`;
}
}
class ZombieFactory {
*getHorde() {
while (true) {
const name = names[Math.floor(Math.random() * names.length)];
const condition =
conditions[Math.floor(Math.random() * conditions.length)];
yield new Zombie(name, condition);
}
}
}
const factory = new ZombieFactory();
const horde = factory.getHorde();
const herd = Array.from({ length: 300 }, () => horde.next().value);
for (let i = 0; i < herd.length; i++) {
console.log(herd[i].showCondition());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment