npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cast-curry

v1.0.0

Published

Curry, and partially apply funcitons

Downloads

5

Readme

cast-curry

A hybrid curry, and partial function.

Curry, or partially apply a function.

Example

var curry = require('cast-curry');

function func(a, b, c){
    return a + b + c;
}

var f = curry(func, 'x');
//print xyz
console.log(f('y', 'z'));

If some parameters positioned more to the left aren't known yet then you can use a placeholder.

var curry = require('cast-curry'),
    _ = require('cast-curry/__');

function func2(a, b, c, d){
    return a + b + c + d;
}

var part = curry(func2, _, 'x', _, 'z');
//print wxyz
console.log('part '+part('w', 'y'));

If the function you want to curry is variadic you can add a placeholder to the right if you know how many arguments you will need.

var curry = require('cast-curry'),
    _ = require('cast-curry/__');

function greet(){
    var str = '';

    for(var i=0; i<arguments.length; i++){
        str += arguments[i];
    }

    return str;
}

var getGreeting = curry(greet, 'Hello ', _, _, _, _);
//print Hello how are you?
console.log(getGreeting('how ', 'are ')('you')('?'));

Variadic functions won't work correctly if you don't add a placeholder to the right.

var getGreeting = curry(greet, 'Hello');
//This is an error.
console.log(getGreeting('how ', 'are ')('you')('?'));

this!

You can pass a this context as the first argument to set the function's context.

If you do then set the function to curry to the second argument.

Here push is native. Like many native functions push doesn't have a Function.length property so you need to set a placeholder as the first push parameter.

var array = [];

var push = curry(array, [].push, _);

//print 1
console.log(push('5'));

//print [ '5' ]
console.log(array);

//print 2
console.log(push('10'));

//print [ '5', '10' ]
console.log(array);

Warning!

Objects, and arrays still have references to their data even when passed to a function.

So if you curry a function with a default parameter of array, or object later they can be altered. This means sometimes you might find you're data has changed unexpectedly.

This is ok if that's what you want, and you think you can keep track of that.

In many situations you probably don't want partial parameters to change.

For partial parameters use a deep clone function, or a library like Immutable.js to order to make your objects, or list types not changeable if you want to limit side effects.

About

Curry functions, and make partials out of functions.

The placeholder parameter concept is taken from the really cool library Rambda.