ts-hs

1.9.0 • Public • Published

ts-hs

Lazy evaluated functions written in TypeScript, and inspired by Haskell.

In programming language theory, lazy evaluation, or call-by-need, is an evaluation strategy which delays the evaluation of an expression until its value is needed (non-strict evaluation) and which also avoids repeated evaluations (sharing).

The benefits of lazy evaluation include:

  • The ability to define control flow (structures) as abstractions instead of primitives.
  • The ability to define potentially infinite data structures. This allows for more straightforward implementation of some algorithms.
  • The ability to define partially-defined data structures where some elements are errors. This allows for rapid prototyping

source

How to use

In a project

$ npm i ts-hs

On its own

$ git clone git@github.com:kerron/ts-hs.git
$ cd ts-hs
$ npm install
$ npm run dev

Example

Lazy Sum

/**
 * This function uses lazy evaluation.
 * 1 + 5 is not evaluated until it is called within the lazy sum fn.
 * That because the arguments to `sum` are functions, which are invoked
 * only when needed.
 */
export const sum: TLazySum = (a, b) => {
  return () => a() + b();
};

console.log(
  sum(
    () => 1 + 5,
    () => 4
  )()
); // 10

See TLazySum type here

Package Sidebar

Install

npm i ts-hs

Weekly Downloads

0

Version

1.9.0

License

ISC

Unpacked Size

9.22 kB

Total Files

21

Last publish

Collaborators

  • kerron