Skip to content

Repository files navigation

Functors banner - the functors mascot generated by dall-e 2

@ortense/functors

install sizeCoverage Status

A collection of dependency-free functors written in TypeScript, created to be type-safe, immutable, and lightweight.

What is a Functor?

In functional programming, a functor is a concept that represents a type with the ability to be mapped over. This means that you can apply a function to the values inside the type, without altering the structure of the type itself. Functors provide a powerful abstraction for building composable and reusable code, promoting a declarative and functional style of programming.

functor chart - made in excalidraw.com

Overview

This project is a TypeScript library that embraces the functor concept, offering implementations of common functors. These functors provide useful abstractions for various scenarios in your application, allowing you to write more expressive and maintainable code.

Install

Pick your favorite package manager.

npm install @ortense/functors # npm
yarn add @ortense/functors # yarn
pnpm add @ortense/functors # pnpm
bun add @ortense/functors # bun
deno add @ortense/functors # deno from jsr.io

Implemented Functors

For detailed documentation, please refer to the official documentation.

Lazy

Represents a lazy-evaluated value, allowing computations to be deferred until needed.

import{lazy}from'@ortense/functors'constdouble=(value: number)=>{console.log('Doubling...')returnvalue*2}constlazyDouble=lazy(()=>7).map(double).map(double)constresult=lazyDouble.evaluate()console.log(result)// Output:// Doubling...// Doubling...// 42

History

Represents a history of values, enabling operations like mapping, resetting, and rolling back to previous states.

import{history}from'@ortense/functors'constuserHistory=history({name: 'Jane Doe'}).map(user=>({ ...user,id: 1})).map(({ id })=>({ id,name: 'Jonh Due'}))console.log(userHistory.current())// { id: 1, name: 'Jonh Due' }console.log(userHistory.rollback().current())// { id: 1, name: 'Jane Doe' }console.log(userHistory.reset().current())// { name: 'Jane Doe' }

Either

Represents a type that can be either of type L or R, providing a mechanism for branching based on success or failure.

import{Either,left,right}from'@ortense/functors'constdivide=(numerator: number,denominator: number,): Either<Error,number>=>{if(Number.isNaN(numerator)){returnleft(newError('Numerator is not a number.'))}if(Number.isNaN(denominator)){returnleft(newError('Denominator is not a number.'))}if(denominator===0){returnleft(newError('Division by zero is not posible.'))}returnright(numerator/denominator)}constnumerator=Number(document.querySelector('input#numerator').value)constdenominator=Number(document.querySelector('input#denominator').value)constdisplay=document.querySelector('div#display-result')divide(numerator,denominator).right(result=>{display.textContent=`${numerator} / ${denominator} = ${result}`}).left(error=>{display.textContent=error.message})

Maybe

Represents a type that may contain a value or be empty, helping to handle null or undefined values.

import{Maybe,maybe}from'@ortense/functors'typeUser={id: string;name: string}constfindUserById=async(id: string): Maybe<User>=>{constuser=awaitdb.users.findByID(id)// suppose that return user or nullreturnmaybe<User>(user)};constuserName=findUserById('123').map(user=>user.name)// Maybe<string>.mapEmpty(()=>'Default Name').unwrap()console.log(userName)// Output: Default Name (if user not found)

Contribution

Contributions are welcome! Feel free to open issues, submit pull requests, or provide feedback to help improve this library.

License

This library is licensed under the MIT License - see the LICENSE file for details.

About

A collection of dependency-free functors written in TypeScript, created to be type-safe, immutable, and lightweight.

Topics

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages