New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cast-curry

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cast-curry

Curry, and partially apply funcitons

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
4
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 05 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc