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

@funkia/jabz

v0.0.24

Published

Powerful and practical abstractions.

Downloads

1,804

Readme

Jabz

Powerful and practical abstractions for JavaScript. Functors, monads, foldables, traversables, and all that jazz.

Build Status codecov Gitter chat

Goals and features

  • Be as simple and convenient as possible in usage
  • Allow for performant implementations
  • TypeScript support
  • Batteries included. Provide implementations of often used instances and commonly used utility functions.
  • Do-notation
  • Seamless instances
  • Comes with files in ES2015 module format for tree-shaking

For a more detailed introduction to the design of the specification and a comparison to Fantasy Land please see this blog post.

Install

npm install @funkia/jabz

Documentation

See the API documentation and the example below.

Note that the specification for the abstractions is not written down formally yet. But the source code contain TypeScript interfaces that documents the different required methods. The laws associated with the abstractions are as expected if one is familiar with them.

Example

This example demonstrates some of what Jabz can do by implementing a simple singly linked list aka. a cons list.

@monad @traversable
class Cons {
  constructor(v, t) {
    this.val = v;
    this.tail = t;
  }
  concat(c) {
    return this === nil ? c : cons(this.val, this.tail.concat(c));
  }
  of(b: B) {
    return cons(b, nil);
  }
  chain<B>(f) {
    return this === nil ? nil : f(this.val).concat(this.tail.chain(f));
  }
  traverse<B>(a, f) {
    return this === nil ? a.of(nil) : lift(cons, f(this.val), this.tail.traverse(a, f));
  }
}
const nil = new Cons(undefined, undefined);
function cons(a: A, as) {
  return new Cons(a, as);
}
function fromArray(as) {
  return as.length === 0 ? nil : cons(as[0], fromArray(as.slice(1)));
}

Since Cons contains the methods of and chain it can implement monad. This is done with the @monad decorator. JavaScript decorators are just plain old functions so they can also be used without the decorator syntax

monad(Cons);

The function allows implementations flexibility in what methods they choose to provide. For instance monad can also be implemented by defining a of, a map and a chain method.

Similar to Monad, Traversable is implemented by defining the traverse method and using the traversable decorator.

When we implement Monad Jabz automatically derives implementations for Functor and Applicative. Likewise when we implement Traversable it derives Foldable. Thus, Jabz can give us a lot of things for free just from the few methods the Cons class defines.

Map functions over elements in the list.

mapTo((n) => n * n, fromArray([1, 2, 3, 4])); //=> [1, 4, 9, 16]

Change each element in the list to a constant.

mapTo(8, fromArray([1, 2, 3, 4])); //=> [8, 8, 8, 8]

Apply a list of functions to a list of values.

ap(fromArray([(n) => n * 2, (n) => n * n]), fromArray(1, 2, 3)); //=> [2, 4, 6, 1, 4, 9]

Folding.

foldr((n, m) => n + m, 3, fromArray([1, 2, 3, 4, 5])); //=> 18

Find an element satisfying a predicate.

find((n) => n > 6, fromArray([1, 8, 3, 7, 5])); //=> just(8)
findLast((n) => n > 6, fromArray([1, 8, 3, 7, 5])); //=> just(7)

We can convert a cons-list to an array

toArray(fromArray([1, 2, 3, 4])); //=> [1, 2, 3, 4]

We can flatten nested cons-lists.

flatten(fromArray([fromArray([1, 2]), fromArray([3, 4, 5])])); //=> [1, 2, 3, 4, 5]

Seamless instances

Seamless instances means that certain native JavaScript types can be used as if they implemented the abstractions relevant for them.

  • string, implements setoid and monoid.
  • array, implements setoid, monoid, functor, foldable and traversable.