lazy-thunk
Version:
A set of data structures in TypeScript which utilizes lazy evaluation
65 lines (45 loc) • 1.74 kB
Markdown
//github.com/J3m3/lazy-thunk/actions/workflows/ci.yml/badge.svg)](https://github.com/J3m3/lazy-thunk/actions/workflows/ci.yml)
> A set of data structures in TypeScript which utilizes lazy evaluation (w/o 3rd party dependencies!)
`lazy-thunk` emulates lazy evaluation simply by wrapping expressions into a function.
## Simple Examples
```ts
import { type Thunk, toThunk } from "lazy-thunk";
import * as LL from "lazy-thunk/LinkedList";
// an infinite list of even numbers
const isEven = (n: Thunk<number>) => n() % 2 === 0;
const evens = LL.filter(isEven, LL.range(1));
const xs = LL.unsafeToArray(LL.take(5, evens)); // [2, 4, 6, 8, 10]
// an infinite list of prime numbers
const sieve = (xs: LL.LazyList<Thunk<number>>): LL.LazyList<Thunk<number>> => {
return () => {
const node = xs();
if (node === null) {
return null;
}
const h = node.head;
return {
head: h,
rest: sieve(LL.filter((n) => n() % h() !== 0, node.rest)),
};
};
};
const primes = sieve(LL.range(2));
const ys = LL.unsafeToArray(LL.take(5, primes)); // [2, 3, 5, 7, 11]
```
```console
npm i lazy-thunk
```
TODO
A [`thunk`](https://wiki.haskell.org/Thunk) is a term used in Haskell, which denotes value that is yet to be evaluated.
> [!IMPORTANT]
> lazy-thunk does not strictly emulates `thunk` in Haskell. Haskell evaluates expressions up to [WHNF (Weak Head Normal Form)](https://wiki.haskell.org/Weak_head_normal_form).
- HaskellWiki: https://wiki.haskell.org/
- Basic idea comes from: https://youtu.be/E5yAoMaVCp0?si=slYqwH_GDHVTm-Ty
This project is licensed under the terms of the [MIT License](LICENSE).
[![CI](https: