{"id":13547907,"url":"https://github.com/gilbert/es-papp","last_synced_at":"2025-04-09T13:05:30.676Z","repository":{"id":57318734,"uuid":"46007853","full_name":"gilbert/es-papp","owner":"gilbert","description":"A proposal for adding partial application support to JavaScript.","archived":false,"fork":false,"pushed_at":"2017-08-02T13:11:44.000Z","size":8,"stargazers_count":361,"open_issues_count":5,"forks_count":12,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-01T18:22:38.881Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gilbert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-11T20:24:42.000Z","updated_at":"2025-04-01T16:17:17.000Z","dependencies_parsed_at":"2022-08-25T20:40:12.389Z","dependency_job_id":null,"html_url":"https://github.com/gilbert/es-papp","commit_stats":null,"previous_names":["mindeavor/es-papp"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbert%2Fes-papp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbert%2Fes-papp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbert%2Fes-papp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gilbert%2Fes-papp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gilbert","download_url":"https://codeload.github.com/gilbert/es-papp/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045230,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:01:03.020Z","updated_at":"2025-04-09T13:05:30.641Z","avatar_url":"https://github.com/gilbert.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# ECMAScript Proposal: Function.prototype.papp\n\nThis proposal introduces `papp` and `pappRight` – concise ways of using partial application for functions that require no immediate `this` parameter. It is backwards-compatible, and is immediately useful with most any JavaScript function today.\n\n## Try it out!\n\n    npm install --save papp-polyfill\n\nThen require it once (in your code, as early as possible):\n\n    require('papp-polyfill')\n\n## Introduction\n\nPartial application is possible in JavaScript via `Function.prototype.bind`:\n\n```js\nfunction add (x, y) { return x + y; }\n\nvar addTen = add.bind(null, 10);\naddTen(20) //=\u003e 30\n```\n\nHowever, `bind` is undesirable for three reasons:\n\n1. Sometimes you don't care about the value of `this`, yet you still must provide `bind`'s first argument\n2. Sometimes you **do** care about the value of `this`, but don't want to commit to a specific value yet.\n3. ~~[Using `bind` is significantly slower than using a plain closure](http://stackoverflow.com/questions/17638305/why-is-bind-slower-than-a-closure) (!)~~ (this [has been fixed](http://v8project.blogspot.co.uk/2016/06/release-52.html) in V8, and wasn't an issue in other engines for quite a while)\n\n`Function.prototype.papp` solves both these issues in a simple, elegant, and noise-free manner. Here is an illustrative example:\n\n```js\nfunction add (x, y, z) { return x + y + z; }\n\nvar addTen = add.papp(3, 7);\naddTen(5) //=\u003e 15\n\n// AS OPPOSED TO:\n// var addTen = add.bind(null, 3, 7)\n// OR:\n// var addTen = (x) =\u003e add(3, 7, x)\n\nvar addThenIncrement = add.papp(1);\naddThenIncrement(10, 6) //=\u003e 17\n\n// AS OPPOSED TO:\n// var addThenIncrement = add.bind(null, 1)\n// OR:\n// var addThenIncrement = (a, b) =\u003e add(1, a, b)\n```\n\nAccepting `papp` into the ES standard will allow JS runtimes to implement a more performant version of `bind` that is dedicated to partial application.\n\n### Ignoring `this`\n\nFor functions that don't use the keyword `this`, `papp` helps with brevity:\n\n```js\nfunction add (x, y) { return x + y; }\n\nvar addTen = add.papp(10);\naddTen(20) //=\u003e 30\n```\n\n### Deferring Function Binding\n\nIf a function *does* use the keyword `this`, `papp` allows you to partially apply arguments without committing to a `this` value:\n\n```js\nfunction greet (target) {\n  return `${this.name} greets ${target}`;\n}\n\nvar greetNewcomer = greet.papp('the newcomer');\ngreetNewcomer.call({ name: 'Alice' }) //=\u003e 'Alice greets the newcomer'\n```\n\n\n## Practical Examples\n\nThese examples are pulled from real-world use cases of partial application.\n\n### HTTP API Output Whitelisting\n\n```js\nPlayer.whitelist = {\n  basic: pluck.papp(['name', 'score']),\n  admin: pluck.papp(['name', 'score', 'email']),\n};\n\nfunction pluck (attrs, obj) {\n  var result = {};\n  attrs.forEach( name =\u003e result[name] = obj[name] );\n  return result;\n}\n\n// Example use (in practice, alice would come from a database):\nvar alice = { name: 'Alice', score: 100, email: 'alice@example.com', password_hash: '...' };\n\nPlayer.whitelist.basic(alice) //=\u003e { name: 'Alice', score: 100 }\n\nPlayer.whitelist.admin(alice) //=\u003e { name: 'Alice', score: 100, email: 'alice@example.com' }\n```\n\n### Constructing User-friendly APIs\n\n```js\nfunction createClient (host) {\n  return {\n    get:  makeRequest.papp(host, 'GET'),\n    post: makeRequest.papp(host, 'POST'),\n    put:  makeRequest.papp(host, 'PUT'),\n    del:  makeRequest.papp(host, 'DELETE'),\n  };\n}\n\nvar client = createClient('https://api.example.com');\nclient.get('/users');\nclient.post('/comments', { content: \"papp is great!\" });\n\nfunction makeRequest (host, method, url, data, options) {\n  // ... Make request, return a promise ...\n}\n\n// AS OPPOSED TO:\n// function createClient (host) {\n//   return {\n//     get:  (url, data, options) =\u003e makeRequest(host, 'GET', url, data, options),\n//     post: (url, data, options) =\u003e makeRequest(host, 'POST', url, data, options),\n//     put:  (url, data, options) =\u003e makeRequest(host, 'PUT', url, data, options),\n//     del:  (url, data, options) =\u003e makeRequest(host, 'DELETE', url, data, options),\n//   }\n// }\n```\n\n## Other Examples\n\nThese examples illustrate concepts you can use in your own applications.\n\n### Mapping with Arrays\n\n```js\nvar chapters = [\"The Beginning\", \"Climax\", \"Resolution\"];\n\nvar numberedChapters = chapters.map( toListItem.papp('My Book') )\n//=\u003e [\"My Book / 1. The Beginning\", \"My Book / 2. Climax\", \"My Book / 3. Resolution\"]\n\n// AS OPPOSED TO:\n// var numberedChapters = chapters.map( (chapter, i) =\u003e toListItem('My Book', chapter, i) )\n\nfunction toListItem (prefix, item, i) {\n  return `${prefix} / ${i + 1}. ${item}`\n}\n```\n\n## Polyfill\n\nES6:\n\n```js\nFunction.prototype.papp = function (...args) {\n  var fn = this;\n  return function (...moreArgs) {\n    return fn.apply(this, [...args, ...moreArgs]);\n  };\n};\n```\n\nES5:\n\n```js\nFunction.prototype.papp = function () {\n  var slice = Array.prototype.slice;\n  var fn = this;\n  var args = slice.call(arguments);\n  return function () {\n    return fn.apply(this, args.concat(slice.call(arguments)));\n  };\n};\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbert%2Fes-papp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgilbert%2Fes-papp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgilbert%2Fes-papp/lists"}