Socket
Socket
Sign inDemoInstall

threading

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    threading

A clojure-inspired utility for threading arbitrary data into the first or last argument of a sequence of functions


Version published
Weekly downloads
66
increased by371.43%
Maintainers
1
Install size
10.5 kB
Created
Weekly downloads
 

Readme

Source

threading

A clojure-inspired utility for threading arbitrary data into the first or last argument of a sequence of functions

Motivation

In order to compose function operations on arbitrary data, you have to nest functions

expect(subtract(add(3, 3) 3)).toBe(3);

The legibility of the example decreases as the number of functions increase.

Example Usage

Thread-first

Apply the initial & resulting values of the function list to the first argument of the function.

Simply provide an initial value then the functions you'd like to compose on the value.

import { threadFirst } from 'threading';

const value = threadFirst(
    5,
    timesThree,
    plusOne
);

expect(value).toBe(16);

To use with a function that should already have a value you can pass an array containing the function and the other arguments.

import { threadFirst } from 'threading';

const value = threadFirst(
  5,
  [times 3],
  [divideby 5]
);

expect(value).toBe(3);
Thread-last

Apply the initial & resulting values of the function list to the last argument of the function.

Simply provide an initial value then the functions you'd like to compose on the value.

import { threadLast } from 'threading';

const value = threadLast(
    5,
    timesThree,
    plusOne
);

expect(value).toBe(16);

To use with a function that should already have a value you can pass an array containing the function and the other arguments.

import { threadLast } from 'threading';

const value = threadFirst(
  5,
  [times 2],
  [divideby 5]
);

expect(value).toBe(0.5);

Keywords

FAQs

Last updated on 03 Dec 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc