Something went wrong. Try again.
An educational pure functional programming library in TypeScript
Something went wrong. Try again.
Files
README.md
# purus-ts
Pure functional TypeScript with typed errors, fiber concurrency, and zero dependencies.
> *"purus"* is Latin for *"pure"*
## Features
- **Branded Types** — Create distinct types from primitives (`UserId`, `Email`)
- **Refinements** — Encode validated properties in the type system (`Positive`, `NonEmpty`, `Sorted`)
- **Result & Option** — Handle errors and nullability as values, not exceptions
- **Pattern Matching** — Exhaustive, type-safe matching on discriminated unions
- **Optics** — Composable, law-abiding immutable data access (Lens, Prism, Optional, Traversal)
- **Effect System** — Composable effects with typed errors and dependency injection
- **Fiber Runtime** — Cooperative concurrency with cancellation, racing, and parallelism
- **Zero Dependencies** — Pure TypeScript library with zero runtime dependencies
## Installation
```bash
npm install purus-ts
# or
bun add purus-ts
# or
pnpm add purus-ts
```
## Quick Start
```typescript
import { pipe, ok, err, mapResult, match, succeed, flatMap, runPromise } from 'purus-ts'
// Result: errors as values
const divide = (a: number, b: number) =>
b === 0 ? err('division by zero') : ok(a / b)
const result = pipe(
divide(10, 2),
mapResult(x => x * 2)
)
match(result)({
Ok: ({ value }) => console.log(`Result: ${value}`),
Err: ({ error }) => console.log(`Error: ${error}`)
})
// Effects: async operations with typed errors
const fetchUser = pipe(
succeed({ id: 1, name: 'Alice' }),
flatMap(user => succeed(`Hello, ${user.name}!`))
)
runPromise(fetchUser).then(console.log) // "Hello, Alice!"
```
## Core Concepts
### Branded Types
Create distinct types that are incompatible at compile time:
```typescript
import { brand, type Branded } from 'purus-ts'
type UserId = Branded<string, 'UserId'>
type OrderId = Branded<string, 'OrderId'>
const userId: UserId = brand('user-123')
const orderId: OrderId = brand('order-456')
// userId = orderId // Compile error!
```
### Refinements
Encode validated properties in the type system:
```typescript
…