Coder Social home page Coder Social logo

isruslan / learn-generators Goto Github PK

View Code? Open in Web Editor NEW
269.0 9.0 43.0 863 KB

JavaScript ES(6|2015) generators workshopper. Learn in practice. :metal:

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%
workshop nodeschool generators tutorial es6-generators javascript workshopper

learn-generators's Introduction

Learn ES6 Generators

An Intro to ES6 generators via a set of self-guided workshops.

learn-generators

NPM

[![npm version](https://badge.fury.io/js/learn-generators.svg)](http://badge.fury.io/js/learn-generators)

You can ask questions in nodeschool/discussions board: Gitter

Send an anonymous feedback about learn-generators here: google/form.

Install or Update

You need nodejs >= 0.12.x or iojs >= 1.0.x

$ npm install learn-generators -g

Start learning

0. General information

learn-generators is i18n-friendly workshopper, currently you can learn on English or French language. Hit choose language menu item or selet language with -l flag with aviable prefixes: en, fr, ko, ja, es and ru. Type:

$ learn-generators -l

To see available languages and select the language which you like.

You can also get all available commads in help:

$ learn-generators help

1. Select a topic to learn

Once the workshop is installed, run learn-generators to print a menu where you use the arrows ↑↓ (vim jk works too) to select a topic to learn.

$ learn-generators

2. Writing your solution

Once you have selected a topic, the workshop will remember which problem you are working on. Using your preferred editor, simply create a file to write your solution in. Most topics will supply some boilerplate with which to get started. Copy this from the topic description to your solution file.

3. Testing your solution

Use the workshop's run command to point the workshop at your solution file. Your solution will loaded and passed the topic input. This usually won't perform any validation, it will only show the program output.

$ learn-generators run mysolution.js

4. Verifying your solution

Your solution will be verified against the output of the 'official' solution. If all of the output matches, then you have successfully solved the problem!

$ learn-generators verify mysolution.js

Stuck?

Feedback and criticism is welcome, please log your troubles in issues. Or you can send an anonymous feedback here: google/form.

Thanks

You can learn generators due to this people:

NameGitHubTwitter
Ruslan Ismagilov@isRuslan@is_ruslan
Ilia Akhmadullin@i-akhmadullin@i_akhmadullin
Max Ogden@maxogden@maxogden
Christophe Porteneuve@tdd@porteneuve
Julien Fontanet@julien-f@JulienFontanet
Shim@marocchino@marocchino
Kuniyoshi Tone@kunichan2013@kunitone
Nicolás Bevilacqua@nicobevilacqua@NicoBeviIacqua_

Rod Vagg (@rvagg) made this workshopper framework (⌐■_■)

License

MIT © Ruslan Ismagilov

learn-generators's People

Contributors

beyondcompute avatar derek-dchu avatar eush77 avatar finnp avatar giuscri avatar i-akhmadullin avatar isruslan avatar julien-f avatar knholland avatar kunichan2013 avatar marocchino avatar max-mapper avatar nucleartux avatar palerdot avatar tdd avatar trufi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

learn-generators's Issues

Help with "Look Sync. Do Async"

Hi

Thanks for the great tutorials.

As suggested I added some console.log statements to try and understand the program flow in your example.

var fs = require('fs');

function run (generator) {
  console.log('before it');
  var it = generator(go);
  console.log('after it');
  function go (err, result) {
    console.log('before next');
    it.next(result);
    console.log('after next');
  }
  console.log('before go');

  go();
  console.log('after go');

}

run(function* (done) {
  // read `learn-generators` exercises folder
  console.log('before yield');
  var exercises = yield fs.readdir('exercises', done);
  console.log('after yield');

  console.log(exercises); // [ 'look_sync_do_async', ..., 'run_stop_run' ]
});

With the following output:

before it
after it
before go
before next
before yield
after next
after go *
before next
after yield
RESULTS
after next

I am somewhat lacking in my understanding of generators as I can't understand how the program get's back to "before next" after "after go" (see * above). What happens that causes the go function to be called again?

Thanks for you help

Steven

cannot find module ./learn-generators.js

$ npm i -g learn-generators
/home/michael/.nvm/versions/io.js/v1.3.0/bin/learn-generators -> /home/michael/.nvm/versions/io.js/v1.3.0/lib/node_modules/learn-generators/start
[email protected] /home/michael/.nvm/versions/io.js/v1.3.0/lib/node_modules/learn-generators
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

$ learn-generators
module.js:322
throw err;
^
Error: Cannot find module '/home/michael/repos/nodeschool/learn-generators/learn-generators.js'
at Function.Module._resolveFilename (module.js:320:15)
at Function.Module._load (module.js:262:25)
at Function.Module.runMain (module.js:485:10)
at startup (node.js:111:16)
at node.js:799:3

Any tip on the first assignment

Hello,

I try to solve the first assigment with this code :


function *range(from, to) {
      yield  ; 
    }

    for (var r of range(5, 10)) {
        console.log( r );
    }
    // should print: 5, 6, 7, 8, 9, 10

but the only thing I see is undefined. Can anyone give me a tip how to solve this one.

Question : what is wrong here


1.function *flat (arr) {


2.    if (Array.isArray(arr)) {


3.        for (var i = 1 ; i <= arr.length; i++) {


4.            yield *flat(arr[i]); 


5.        }


6.       }


7.    else {


8.        yield arr


9.         }


10.}                                                                                                                                                                                                                                                                                                                                                                                                                                              


11.                                                                                                                                                                                                                                           


12.var A = [1, [2, [3, 4], 5], 6];                                                                                                                                                                                                        


13. 


14.for (var f of flat(A)) {                                                                                                                                                                                                               


15.        console.log( f );


16.}

Windows Compatibility

Everything is in the title 😉 : the current version is fully developed around bash.exe, not available on Windows. Is it possible to make the project shell-agnostic, to make it work on alternative shells or non-Unix system ?

Question: Look sync, make promise

As part of my working solution, I wrote the code:
if (result.done) result.return(result.value);

The official solution is:
if (result.done) return result.value;

Are both acceptable, or should I have not used Generator.prototype.return() in this instance?

Thanks in advance!

Generators and Iterators - wrong task goal

As task says:

Write 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.

So, what I expect to do is:

factorial(5):
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120

But answer is wrong, because what seems to be checked is:

Write a generator function factorial that, given an input number, yielding the intermediate steps of factorial computation.

Should it be corrected in some way?

Using this workshop with io.js

Hi. I have installed io.js 1.1.0 and when i tried to execute $ learn-generators i got an error:

You need nodejs >= 0.11.x or iojs >= 1.0.x

io.js was installed globally.

Help with Look Sync, Make Promise

Hi @isRuslan

You were so helpful with my last query that I have another quick on for you,

Here is my code (it works):

function getFoo () {
  return new Promise(function (resolve, reject){
    resolve('foo');
  });
}

function run (generator) {
  var it = generator(); //create itterator to control generator

  function go(result) { //result is what is yielded by the generator following it.next(), firt time around it is a new Promise

    if (result.done) return result.value; //returns undefined
    //if not done, the promise is pending

    console.log(result);                //{ value: Promise { 'foo' }, done: false }
    console.log(result.value);          //Promise { 'foo' }
    console.log(result.value.then());   //Promise { <pending> }

    result.value.then(fufilled,rejected); //return???

    function fufilled(value) {
      go(it.next(value)); //pass the resolved value back to the run function  - need to return???
    }

    function rejected(error) {
      go(it.throw(error));   //return???
    }

  }
  go(it.next()); //start generator
}

run(function* () {
  try {
    var foo = yield getFoo();
    console.log(foo);
  } catch (e) {
    console.log('Error: ' + e);
  }
});

I got stuck for a very long time dealing with the resultparameter. I thought result was a Promise returned by getFoo() but result.then() did not work.

Only through some logging

    console.log(result);                //{ value: Promise { 'foo' }, done: false }
    console.log(result.value);          //Promise { 'foo' }
    console.log(result.value.then());   //Promise { <pending> }

Did I see that I needed to do result.value to access the Promise.
Can you explain why this is as nowhere in the documentation can I find any references to Promise.value

For example

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

creates a new promise p1 and then p1.then() works as I would expect.

The last thing is why are returns required here in your solution:

    return result.value.then(function (value) {
      return go(it.next(value));
    }, function (error) {
      return go(it.throw(error));
    });

I didn't put them in and it works fine, am I asking for trouble ;-)

Thanks so much again for everything!

Solution for "Catch Error!" not working with forEach

it's probably a noob question, but why does this solution not work for the "catch error" excercise

function *upper (items) {
  items.forEach(function(item) {
    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);
}

Exercise 3 is confusing

The entire description of delegating generators:

We can delegate iteration control from our generator to another one.
yield * expression will do the trick, * means that expression
is a generator too, so we can send message to it.

It doesn't explain what "delegate iteration control" means or what I should expect to happen when we "send message" to another generator.

Cannot read property 'has' of undefined on verify

full terminal output below. I installed using npm install learn-generators on node 0.11.15. learn-generators run 01.js works as expected, but verify fails.

~/Desktop/learn-generators 🐈  nave use 0.11.x
What version of node?
stable, latest, x.y, or x.y.z > latest
Creating new env named '0.11.x' using node 0.11.15
######################################################################## 100.0%
installed from binary
~/Desktop/learn-generators 🐈  learn-generators run 01.js 
5
6
7
8
9
~/Desktop/learn-generators 🐈  learn-generators verify 01.js 
/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/exercise.js:79
      return i18n.has(lookup) ? i18n.raw(lookup) :
                 ^
TypeError: Cannot read property 'has' of undefined
    at Object.i18n.i18n.get (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/exercise.js:79:18)
    at Object.raw (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/i18n-core/index.js:49:21)
    at Object.defaultTranslation [as translate] (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/i18n-core/index.js:28:17)
    at Object.__ (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/i18n-core/lib/createTranslator.js:34:15)
    at Exercise.transform (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/comparestdout.js:53:37)
    at Transform._read (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at Transform._write (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at Transform.Writable.write (/usr/local/lib/node_modules/learn-generators/node_modules/workshopper-exercise/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)
~/Desktop/learn-generators 🐈  cat 01
cat: 01: No such file or directory
~/Desktop/learn-generators 🐈  cat 01.js
function *range(from, to) {
  for (var i = from; i < to; i++) yield(i)
}

for (var r of range(5, 10)) {
   console.log( r );
}
 // 5, 6, 7, 8, 9, 10

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.