evilsoft / crocks

A collection of well known Algebraic Data Types for your utter enjoyment.
https://crocks.dev
ISC License
1.59k stars 102 forks source link
adt composition curry functor lift monad monoid pair predicate-functions unary-functions

Build Status Coverage Status Join the chat at https://gitter.im/crocksjs/crocks NPM version

crocks is a collection of popular Algebraic Data Types (ADTs) that are all the rage in functional programming. You have heard of things like Maybe and Either and heck maybe even IO, that is what these are. The main goal of crocks is to curate and provide not only a common interface between each type (where possible of course), but also provide all of the helper functions needed to hit the ground running.

Table of Contents

Installation

crocks is available from npm and is just a shell command away. All you need to do is run the following to save it as a dependency in your current project folder:

$ npm install crocks -S

Usage

There are many options to use crocks to suit the needs of your, projects. When used on the backend or in an environment where size is not a big concern, the entire lib can be brought in and the various elements can be either be plucked off of or referenced by the namespace.

For those cases where size matters, like in the case of frontend bundle building, the individual entities can be brought in. This will ensure that your finished bundles include only what is needed for your application/program.

For using the latter case, refer to the desired function's documentation to find the path in which it resides.

Entire crocks library (CommonJS)

// namespace entire suite to crocks variable
const crocks = require('crocks')

// pluck anything that does not require name-spacing
const { safe, isNumber } = crocks

// still requires entire object, but removes name-spacing
const { and, liftA2 } = require('crocks')

Entire crocks library (JS Modules)

// namespace entire suite to crocks variable
import crocks from 'crocks'

// still imports entire object, but removes name-spacing
import { and, liftA2 }  from 'crocks'

// pluck anything that does not require name-spacing
const { safe, isNumber } = crocks

Single entities (CommonJS)

// require in each entity directly
const and = require('crocks/logic/and')
const curry = require('crocks/helpers/curry')
const isNumber = require('crocks/predicates/isNumber')
const liftA2 = require('crocks/helpers/liftA2')
const safe = require('crocks/Maybe/safe')

Single entities (JS Modules)

// import in each entity directly
import and from 'crocks/logic/and'
import curry from 'crocks/helpers/curry'
import isNumber from 'crocks/predicates/isNumber'
import liftA2 from 'crocks/helpers/liftA2'
import safe from 'crocks/Maybe/safe'

Example

Documentation references: and, curry, predicates (isNumber), liftA2, safe.

// divide :: Number -> Number -> Number
const divide = x => y =>
  x / y

// safeNumber :: a -> Maybe Number
const safeNumber =
  safe(isNumber)

// notZero :: a -> Maybe Number
const notZero = safe(
  and(isNumber, x => x !== 0)
)

// safeDivide:: a -> a -> Maybe Number
const safeDivide = curry(
  (x, y) => liftA2(divide, safeNumber(x), notZero(y))
)

safeDivide(20)(0)
//=> Nothing

safeDivide(20, 0)
//=> Nothing

safeDivide(20, 5)
//=> Just 4

safeDivide('number', 5)
//=> Nothing

Documentation

What is Included?

There are (8) classifications of "things" included in this library:

Contributors

Thanks goes to these wonderful people (emoji key):


Ian Hofmann-Hicks

πŸ’» πŸ“– πŸ“Ή

Ryan

πŸ’» πŸ› πŸ‘€

Andrew Van Slaars

πŸ“– πŸ“Ή

Henrique Limas

πŸ’» πŸ“– πŸ‘€

Robert Pearce

πŸ› πŸ’» πŸ‘€ βœ… πŸ“–

Scott McCormack

πŸ›

Fred Daoud

πŸ‘€

Karthik Iyengar

πŸ‘€ πŸ’» πŸ“–

Jon Whelan

πŸ› πŸ’»

Benny Powers

πŸ“– πŸ’» πŸ‘€

Dale Francis

πŸ’» πŸ‘€

Premith

πŸ“–

Dipen Bagia

πŸ’‘

Andrew Jones

πŸ“–

W. K. Seymour III

πŸ“–

Eliseu Benedito Codinhoto

πŸ“–

NiallArkEnergy

πŸ’» πŸ“–

vidyu

πŸ“–

Michael Wolfenden

πŸ“–

Johan Codinha

πŸ›

Matt Ross

πŸ’»

Jasmina Jacquelina

πŸ“– πŸ’»

Denis Zolkin

πŸ“–

Zhentian Wan

πŸ“–

RichardForrester

πŸ’» πŸ“– ⚠️

Furkan TunalΔ±

πŸ“– πŸ’‘

Paul Desmond Parker

πŸ“–

Rodrigo Erades

πŸ“–

Jamie Dixon

πŸ’»

Basant Pandey

πŸ“–

Course/Videos

Video evilsoft

Video avanslaars

Tutorials

Tutorial rpearce

Examples

Example dbagia