SkillAgentSearch skills...

Pratica

🥃 Functional Algebraic Data Types

Install / Use

/learn @rametta/Pratica
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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

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 on the data inside the Maybe. Map will only run the function if the Maybe type is Just. If it's Nothing, the map will short circuit and be skipped.

import { nullable } from "pratica"

const person = { name: "Jason", age: 4 }

// Example with real data
nullable(person)
  .map((p) => p.age)
  .map((age) => age + 5)
  .cata({
    Just: (age) => console.log(age), // 9
    Nothing: () => console.log(`This function won't run`),
  })

// Example with null data
nullable(null)
  .map((p) => p.age) // Maybe type is Nothing, so this function is skipped
  .map((age) => age + 5) // Maybe type is Nothing, so this function is skipped
  .cata({
    Just: (age) => console.log(age), // Maybe type is Nothing, so this function is not run
    Nothing: () => console.log("Could not get age from person"), // This function runs because Maybe is Nothing
  })
Maybe.chain

Chain is used when you want to return another Maybe when already inside a Maybe.

import { nullable } from "pratica"

const person = { name: "Jason", age: 4 }

nullable(person)
  .chain((p) => nullable(p.height)) // p.height does not exist so nullable returns a Nothing type, any .map, .chain, or .ap after a Nothing will be short circuited
  .map((height) => height * 2.2) // this func won't even run because height is Nothing, so `undefined * 2.2` will never execute, preventing problems.
  .cata({
    Just: (height) => console.log(height), // this function won't run because the height is Nothing
    Nothing: () => console.log("This person has no height"),
  })
Maybe.alt

Alt is a clean way of making sure you always return a Just with some default data inside.

import { nullable } from "pratica"

// Example with default data
nullable(null)
  .map((p) => p.age) // won't run
  .map((age) => age + 5) // won't run
  .alt(99) // the data is null so 99 is the default
  .cata({
    Just: (age) => console.log(age), // 99
    Nothing: () => console.log(`This function won't run because .alt() always returns a Just`),
  })
Maybe.ap

Sometime's working with Maybe can be reptitive to always call .map whenever needing to a apply a function to the contents of the Maybe. Here is an example using .ap to simplify this.

Goal of this example, to perform operations on data inside the Maybe, without unwrapping the data with .map or .chain

import { Just, nullable } from "pratica"

// Need something like this
// Just(6) + Just(7) = Just(13)
Just((x) => (y) => x + y)
  .ap(Just(6))
  .ap(Just(7))
  .cata({
    Just: (result) => console.log(result), // 13
    Nothing: () => console.log(`This function won't run`),
  })

nullable(null) // no function to apply
  .ap(Just(6))
  .ap(Just(7))
  .cata({
    Just: () => console.log(`This function won't run`),
    Nothing: () => console.log(`This function runs`),
  })
Maybe.inspect

Inspect is used for seeing a string respresentation of the Maybe. It is used mostly for Node logging which will automatically call inspect() on objects that have it, but you can use it too for debugging if you like.

import { nullable } from "pratica"

nullable(86).inspect() // `Just(86)`
nullable("HELLO").inspect() // `Just('HELLO')`
nullable(null).inspect() // `Nothing`
nullable(undefined).inspect() // `Nothing`
Maybe.cata

Cata is used at the end of your chain of computations. It is used for getting the final data from the Maybe. You must pass an object to .cata with 2 properties, Just and Nothing (capitalization matters), and both those properties must be a function. Those functions will run based on if the the computations above it return a Just or Nothing data type.

Cata stands for catamorphism and in simple terms means that it extracts a value from inside any container.

import { Just, Nothing } from "pratica"

const isOver6Feet = (person) => (person.height > 6 ? Just(person.height) : Nothing)

isOver6Feet({ height: 4.5 })
  .map((h) => h / 2.2)
  .cata({
    Just: (h) => console.log(h), // this function doesn't run
    Nothing: () => console.log(`person is not over 6 feet`),
  })
Maybe.toResult

toResult is used for easily converting Maybe's to Result's. Any Maybe that is a Just will be converted to an Ok with the same value inside, and any value that was Nothing will be converted to an Err with no value passed. The cata will have to include Ok and Err instead of Just and Nothing.

import { Just, Nothing } from "pratica"

Just(8)
  .toResult()
  .cata({
    Ok: (n) => console.log(n), // 8
    Err: () => console.log(`No value`), // this function doesn't run
  })

Nothing.toResult().cata({
  Ok: (n) => console.log(n), // this function doesn't run
  Err: () => console.log(`No value`), // this runs
})
Maybe.isJust

isJust returns a boolean representing the type of the Maybe. If the Maybe is a Just type then true is returned, if it's a Nothing, returns false.

import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

isOver6Feet(7).isJust() // true
isOver6Feet(4).isJust() // false
Maybe.isNothing

isNothing returns a boolean representing the type of the Maybe. If the Maybe is a Just type then false is returned, if it's a Nothing, returns true.

import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

isOver6Feet(7).isNothing() // false
isOver6Feet(4).isNothing() // true
Maybe.value

value returns the encapsulated value within the Maybe. If the Maybe is a Just type, then the arg is returned, otherwise, if it is a Nothing, then it returns undefined.

import { Just, Nothing } from "pratica"

const isOver6Feet = (height) => (height > 6 ? Just(height) : Nothing)

isOver6Feet(7).value() // 7
isOver6Feet(4).value() // undefined

Result

Use this when dealing with conditional logic. Often a replacment for if statements - or for simplifying complex logic trees. A Result can either be an Ok or an Err type.

Result.map
import { Ok, Err } from "pratica"

const person = { name: "jason", age: 4 }

Ok(person)
  .map((p) => p.name)
  .cata({
    Ok: (name) => console.log(name), // 'jason'
    Err: (msg) => console.error(msg), // this func does not run
  })
Result.chain
import { Ok, Err } from "pratica"

const person = { name: "Jason", age: 4 }

const isPerson = (p) => (p.name && p.age ? Ok(p) : Err("Not a person"))

const isOlderThan2 = (p) => (p.age > 2 ? Ok(p) : Err("Not older than 2"))

const isJason = (p) => (p.name === "jason" ? Ok(p) : Err("Not jason"))

Ok(person)
  .chain(isPerson)
  .chain(isOlderThan2)
  .chain(isJason)
  .cata({
    Ok: (p) => console.log("this person satisfies all the checks"),
    Err: (msg) => console.log(msg), // if any checks return an Err, then this function will be called. If isPerson returns Err, then isOlderThan2 and isJason functions won't even execute, and the err msg would be 'Not a person'
  })
Result.mapErr

You can also modify errors that may return from any result before getting the final result, by using .mapErr or .chainErr.

import { Err } from "pratica"

Err("Message:")
  .mapErr((x) => x + " Syntax Error")
  .map((x) => x + 7) // ignored because it's an error
  .cata({
    Ok: (x) => console.log(x), // function not ran
    Err: (x) => console.log(x), // 'Message: Syntax Error'
  })
Result.chainErr
import { Err } from "pratica"

Err("Message:")
  .chainErr((x) => x + Err(" Syntax Error"))
  .map((x) => x + 7) // ignored because it's an error
  .cata({
    Ok: (x) => console.log(x), // function not ran
    Err: (x) => console.log(x), // 'Message: Syntax Error'
  })
Result.swap

Use .swap() to convert an Err to an Ok, or an Ok to an Err.

import { Ok } from "pratica"

Ok("hello")
  .swap()
  .cata({
    Ok: () => console.log(`doesn't run`),
    Err: (x) => expect(x).toBe("hello"), // true
  })
Result.bimap

Use .bimap() for easily modifying an Ok or an Err. Shorthand for providing both .map and .mapErr

import { Ok } from "pratica"

Ok("hello")
  .bimap(
    (x) => x + " world",
    (x) => x + " goodbye",
  )
  .cata({
    Ok: (x) => expect(x).toBe("hello world"), // true
    Err: () => {},
  })

Err("hello")
  .bimap(
    (x) => x + " world",
    (x) => x + " goodbye",
  )
  .cata({
    Ok: () => {},
    Err: (x) => expect(x).toBe("hello goodbye"), // true
  })
Result.ap
import { Ok } from "pratica"

// Need something like this
// Ok(6) + Ok(7) = Ok(13)
Ok
View on GitHub
GitHub Stars487
CategoryDevelopment
Updated1mo ago
Forks19

Languages

TypeScript

Security Score

100/100

Audited on Feb 24, 2026

No findings