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

typecat

v0.1.18

Published

A tiny functional programming library for TypeScript that provides Option (Some/None) and Either(Left/Right) with functor map, applicative map and monad flatmap

Downloads

24

Readme

TypeCat

Introduction

TypeCat is a tiny, zero dependency TypeScript library that supplements the missing but very practical types (classes in TypeScript term) you can find in typical functional programming laugages such as Scala or Haskell:

  • Option and subclasses Some and None
  • Either and subclasses Left and Right

Classical functional operators fmap, applyMap ( applicative map as in Haskell ) and flatMap ( monad map as in Haskell and Scala ).

Iterator is also added so that you can use convenient for expression on the types.

The types and the operators meet the laws of Functor, Applicative and Monad.

Documentation @ Gitbook

undefined and null

It is decided explicitly that Some / Right can't be initiated with undefined and null. TypeError will throwned if tried.

The reason is to simplify and also streamline the functions used for fmap, applicativeMap and flatMap.

get getOrElse, getLeft, getLeftOrElse

TypeCat provides methods to take out value from Option/Either

  • get / getOrElse : Option / Either. If None or Left, get will throw exception. Use getOrElse (defaultValue)
  • getLeft / getLeftOrElse: Either. If Right, getLeft will throw exception. Use getLeftOrElse(defaultValue)

Usage

Install

yarn add typecat

or

npm install typecat

Use

import { id, Option, Some, None, Either, Left, Right } from 'typecat'

const option = new Some(3).fmap( ( i: number ) => i * 2 ) 

// option = Some( 6 )

console.log( option )

for( const i of option ) 
    console.log( i )  // Console output 6
    
const failed = new Some( 0 ).
    flatMap( ( i: number ) => i === 0 ? new None() : new Some ( 1 / i ) ).
    fmap( (w: number) => `inverse is ${w}` )
    
    // failed = None

console.log(failed)

const succeeded = new Some( 10 ).
    flatMap( ( i: number ) => i === 0 ? new None() : new Some ( 1 / i ) ).
    fmap( (w: number) => `inverse is ${w}` )
    
    // succeeded = Some(0.1)
    
console.log(succeeded)

const either = new Right(25).fmap( ( i: number ) => i > 20 ? true : false )
    .flatMap( ( t: boolean ) => t ? new Right( 'Ok' ) : new Left( 'Fail' ) )
    
    // Either = Right('Ok')
const triplePlusF = (a: number) => (b: number) => (c: number) => a + b + c
const triplePlus = new Some(triplePlusF)
    
const a = new Some(2)
const b = new Some(3)
const c = new Some(4)
const d = new None()

const plusedA = triplePlus.applyMap(a).applyMap(b).applyMap(c) // plusdA = Some( 2 + 3 + 4 )

console.log(plusedA) // plusedA = Some(9)

const ra = plusedA.get() // ra = 9


const plusedB = triplePlus.applyMap(a).applyMap(b).applyMap(d) // plusedB = None()
const rb = plusedB.get() // Exception thrown : Value not exist in None
const rc = plusedB.getOrElse(0) // rc = 0

console.log(ra)
console.log(rb)
console.log(rc)