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

Haskell-esque automatic partial application, without runtime.

NotificationsYou must be signed in to change notification settings

VSADX/postoffice.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 

Repository files navigation

PostOffice helps you createmore reusable functions.

  • Powerful: PostOffice lets you create simple functions, then bind any of the parameters you choose.Like.bind in JavaScript, binding a parameter creates a copy of your function. This way, you can bindparameters to the same function many times creating new functions (like Dependency Injection or PartialApplication).
  • Simple: You don't need to write in a particular style! PostOffice exports just one function. If youpass in a function topostoffice(), you get a new function that support arbitrary parameter binding.
  • Tiny: The principles behind this library are so simple, you could do it yourself. However,postoffice.jsis only 22 lines of code, so feel free to check out how it works.

import{postoffice,N}from"postoffice.js"constsubtract=postoffice((first,second)=>first-second)// `subtract` takes two parameters, but we only fill in the `first`consttake_from_100=subtract(100)console.log(take_from_100(70))// 30console.log(take_from_100(127))// -27

constsubtract=postoffice((first,second)=>first-second)// `subtract` takes two parameters, but we only supply the `second`// the const `N` tells our function that we don't want to place a value in it yetconsttake_away_20=subtract(N,20)console.log(take_away_20(80))// 60console.log(take_away_20(13))// -7

What's PostOffice doing?

Like a real postoffice, when you fill-out an envelope but forget some of the information, you get your letter back.You also get instructions on what is missing. Once you add all the missing information, the postoffice delivers yourmail. This is not how most programming languages work. If you try to run a function, but don't supply all the required parameters,expect an exception or unexpected behavior.

What does this library do? When you run a postoffice function, you don't have to supply all the required parameters.The function will bind all the parameters you added, but return a new function that takes the missing parameters.When you supply all the missing parameters, the function promptly executes.

This is more than a debugging tool. This library is designed to give you more freedom as a function author or API user.How so? The next code example shows how you can useN to add placeholders for any parameters of a function. Also,PostOffice can create multiple functions from just one original function. Each copied function can have differentparameters bound, or the same one bound but using a different value.


constsell_food=postoffice((item,price,count)=>`${item}: $${price*count}`)constsell_pizza=sell_food("Pizza",12.50,N)constsell_drink=sell_food(N,1.20,1)//////sell_pizza(3)// "Pizza: $37.50"sell_drink("Sprite")// "Sprite: $1.20"sell_drink("Pepsi")// "Pepsi: $1.20"

FizzBuzz example

constmultiple_of=postoffice((num,multiple,message)=>num%multiple===0 ?message :false)constby3=multiple_of(N,3,"Fizz")constby5=multiple_of(N,5,"Buzz")constby3x5=multiple_of(N,3*5,"Fizz Buzz")constall_funcs=[by3x5,by5,by3]for(leti=1;i<20;i++){console.log(all_funcs.map(func=>func(i)).find(x=>x!==false)||i)}

References
XMoñOcci, XBind, Ocxi.js

About

Haskell-esque automatic partial application, without runtime.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp