Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

dethunking-compose

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dethunking-compose

Lazy evaluation + compose

  • 0.1.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
13
increased by550%
Maintainers
1
Weekly downloads
 
Created
Source

Dethunking Compose

[Function composition](https://en.wikipedia.org/wiki/Function composition) allows the writing of declarative functional code. I like to pair this style of coding with the main functions at the top of a file followed by the definitions of the smaller functions out of which it's composed.

Unfortunately, if you are writing in javascript and using function expressions rather than function statements (because, for example, you are writing curried functions) these can be difficult to combine.

Because the arguments to a javascript function are eagerly evaluated, the functions you are composing have to be defined above the point that you compose them.

This module allows you to pass a series of thunks to a compose function (thus delaying their evaluation) which you allows you to define the smaller functions later (it still has to be before their actual use).

This module also stringifies in a debuggable way.

I'm not sure this module is worth using without the low affordance for writing thunks provided by ES6 arrow functions.

Example

var compose = require('dethunk-compose')

var triple_add_3 = module.exports = compose(
    () => add_2,
    () => add_1,
    () => triple
)

var add_2 = compose(
    () => add_1,
    () => add_1
)

var add_1 = x => x + 1

var triple = x => x * 3

Compare to this attempt with a normal compose module, which would throw.

var compose = require(/* Normal compose module */)

var add_2 = compose(
    add_1,
    add_1
)

var add_1 = x => x + 1

add_2(4)

// undefined is not a function

Pipe

If you prefer to compose left-to-right, you can use the pipe API.

var pipe = require('dethunk-compose').pipe

var triple_add_3 = module.exports = pipe(
    () => triple,
    () => add_1,
    () => add_2
)

...

Keywords

FAQs

Package last updated on 02 Dec 2015

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