Giter Site home page Giter Site logo

jstip's Introduction

JavaScript: The Irreducible Parts

https://github.com/rsp/jstip (readme)

This is a try to find a minimal, irreducible set of JavaScript features that can be used to write useful programs.

This is not an attempt to produce anything even remotely practical. If any feature of the language can be avoided ad reimplemented using a smaller set of features making programs run million times slower, it will be.

Syntax

In the examples below I'll be using the ES6 fat arrow function syntax.

So instead of:

f = function (x) { return x+5; };

i will use:

f = x => x+5;

Those two functions are the same for our needs (they differ with the scope of this and arguments which we will not use). So every time you see x=>2*x just think of function(x){return 2*x;} only written in a much shorter way.

Also this:

f = a => b => a+b;

is a shorthand of this:

f = function (a) { return function (b) { return a+b; }; };

I don't have to tell you that writing functions such as:

f = a => b => c => d => a(b(c))(d);

is much more convenient with the fat arrow syntax. (Yes, we will use that function.)

The question

The question is - which parts of the language are there only for convenience and can be reduced to other constructs, and which parts are fundamentally irreducible.

We will try to go through the most important parts of the language and ask ourselves if we need it or not, trying to achieve the same effect avoiding the construct.

Do we need var?

Variables seem to be a pretty fundamental feature of the language but it turns out that we don't need them. As long as we have function calls then function parameters can serve the role of our variables.

With var:

var a = 17;
console.log(a);

Without var:

(b => {
    console.log(b);
})(17);

Using multiple variables - with var:

var a = 17, b = 3;
console.log( a + b );

Without var:

((c, d) => {
    console.log( c + d );
})(17, 3);

We can get rid of var.

Multi-parameter functions

In the previous example we used a function of multiple parameters. Do we really need such functions? No. Every time we need a function of two parameters we can use a function of one parameter that returns another function of one parameter.

With multiple parameters:

var a = (x, y) => x+y;
console.log( a(2, 3) );

Without multiple parameters:

var b = x => y => x+y;
console.log( b(2)(3) );

We can change our var example even further:

With var:

var a = 17, b = 3;
console.log( a + b );

Without var and without multiple parameters:

(e => f => {
    console.log( e + f );
})(17)(3);

Such functions have some interesting properties - more about this later.

...

This is work in progress - more to come.

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski - https://github.com/rsp

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.