# Functional Programming Library Documentation
## Table of Contents
1. [Overview](#overview)
2. [Either Module](#either-module)
3. [List Module](#list-module)
4. [Option Module](#option-module)
5. [Utility Module](#utility-module)
6. [Bun Extensions](#bun-extensions)
## Overview
This zero-dependency functional programming library provides type-safe implementations of common FP constructs optimized for Bun runtime. Core features include:
- `Either` for error handling
- `Option` for nullable values
- Immutable linked `List`
- Do-notation for monadic composition
- Bun-specific file and environment utilities
## Either Module
Represents computations that can succeed (`Right`) or fail (`Left`).
### Types
```typescript
type Left = { readonly _tag: 'Left'; readonly left: E }
type Right = { readonly _tag: 'Right'; readonly right: A }
type Either = Left | Right
```
### Constructors
| Function | Description | Example |
|----------|-------------|---------|
| `left(e: E)` | Creates failure case | `left('error')` |
| `right(a: A)` | Creates success case | `right(42)` |
### Operations
| Function | Description | Type Signature |
|----------|-------------|----------------|
| `eitherMap` | Maps over success value | `(f: (a: A) => B) => (fa: Either) => Either` |
| `eitherChain` | Chains Either-returning functions | `(f: (a: A) => Either) => (fa: Either) => Either` |
| `fold` | Handles both cases | `(onLeft: (e: E) => B, onRight: (a: A) => B) => (fa: Either) => B` |
### Do Notation
```typescript
const result = DoEither.start<{ x: number }, string>()
.bind('x', right(5))
.bind('y', right(10))
.return(scope => scope.x + scope.y)
// Returns Either
```
| Method | Description |
|--------|-------------|
| `bind(key, either)` | Binds named value from Either |
| `return(f)` | Transforms final scope |
| `do(either)` | Executes effect without binding |
| `doL(f)` | Executes scope-dependent effect |
## List Module
Immutable linked list implementation.
### Types
```typescript
type Nil = { readonly _tag: 'Nil' }
type Cons = {
readonly _tag: 'Cons'
readonly head: A
readonly tail: List
}
type List = Nil | Cons
```
### Constructors
| Function | Description | Example |
|----------|-------------|---------|
| `nil` | Empty list | `nil` |
| `cons` | Creates list node | `cons(1, cons(2, nil))` |
| `fromArray` | Converts array to list | `fromArray([1, 2, 3])` |
### Operations
| Function | Description | Type Signature |
|----------|-------------|----------------|
| `listMap` | Transforms list elements | `(f: (a: A) => B) => (fa: List) => List` |
| `listReduce` | Reduces list to value | `(f: (b: B, a: A) => B, initial: B) => (fa: List) => B` |
| `toArray` | Converts list to array | `(fa: List) => Array` |
## Option Module
Represents optional values (`Some` or `None`).
### Types
```typescript
type None = { readonly _tag: 'None' }
type Some = { readonly _tag: 'Some'; readonly value: A }
type Option = None | Some
```
### Constructors
| Function | Description | Example |
|----------|-------------|---------|
| `none` | Empty value | `none` |
| `some` | Wraps a value | `some(42)` |
| `fromNullable` | Converts nullable | `fromNullable(null) → none` |
### Operations
| Function | Description | Type Signature |
|----------|-------------|----------------|
| `map` | Transforms value | `(f: (a: A) => B) => (fa: Option) => Option` |
| `chain` | Chains computations | `(f: (a: A) => Option) => (fa: Option) => Option` |
| `getOrElse` | Default value | `(defaultValue: A) => (fa: Option) => A` |
### Do Notation (Sync)
```typescript
DoOption.start()
.bind('user', getUser())
.bind('profile', getProfile())
.return(scope => `${scope.user.name}: ${scope.profile.bio}`)
```
### Async Do Notation
```typescript
AsyncDoOption.start()
.bind('user', fetchUser())
.bindL('posts', scope => fetchPosts(scope.user.id))
.return(scope => renderPage(scope))
```
| Async Method | Description |
|--------------|-------------|
| `bind(key, promise)` | Binds async Option |
| `bindL(key, f)` | Binds scope-dependent async Option |
| `do(promise)` | Executes async effect |
| `doL(f)` | Executes scope-dependent async effect |
## Utility Module
### Function Composition
| Function | Description | Example |
|----------|-------------|---------|
| `pipe` | Left-to-right composition | `pipe(1, x => x+1, x => x*2) → 4` |
| `compose` | Right-to-left composition | `compose(x => x*2, x => x+1)(1) → 4` |
### Error Handling
| Function | Description | Example |
|----------|-------------|---------|
| `tryCatch` | Safely execute function | `tryCatch(() => JSON.parse(str))` |
### Type Utilities
| Function | Description |
|----------|-------------|
| `hasTag` | Creates tag predicate | `list.filter(hasTag('Cons'))` |
## Bun Extensions
Optimized utilities for Bun runtime.
### File Operations
| Function | Description | Type Signature |
|----------|-------------|----------------|
| `readFileSafe` | Reads file safely | `(path: string) => Promise>` |
| `listToFile` | Writes list to file | `(list: List, path: string) => Promise>` |
### JSON & Environment
| Function | Description |
|----------|-------------|
| `safeJsonParse` | Safe JSON parsing | `safeJsonParse(data): Either` |
| `envOption` | Environment variable as Option | `envOption('PORT')` |
### Server Utilities
| Function | Description | Example |
|----------|-------------|---------|
| `createServer` | Creates HTTP server | `createServer(handleRequest)` |
| `asyncReduce` | Async list reduction | `asyncReduce(list, reducer, initial)` |
### Server Example
```typescript
const handleRequest = (req: Request): Either => {
return req.url === '/'
? right(new Response('Hello'))
: left(new Error('Not found'))
}
createServer(handleRequest)