DEV Community

Jason
Jason

Posted on • Edited on • Originally published at rametta.org

Pratica - Monadic Library, now fully in Typescript!

Pratica is now written completely in Typescript!

What is Pratica?

Pratica is a super tiny 720B monadic library, comparable to Crocks or Monet JS.

Why would I use Pratica?

If you want to start writing more functional code in Javascript or Typescript, this is a great library for learning some FP fundamentals, while also making your code safer and more resilient to runtime bugs. It's super tiny size and easy to read dot-chaining syntax makes it easy to get started in any project.

How do I start?

You can install it with: yarn add pratica or npm i pratica. Then you can import the main functions like:

import { nullable } from 'pratica'
Enter fullscreen mode Exit fullscreen mode

Create small, safe and easy to read programs by composing functions together, like:

// Typescript

import { Maybe, nullable, get, parseDate } from 'pratica'

const getPersonAge = (person?: Person): Maybe<number> =>
  nullable(person)
    .chain(get<string>(['birthday']))
    .chain(parseDate)
    .map(birthday => Date.now() - birthday.getTime())
    .chain(parseDate)
    .map(date => Math.abs(date.getUTCFullYear() - 1970))


getPersonAge({ birthday: '1994-06-08' }) // -> Just(25)
getPersonAge({ birthday: 771033600000  }) // -> Just(25)
getPersonAge({ birthday: null }) // -> Nothing
getPersonAge(null) // -> Nothing
Enter fullscreen mode Exit fullscreen mode

Pratica works great with React too! Use it in your JSX for handling cases with missing data.

const viewPersonAge = ({ person }) =>
  getPersonAge(person).cata({
    Just: age => <div>{age}</div>
    Nothing: () => <span>No age available</span>
  })
Enter fullscreen mode Exit fullscreen mode

Try it out

Try it out in an online browser sandbox here!

or check it out on Github below!

GitHub logo rametta / pratica

🥃 Functional Algebraic Data Types

npm Pratica License PRs Welcome

🥃 Pratica

Functional Programming for Pragmatists

Why is this for pragmatists you say?

Pratica sacrifices some common FP guidelines in order to provide a simpler and more approachable API that can be used to accomplish your goals quickly - while maintaining data integrity and safety, through algrebraic data types.

Install

bun i pratica
# or
yarn add pratica
# or
npm i pratica
Enter fullscreen mode Exit fullscreen mode

Documentation

Monads

Maybe

Use this when dealing with nullable and unreliable data that needs actions performed upon.

Maybe is great for making sure you do not cause runtime errors by accessing data that is not there because of unexpected nulls or undefineds.

Every Maybe can either be of type Just or Nothing. When the data is available, it is wrapped with Just, if the data is missing, it is Nothing. The examples below should clarify futher.

Maybe.map

Map is used for running a function…




Top comments (5)

Collapse
 
hongduc profile image
Hong duc

ohh interesting :)

Collapse
 
buinauskas profile image
Evaldas Buinauskas

This is gold.

Collapse
 
macsikora profile image
Pragmatic Maciej

Jason very nice. I am working currently on something similar. If I gave up, maybe I will do some contribution to your lib. But I feel the same need of something simple with fp powers. Thanks for that!

Collapse
 
rametta profile image
Jason

Awesome, any contributions to this library will be welcome!

Collapse
 
sdyalor profile image
sdyalor

😱️😱️