Giter Club home page Giter Club logo

reblaze's Introduction

          _     _
         | |   | |
 _ __ ___| |__ | | __ _ _______
| '__/ _ \ '_ \| |/ _` |_  / _ \
| | |  __/ |_) | | (_| |/ /  __/
|_|  \___|_.__/|_|\__,_/___\___|

A crazy way of template-izing your JS Functions and Generators so they can be generated on the fly.

This should be used for methods that are instantiated once then used many times, since currently the function generation does take quite a bit of time to do.

API

reblaze(values[, name], fn)

  • values {Object}
  • name {String} Default ''
  • fn {Function}
  • Returns new Function

Examples

Let's show a simple example then allow you to go experiment.

'use strict';

const reblaze = require('reblaze');

function Matrix(rows, cols) {
  this._rows = rows >>> 0;
  this._cols = cols >>> 0;
  // Replace template entries with fields in passed object.
  this.sumCols = reblaze({ COLS: this._cols, ROWS: this._rows }, sumCols);

  this._data = new Uint32Array(this._rows * this._cols);
}

// This will look funky due to the template convention.
function sumCols() {
  const data = this._data;
  // Sum of values may exceed a uint32
  const sum = new Float64Array(((COLS)));
  for (var i = 0; i < ((COLS)) * ((ROWS)); i++) {
    sum[i % ((COLS))] += data[i];
  }
  return sum;
}

// Build a new Matrix instance with custom sumCols().
const m = new Matrix(11, 7);

Now sumCols on the instance m:

function sumCols() {
  const data = this._data;
  const sum = new Float64Array(7);
  for (var i = 0; i < 7 * 11; i++) {
    sum[i % 7] += this[i];
  }
  return sum;
}

It will also automatically take care of object properties as well. For example:

function runMe(foo) {
  return foo[((BAR))];
}

const fn = reblaze({ BAR: 'bar' }, runMe);

And the output for the new fn instance:

function runMe(foo) {
  return foo.bar;
}

The name of the function can also be replaced by passing a second optional argument.

function replaceMe(foo) {
  return foo[((BAR))];
}

const fn = reblaze({ BAR: 'bar' }, 'wereReplaced', replaceMe);

Will generate the following function:

function wereReplaced(foo) {
  return foo.bar;
}

If you want to have the full require, __filename, etc. in your function pass module as an optional third (or second if name isn't provided) argument.

function foop() {
  require(__dirname + '/' + ((NAME)));
}
reblaze({ NAME: 'mod_name' }, module, foop);

So, give it a whirl and report any bugs. In the future I plan on implementing even more meta-ness where reblaze can run on itself and return optimized instances for a given construction case.

reblaze's People

Contributors

trevnorris 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

Watchers

 avatar  avatar  avatar  avatar

Forkers

bradparks

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.