Giter VIP home page Giter VIP logo

degenerate's Introduction

degenerate

Build Status

Browser support

Library lets you create ES6 generators with in the (dis)comfort of ES5 JS environment (like < [email protected]) and without syntax sugar. This magic happens at the cost of the syntax transformations at the generator instantiation. You can use this with task.js, co, suspend or any other Generator-based flow control based library without waiting on your favorite JS environment to enable generators for you!

API

Given a generators support in your environment you should be able to write code like this:

var range = function*(from, to, step) {
  step = step || 1
  var n = from

  while (n <= to) {
    yield n
    n ++
  }

  return n
}

var digits = range(0, 9)
var step
while (step = digits.next(), !step.done) {
  console.log(step.value)
}

Unfortunately above code will throw a syntax error in engines that don't have generators support.

With a degenerate you can create equivalent range generator by typeing this:

var Generator = require("degenerate")
var range = new Generator(function(from, to, step) {
  step = step || 1
  var n = from

  while (n <= to) {
    yield(n)
    n ++
  }

  return n
})

var digits = range(0, 9)
var step
while (step = digits.next(), !step.done) {
  console.log(step.value)
}

Notice the difference ? Just loose a * and replace yield n statement with yield(n) call expression.

You can also use delegeting yields:

var concat = new Generator(function(xs, ys) {
  yield* xs
  yield* ys
})

var zs = concat(range(1, 3), range(4, 7))
while (step = zs.next(), !step.done) {
  console.log(step.value)
}

Limitations

yield isn't a function

Don't try to outsmart this tool, even though yield may feel like a first class function, it is not and any attempts to use it as such will break the code.

strict mode is strict about yield

If you try to define function that refers to yield in strict mode, you'll get a syntax error :( To overcome this limitation all you need to do is write this.yield instead:

"use strict";

var range = Generator(function(from, to) {
  while (from <= to)
    this.yield(from++)
})

var concat = new Generator(function(xs, ys) {
  this.yield* xs
  this.yield* ys
})

Enclosed variables are lost

This is the most unfortunate limitation of all! You can not enclose any scope variables, all that generator body will have has access to is a top scope or whatever has being passed into as this pseudo variable or as arguments.

What's the trick ?

Generator function parses source of the given function, transforms AST to make function expression a generator, replace yield(x), this.yield(x) with yield x. Transforms new AST via facebook/regenerator (Thank them for making it possible) and finally generates a generator from the results.

Install

npm install degenerate

degenerate's People

Contributors

gozala avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.