Skip to content

Instantly share code, notes, and snippets.

@peterhil
Forked from 140bytes/LICENSE.txt
Created May 23, 2011 21:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterhil/987688 to your computer and use it in GitHub Desktop.
Save peterhil/987688 to your computer and use it in GitHub Desktop.
140byt.es -- Partial function application

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression, that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

The 140byt.es site hasn't launched yet, but for now follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt. It was inspired by work from Thomas Fuchs and Dustin Diaz.

function(){
var s=[].slice,
a=s.call(arguments),
f=a.shift();
return function(){
return f.apply(null,a.concat(s.call(arguments)));
};
}
function(){var s=[].slice,a=s.call(arguments),f=a.shift();return function(){return f.apply(null,a.concat(s.call(arguments)));};}
Copyright (c) 2011 Peter Hillerström, http://peter.vasb.fi/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "140bytes-peterhil-partial",
"description": "From Wikipedia: Partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.",
"keywords": [
"functional",
"partial application",
"partial"
]
}
@peterhil
Copy link
Author

Usage example:
-> var partial = function(){var s=Array.prototype.slice,a=s.call(arguments),f=a.shift();return function(){return f.apply(null, a.concat(s.call(arguments)))}}
-> var pow2 = partial(Math.pow, 2)
undefined
-> pow2(16)
65536
-> var r=[]; for (var i = 0; i <= 64; i++) { r.push(i); };
65
-> map(pow2, r)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963970, 72057594037927940, 144115188075855870, 288230376151711740, 576460752303423500, 1152921504606847000, 2305843009213694000, 4611686018427388000, 9223372036854776000, 18446744073709552000]

@jed
Copy link

jed commented Jul 11, 2011

hey @peterhil, would you mind taking the comments out of your package.json?

@peterhil
Copy link
Author

@jed Here you are. Removed the comments.

@minikomi
Copy link

Could you use s=[].prototype.slice to save a few chars?

@peterhil
Copy link
Author

peterhil commented Oct 5, 2011

@minikomi first I thought that wouldn't work, but it seems you can do s=[].slice and then s.call([3,4,5,6],1,3), which returns [4, 5].

@minikomi
Copy link

minikomi commented Oct 5, 2011

Even shorter :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment