Skip to content

Instantly share code, notes, and snippets.

@Anna-Myzukina
Last active September 19, 2018 22:39
Show Gist options
  • Save Anna-Myzukina/4d9a9462d4ce66c2a70f681f31e7c6c3 to your computer and use it in GitHub Desktop.
Save Anna-Myzukina/4d9a9462d4ce66c2a70f681f31e7c6c3 to your computer and use it in GitHub Desktop.
learn-generators
//Exercise 1
/*Write a range generator-function that takes from and to arguments.
Print the numbers as strings within the specified range, one per line.
Follow this boilerplate:
*/
function *range(from, to) {
// your code goes here
for(var i = from; i <= to; i++) {
yield i;
}
}
for (var r of range(5, 10)) {
console.log( r );
}
// should print: 5, 6, 7, 8, 9, 10
//Exercise 2
/*fWrite a generator function factorial that, given an input number, starts at 1 and goes
up to the number, yielding the factorial of each number along the way.
Don't use recursion. Use a loop.
Follow this boilerplate:
*/
function *factorial (n) {
var result = 1;
for (var i = 1; i <= n; i++) {
result *= i;
yield result;
}
}
for (var n of factorial(5)) {
console.log(n)
}
//Exercise 3
/*Task
Write generator-function flat that takes nested array inside and flattens it.
Follow this boilerplate:
*/
function *flat (arr) {
if (Array.isArray(arr)) {
for (var i = 0; i < arr.length; i++) {
yield* flat(arr[i]);
}
} else {
yield arr;
}
}
var A = [1, [2, [3, 4], 5], 6];
for (var f of flat(A)) {
console.log( f );
}
// 1 2 3 4 5 6
//Exercise 4
/*Write a generator-function upper that takes an array of strings and
yields each of them in upper case.
The problem: some person sent a number into the upper and it's broken.
Please fix it. If upper gets a number it should return null.
Follow this boilerplate
*/
function* upper(items) {
// your code goes here
for (let item of items) {
try {
yield item.toUpperCase()
} catch (e) {
yield null
}
}
}
var bad_items = ['a', 'B', 1, 'c'];
for (var item of upper(bad_items)) {
console.log(item);
}
// want to log: A, B, null, C
//Exercise 5
/*Add error handling to this boilerplate. Teach run to throw an exception and
catch that in generator. firstFile should be null if it doesn't exist.
Follow this boilerplate:
*/
var fs = require("fs");
function run(generator) {
var it = generator(go)
function go(err, result) {
if (err) return it.throw(null)
it.next(result)
}
go()
// improve `run` above
}
run(function* (done) {
// catch exception
try {
var dirFiles = yield fs.readdir("NoNoNoNo", done); // No such dir
} catch (e) {
console.log(e)
}
var firstFile = dirFiles[0]; // TypeError: Cannot read property '0' of undefined
console.log(firstFile);
});
//Exercise 6
/*
Write run function that gets a generator as an argument, starts it and
yields promise value foo. Use run from callback exercise as a example.
Follow this boilerplate:
*/
function getFoo () {
return new Promise(function (resolve, reject){
resolve('foo');
});
}
function run (generator) {
var it = generator();
function go(result) {
// take a look also on `Generator.prototype.return`
if (result.done) return result.value;
return result.value.then(function (value) {
return go(it.next(value));
}, function (error) {
return go(it.throw(error));
});
}
go(it.next());
}
run(function* () {
try {
var foo = yield getFoo();
console.log(foo);
} catch (e) {
console.log(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment