- Notifications
You must be signed in to change notification settings - Fork0
JavaScript with Partial Application
License
athanclark/frankenscript
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
It's ALLLIIIIIVEEEEEEE!
Just kidding, that would be cool, though.
Status:Experimental
FrankenScript is a Partial Application utility for JavaScript.
Let's dissect addition and turn it into a monstrosity. Here is an explicitaddition function,plus:
functionplus(n,m){returnn+m;}
Very standard. Now, in order to mutilate arbitrary functions andturn them into nightmares, we useUhgg.
varfrankenPlus=Ughh(plus);
...which is pronouncedgroan.frankenPlus can make for some pretty abbominable arithmetic! Watch in horroras we attach the dismembered parameters and get our desired solution:
varfrankenPlus1=frankenPlus(1);frankenPlus1(2);➥3frankenPlus1(3);➥4
Now thatfrankenPlus has the strength of a hundred functions, we canreally be amad (computer) scientist! For instance,zipWith:
varset1=[1,2,3,4,5];varset2=[1,2,3,4,5,6];zipWith(frankenPlus,set1,set2);➥[2,4,6,8,10]
or, equivalently withUnderscore's map & element-wise application:
varplusN=_.map(set1,frankenPlus);app(plusN,set2);➥[2,4,6,8,10]
BothzipWith andapp are taken fromHaskell.
Lo-Dash has acurry function that does something similar tothe partial application examples above, but it blends between JavaScript's multiple parametersand chained invocations. Now, I've included the same functionality so we can do fun stuffs likethis:
varplus4=function(a,b,c,d){returna+b+c+d;}varfrankenPlus4=Ughh(plus4);frankenPlus4(1)(2)(3)(4);➥10frankenPlus4(1,2)(3,4);➥10frankenPlus4(1)(2,3,4);➥10
I have an issue calling thiscurry, 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 functoruniversally, because it's not first-class. I'm goingto try and make a propercurry /uncurry that respects the adjoint, but first I need a lambdacalculus.
We now have a very, very trivial method to declaring type signatures for your function. Observe:
varplusT=Ughh.typed("Number -> Number -> Number",function(n,m){returnn+m});plusT;➥{[Function:func] typeSig:'Number -> Number -> Number'}plusT(1);➥{[Function:func] typeSig:'Number -> Number'}plusT(1)(2);➥3plusT(1)("foo");➥[Error:Parameter(s)doesnotmatchtypesignature]
- Hindley-Milner parametric polymorphism type inference
- Incremental (pseudo) type checking and signature declaration / binding
- Fixpoint termination with explicit recursion
- Lazy and Eager evaluation schemes
About
JavaScript with Partial Application
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.