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

tamda

v1.0.5

Published

Practical functional programming library for TypeScript

Downloads

11

Readme

Tamda ~ Build Status Coverage Status npm version dependencies

Practical functional programming library for TypeScript

Goal

Other functional programming libraries either have none or lacking TypeScript support, or dive too deep into functional programming with mandatory data structures such as Functors and Monads.

Tamda aims to address these issues:

  • TypeScript is a first-class citizen
  • Low friction API - intuitive to use and easy to understand
  • No specialized data structures - just plain objects and primitives

The entire Tamda API revolves around pure functions: input/output.

All of them are exported separately so that they can be tree-shaken, but they can all be imported from index.

e.g. import { <fn> } from 'tamda'

Similarities

Like the name suggests, Tamda borrows much from Ramda, but also from lodash and underscore, and the functional programming paradigm in general.

Most functions share names and sometimes arguments, but implementation may differ wildly as all were built from scratch.

Tamda is also just a bunch of functions, as specified earlier, there are no specialized data structures.

Differences

The major one is that it's built with and for TypeScript, hence the API design had to accomodate related expectations and the implementation to workaround some intrinsic complexities.

For example, most functions that would take the data argument as the last parameter instead take it as first so that TypeScript can infer the object's type, e.g. map(array, i => {}).

However, all functions have an overload where the data argument is provided last so that it's easily composable, in a way that would be called "curried" in Ramda, but perhaps more accurately is just a "partially applied function", e.g. map (i => {}) (array).

Since the idea of using TypeScript is actually benefiting from type safety and inference, having these two overloads for each function makes using them stand-alone or when composing/piping a breeze, especially when TypeScript will be giving you all hints.

Some functions have limitations on how much TypeScript can infer and may require some hinting - the API tries to make that as easy as possible, but the vast majority just works.

It also means that functions are not "automatically curried" like in Ramda, where any number of arguments can be provided and the function will partially apply accordingly.

Usage

Standalone function

import { groupBy } from 'tamda';

const data = [
  { type: 'functional', name: 'groupBy' },
  { type: 'functional', name: 'map' },
  { type: 'imperative', name: 'for' },
];

groupBy(data, d => d.type);
// {
//   functional: [
//     { type: 'functional', name: 'groupBy' },
//     { type: 'functional', name: 'map' },
//   ],
//   imperative: [
//     { type: 'imperative', name: 'for' },
//   ],
// }

Compose/pipe

Both achieve the same thing, the difference is that compose is right-to-left and pipe is left-to-right.

If you're used to imperative, in general pipe feels more natural.

import { filter, map, pipe, sum, unique } from 'tamda';

const data = [1, 1, 2, 3, 4, 5, 5];

const doubleSumUniqueOdds = pipe(
  //         vvvvvv    needs a type hint here
  filter((n: number) => n % 2),
  unique(),
  sum(),
  map(n => n * 2) // infers the rest
);

doubleSumUniqueOdds(data);
// 18

You can also inline pipe execution with use, which will infer the type beforehand and immediately invoke pipe or compose when either is called.

import { concat, not, pipe, pluck, reduceWhile, tap, use } from 'tamda';

const data = [
  { amount: 5 },
  { amount: 4 },
];

const moreData = [
  { amount: 3 },
  { amount: 2 },
];

const hasEnough = (total: number) => total > 10;
const accumulate = pipe(
  (total: number, amount: number) => total + amount,
  tap(console.log)
);

use(data).pipe(
  concat(moreData),
  pluck('amount'), // same as  map(prop('amount'))
  reduceWhile(not(hasEnough), accumulate, 0)
);
// 5
// 9
// 12
// > 12