Git Product home page Git Product logo

frankenscript's Introduction

FrankenScript

It's ALLLIIIIIVEEEEEEE!

Just kidding, that would be cool, though.

Status: Experimental


Introduction

FrankenScript is a Partial Application utility for JavaScript.

Usage

Let's dissect addition and turn it into a monstrosity. Here is an explicit addition function, plus:

function plus(n,m) {
  return n+m;
}

Very standard. Now, in order to mutilate arbitrary functions and turn them into nightmares, we use Uhgg.

var frankenPlus = Ughh(plus);

...which is pronounced groan. frankenPlus can make for some pretty abbominable arithmetic! Watch in horror as we attach the dismembered parameters and get our desired solution:

var frankenPlus1 = frankenPlus(1);

frankenPlus1(2);
 3

frankenPlus1(3);
 4

Now that frankenPlus has the strength of a hundred functions, we can really be a mad (computer) scientist! For instance, zipWith:

var set1 = [1,2,3,4,5];
var set2 = [1,2,3,4,5,6];

zipWith(frankenPlus, set1, set2);
 [ 2, 4, 6, 8, 10 ]

or, equivalently with Underscore's map & element-wise application:

var plusN = _.map(set1, frankenPlus);

app(plusN, set2);
 [ 2, 4, 6, 8, 10 ]

Both zipWith and app are taken from Haskell.

Addons

Chunked Application

Lo-Dash has a curry function that does something similar to the partial application examples above, but it blends between JavaScript's multiple parameters and chained invocations. Now, I've included the same functionality so we can do fun stuffs like this:

var plus4 = function (a,b,c,d){
  return a+b+c+d;
}
var frankenPlus4 = Ughh(plus4);

frankenPlus4(1)(2)(3)(4);
 10

frankenPlus4(1,2)(3,4);
 10

frankenPlus4(1)(2,3,4);
 10

I have an issue calling this curry, because JavaScript's native multi-parameter function application (,) is not a tuple or product functor - it's not even a value, however it might be unique. Regardless, you can't say it's the product functor universally, because it's not first-class. I'm going to try and make a proper curry / uncurry that respects the adjoint, but first I need a lambda calculus.

Trivial Type Checking

We now have a very, very trivial method to declaring type signatures for your function. Observe:

var plusT = Ughh.typed("Number -> Number -> Number", function (n,m) {return n+m});

plusT;
 { [Function: func] typeSig: 'Number -> Number -> Number' }

plusT(1);
 { [Function: func] typeSig: 'Number -> Number' }

plusT(1)(2);
 3

plusT(1)("foo");
 [Error: Parameter(s) does not match type signature]

TODO

  • Hindley-Milner parametric polymorphism type inference
  • Incremental (pseudo) type checking and signature declaration / binding
  • Fixpoint termination with explicit recursion
  • Lazy and Eager evaluation schemes

frankenscript's People

Contributors

athanclark avatar

Stargazers

 avatar

Watchers

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