# @b-jones-rfd/func-help

## 0.7.1

### Patch Changes

- 7a95c35: `IO`, `State` and `Reader` run chains of any depth without overflowing the stack, so a long chain or a recursive loop (retrying, polling, paging) is limited by memory, not the call stack.

  - **`IO`:** `run` handles `map`, `bind`, `tap`, `attempt`, `IO.traverse`, `IO.sequence` and `IO.do` at any depth, including a recursive `bind` loop and recursion through `traverse`. `attempt` captures a throw from anywhere earlier in a deep chain, wrapped as before.
  - **`State`:** `run`, `eval`, `exec` and the function `toFunction` returns handle `map`, `bind`, `get`, `put`, `modify`, `gets` and `State.do` at any depth.
  - **`Reader`:** `run` handles `map`, `bind`, `ask`, `asks`, `local` and `Reader.do` at any depth, putting back the environment when each `local` scope ends.
  - **`bind` with a monad of another kind:** if a `bind` step returns a monad of another kind, which only an untyped `Do.run`/`Do.with` or a cast can do, `run` now throws a `TypeError` naming the expected type. Before, it ran wrongly without an error or failed with an unclear one.

  Laziness, re-runs, where a throw surfaces and every signature are unchanged. `Either` and `Except` are eager, so a recursive loop through their `bind` is still plain JavaScript recursion.

## 0.7.0

### Minor Changes

- 5b5efc3: Remove `IO.fromPromise` and `StateT` before 1.0, so 1.0 doesn't promise them. `State` stays.

  **Breaking changes**

  - **`IO.fromPromise` is removed.** Use `AsyncIO.from(thunk)` for async work, or `IO.from(thunk)` if you really want an `IO<Promise<A>>`.
  - **`StateT` is removed.** Copy `src/monads/StateT.ts` from 0.6.x into your app, changing its `../types.js` and `../utils/Do.js` imports to `@b-jones-rfd/func-help`, or use `State` for the pure part and the wrapped monad for the effect. A typed `StateT` is planned after 1.0.

## 0.6.4

### Patch Changes

- 466f1bc: Add `Reader`: a computation that reads an environment (configuration or services) supplied once, at `run(env)`.

  - **`Reader`:** `Reader.of`, `Reader.ask`, `Reader.asks`, `local`, `map`, `bind`, `run` and `Reader.do`, with `[Symbol.iterator]` for `yield*`. `Reader` is contravariant in its environment (`Reader<in R, out A>`), so a reader over part of an environment fits where the whole is supplied, and `Reader.do` infers the most specific environment the block yields. `local` may also take a different outer environment, as Haskell's `withReader` does.

## 0.6.3

### Patch Changes

- bba5ae9: `sequence` and `traverse` for every type, and concurrent `AsyncIO`: combine many values into one, with tuples typed per position.

  - **`Either`:** `Either.traverse(items, f)` and `Either.sequence(items)`. The first Left stops it; `sequence`'s Left is the union of the items' Lefts, and a tuple gives a tuple.
  - **`Except`:** `Except.traverse` and `Except.sequence`. The first failure stops it; a tuple gives a tuple.
  - **`IO`:** `IO.traverse` and `IO.sequence`, lazy: the items run in order on each `run()`.
  - **`AsyncIO`:** `AsyncIO.traverse` and `AsyncIO.sequence` run the items one after another; `AsyncIO.mapConcurrently` and `AsyncIO.concurrently` run them at the same time, keep input order, reject with the first rejection, and leave no unhandled rejection.

## 0.6.2

### Patch Changes

- f463e63: Error helpers and conversions: work on the failure side, recover from failures, move between the types, and turn a throw back into data.

  - **`Either`:** `mapLeft` (Haskell's `first`), `bimap`, `catchError` (recover from a `Left`; the `Right` type stays the same) and `tap` (a side effect on a `Right`).
  - **`Except`:** `mapError` (Haskell's `withExcept`; `Error` to `Error`), `catchError`, `tap`, `Except.fromEither` (from an `Either` whose `Left` is an `Error`) and `toEither()`. `Except.tryCatch` now also captures a thrown value that can't be turned into a string (an object with no prototype, or one whose `toString` throws), where before it threw.
  - **`IO`:** `attempt()` gives an `IO<Except<A>>`: a throw anywhere earlier in the chain becomes a failure, and `run()` never throws (Haskell's `try`).
  - **`AsyncIO`:** `AsyncIO.fromIO` (lazy; the IO runs on each run), `AsyncIO.fromExcept` (a failure rejects with its `Error`) and `attempt()`, whose `run()` never rejects.

## 0.6.1

### Patch Changes

- 514ed4d: Typed do notation: bound values get their real types and each block returns the concrete monad, with no casts.

  - **`Either.do`:** a `do` block over `Either`. `const x = yield* m` binds the Right value; the first Left stops the block. The Left type is the union of the Left types the block yields.
  - **`Except.do`:** a `do` block over `Except`; the first failure stops the block.
  - **`IO.do`:** a `do` block as an `IO`. Nothing runs until `run()`, and each run starts the block afresh.
  - **`AsyncIO.do`:** a `do` block as an `AsyncIO`, written as a plain generator function; each `yield*` waits for its step.
  - **`State.do`:** a `do` block as a `State`, with the state type inferred from the States it yields.
  - **`StateT.do`:** a `do` block as a `StateT`; takes the wrapped monad's `of`, like every `StateT` constructor.
  - **`[Symbol.iterator]`** on `Either`, `Except`, `IO`, `AsyncIO`, `State` and `StateT`: what lets `yield*` bind them. Spreading one gives a one-element array holding the instance.
  - **`Do.run` / `Do.with`:** for a monad of your own. `yield*` on a monad with the same `[Symbol.iterator]` gives a typed binding, and existing plain `yield` blocks work as before.

## 0.6.0

### Minor Changes

- e85184a: Keep one name for each operation, Haskell's: `bind` for `>>=` and `State.of` for `pure`.

  - **`bind`** is now a method on the prototype of `Either`, `Except`, `IO`, `AsyncIO`, `State` and `StateT`, rather than a property copied onto every instance. It no longer appears in `Object.keys` or in a spread of an instance.
  - **`Monad<T>`** declares `map` and `bind`.

  **Breaking changes**

  - **`flatMap` is removed** from `Either`, `Except`, `IO`, `AsyncIO`, `State` and `StateT` (deprecated in 0.5.2). Rename each call to `bind`, which behaves the same.
  - **`State.return` is removed** (deprecated in 0.5.2). Use `State.of`.
  - **`Monad<T>.flatMap` is renamed `bind`.** If you pass a monad of your own to `Do` or `StateT`, rename its `flatMap` method to `bind`.

## 0.5.2

### Patch Changes

- 683c624: Deprecate `flatMap` and `State.return` in favour of their Haskell names. Both are removed in 0.6.0.

  - **`flatMap`** on `Either`, `Except`, `IO`, `AsyncIO`, `State` and `StateT` is deprecated. Use `bind`, which is the same method (Haskell's `>>=`).
  - **`State.return`** is deprecated. Use `State.of` (Haskell's `pure`).
  - **`Monad<T>`:** in 0.6.0 the interface's `flatMap` member is renamed `bind`. If you pass your own monad to `Do` or `StateT`, give it a `bind` method then.

## 0.5.1

### Patch Changes

- 51a1098: Document every export, and rewrite the README.

  - **README:** new sections for Requirements (Node 22.12, the TypeScript window, browsers), Quick start, Usage (one section per module) and API (one table per module). The description names Haskell as the prior art and links to tea-set.
  - **API reference:** every class, static and method has a summary, parameter and return docs, an example, and a category, so the generated reference is grouped by module. Where a name differs from Haskell's, the docs say so.
  - **`Do` and `StateT`:** the docs and examples show the cast to the concrete monad that callers need, because `Do`'s result and `StateT.run` are typed as the `Monad` interface.

## 0.5.0

### Minor Changes

- b5c69e9: Fix bugs in `AsyncIO`, `Do`, `State` and `Except`, and tighten the types of `Either.tryCatch` and `Except.match`.

  - **`AsyncIO.tap`:** a rejected effect no longer causes an unhandled rejection. If the side effect throws, the promise returned by `run()` rejects with that error.
  - **`AsyncIO.run`:** never throws synchronously. An effect that throws before returning a promise now rejects the promise `run()` returns.
  - **`Do`:** a do-block over a lazy monad (`IO`, `AsyncIO`, `State`) runs none of its code until it is run, and gives the same result every time it is run. Before, the code up to the first `yield` ran when the block was built, and a second run returned `undefined`.
  - **`State.of`:** works when passed as a function, for example `Do.with(State.of)`.
  - **`Except`:** success and failure now follow the constructor used, not `instanceof Error`. `Except.ok(error)` is a success. An error from another realm (an iframe) passed to `failed` is a failure. `tryCatch` without `onError` wraps a thrown non-Error in an `Error`, keeping the thrown value as `cause`.
  - **Docs:** each method that runs your function now says where an error it throws surfaces.

  **Breaking changes**

  - **`Either.tryCatch`:** without `onError`, the Left is typed `unknown`. A call that names the error type without converting it, such as `Either.tryCatch<number, MyError>(fn)`, no longer compiles. Pass `onError` to convert the thrown value instead.
  - **`Except.match`:** it now takes a handlers object. Change `except.match(onError, onOk)` to `except.match({ failed: onError, ok: onOk })`, or use `except.fold(onError, onOk)`.

## 0.4.0

### Minor Changes

- 9cd9911: Publish as an ES module only, and require Node 22.12 or later.

  - **ESM only:** the CommonJS build and the `module` field are gone. The package is `"type": "module"` with an `exports` map. On Node 22.12 and later, CommonJS code can still `require()` it.
  - **Node 22.12 or later:** `engines.node` is now `>=22.12.0`. CI tests 22.12.0 and the latest 22, 24 and 26.
  - **Browsers:** the test suite now also runs in Chromium and WebKit, and browsers with Baseline Widely available features are supported.
  - **Types:** the published declarations are checked against the oldest TypeScript in the two-year support window (5.8 today).
  - **Provenance:** releases are published from GitHub Actions with npm provenance.

## 0.3.1

### Patch Changes

- ab1e7ce: Update deployment config

## 0.3.0

### Minor Changes

- f0ecf47: Added AsyncIO monad

## 0.2.1

### Patch Changes

- 7e2c0cd: Add AsyncIO monad

## 0.2.0

### Minor Changes

- c536156: Added Except monad

## 0.1.1

### Patch Changes

- 0644223: Fix test error

## 0.1.0

### Minor Changes

- c3e8bf7: Initial implementation

## 0.0.1

### Patch Changes

- ca43bdf: Publish to NPM
