Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

JavaScript with Partial Application

License

NotificationsYou must be signed in to change notification settings

athanclark/frankenscript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Addons

Chunked Application

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.

Trivial Type Checking

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]

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

About

JavaScript with Partial Application

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp