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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@algosail/fn

v0.1.0

Published

Function combinators. Building blocks for point-free, pipeable code.

Readme

@algosail/fn

Function combinators and the Reader monad. Building blocks for point-free, pipeable code.

Contents


pipe

pipe :: Foldable f => f (Any -> Any) -> a -> b

Threads a value through a left-to-right sequence of functions. The primary composition tool.

pipe([x => x + 1, x => x * 2])(3)   // => 8   (first +1, then *2)
pipe([Math.abs, Math.sqrt])(−9)       // => 3
pipe([String, s => s.toUpperCase()])(42) // => '42' -> '42' (wrong, let me fix:)

// Build reusable transforms
const normalize = pipe([s => s.trim(), s => s.toLowerCase()])
normalize('  Hello  ')  // => 'hello'

pipeK

pipeK :: (Foldable f, Flatmap m) => f (Any -> m Any) -> m a -> m b

Left-to-right Kleisli composition. Each function receives a plain value and returns a monadic value; the monad is threaded through via flatmap.

import * as M from '@algosail/maybe'

const safeSqrt = (x) => (x >= 0 ? M.just(Math.sqrt(x)) : M.nothing())
const safeLog = (x) => (x > 0 ? M.just(Math.log(x)) : M.nothing())

pipeK([safeSqrt, safeLog])(M.just(100)) // => just(log(10))
pipeK([safeSqrt, safeLog])(M.just(-1)) // => nothing()

id

id :: a -> a

Identity — returns its argument unchanged. Useful as a no-op placeholder.

id(42) // => 42
id([1, 2, 3]) // => [1,2,3]

const_

const_ :: a -> b -> a

K combinator — always returns the first argument regardless of the second.

const_('x')('ignored') // => 'x'
  [(1, 2, 3)].map(const_(0)) // => [0, 0, 0]

T

T :: a -> (a -> b) -> b

Thrush combinator — applies an argument to a function. Useful for flipping callsite style.

T(42)((x) => x + 1) // => 43
T([1, 2, 3])((arr) => arr.length) // => 3

on

on :: (b -> b -> c) -> (a -> b) -> a -> a -> c

P combinator — applies a binary function after first mapping both arguments through g.

// Compare strings case-insensitively
on((a) => (b) => a === b)((s) => s.toLowerCase())('Hello')('hello') // => true

// Sum lengths
on((a) => (b) => a + b)((s) => s.length)('foo')('hello') // => 8

compose

compose :: (b -> c) -> (a -> b) -> a -> c

Right-to-left function composition.

compose((x) => x * 2)((x) => x + 1)(3) // => 8  (first +1, then *2)

flip

flip :: (a -> b -> c) -> b -> a -> c

Flips the order of the first two arguments of a curried function.

const sub = (a) => (b) => a - b
sub(1)(3) // => -2
flip(sub)(1)(3) // => 2   (3 - 1)

contramap

contramap :: (b -> a) -> (a -> c) -> b -> c

Pre-composes a function (contravariant map). Transforms the input of a function.

const strlen = (s) => s.length
contramap((s) => s.trim())(strlen)('  hi  ') // => 2

promap

promap :: (a -> b) -> (c -> d) -> (b -> c) -> a -> d

Maps both the input and output of a function.

// Transform input with +1, output with *2
promap((x) => x + 1)((x) => x * 2)((x) => x)(3) // => 8

handleThrow

handleThrow :: ((...d) -> a) -> ((a, d) -> r) -> ((Error, d) -> r) -> (...d) -> r

Wraps a function so thrown errors are caught and routed to onThrow instead of propagating.

const safeParseJSON = handleThrow(
  JSON.parse,
  (result) => result,
  (err) => null,
)

safeParseJSON('{"a":1}') // => { a: 1 }
safeParseJSON('!bad!') // => null

map

map :: (a -> b) -> (e -> a) -> e -> b

Reader functor — post-composes a function. Transforms the output.

map((x) => x + 1)((x) => x * 2)(3) // => 7  (3*2=6, then +1)

ap

ap :: (e -> a -> b) -> (e -> a) -> e -> b

S combinator — ap(ff)(fa)(x) = ff(x)(fa(x)).

ap((e) => (x) => e + x)((e) => e * 2)(3) // => 9  (3 + 3*2)

of

of :: a -> e -> a

Lifts a value into the Reader context (constant function).

of(42)('anything') // => 42

flatmap

flatmap :: (a -> e -> b) -> (e -> a) -> e -> b

Reader monad bind — depends on the same environment e.

// Both functions share the environment (multiplier)
flatmap((a) => (e) => a + e)((e) => e * 2)(3) // => 9  (3*2=6, then 6+3)

extend

extend :: ((e, e) -> e) -> ((e -> a) -> b) -> (e -> a) -> e -> b

Comonad extend for the Reader context.


chainRec

chainRec :: ((next, done, a) -> e -> Step) -> a -> e -> b

Stack-safe tail-recursive Reader bind. Use instead of flatmap when recursion depth is unbounded.

// Count down without stack overflow
chainRec((next, done, n) => (_) => (n <= 0 ? done(n) : next(n - 1)))(1_000_000)(
  null,
) // => 0