fp-ts: Support for lazy evaluation of large/infinite lists

๐Ÿš€ Feature request

Support for working with generators and iterators without casting them to array to force evaluation.

Current Behavior

Currently fp-ts only supports fully evaluated arrays which means limits itโ€™s use to small arrays. It is often desirable to defined huge structures, while not all the results of that structure are needed.

Desired Behavior

import { pipe } from 'fp-ts/lib/pipeable'
import { map, take } from 'fp-ts/lib/Iterator'
import { enumFrom } from 'fp-ts/lib/Enumeration'

const evenNumbers = pipe(
  enumFrom(0),
  map((x) => 2 * x),
);
expect(pipe(
  evenNumbers,
  take(4),
), [0, 2, 4, 6])

Suggested Solution

Implement new Enumeration and Iterator modules.

Who does this impact? Who is this for?

For everyone. Using iterators makes defining things easier.

Describe alternatives youโ€™ve considered

It is possible to work around this by sacrificing generality. For example one could create a function that specifically calculates the first 4 even numbers without relaying on a general definition of even numbers.

Additional context

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators https://docs.python.org/3/library/itertools.html https://www.futurelearn.com/courses/functional-programming-haskell/0/steps/27237 http://zvon.org/other/haskell/Outputprelude/enumFromThenTo_f.html

Your environment

I tend to use latest versions of fp-ts and TypeScript where possible.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 16 (10 by maintainers)

Most upvoted comments

I still use the code I linked but it has moved to https://github.com/maasglobal/scrapql/blob/master/src/utils/negf.ts https://github.com/maasglobal/scrapql/blob/master/src/utils/__tests__/negf.ts

However, it still more of a temporary hack/experiment and I donโ€™t have intention to develop it much further. I think it would be useful to have separate types for Nonempty generator. generator and infinite generator. Also there is need for utilities for converting between arrays and generators.