# fl-generators

> Fantasy-land compatible abstraction for JavaScript Generators

Latest version **1.0.0** (published 2017-04-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install fl-generators
pnpm add fl-generators
yarn add fl-generators
bun add fl-generators
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2017-04-23 |
| First published | 2017-04-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | functorism |

## Links

- npm: https://www.npmjs.com/package/fl-generators
- Repository: https://github.com/functorism/fl-generators
- Issues: https://github.com/functorism/fl-generators/issues
- npm.io page: https://npm.io/package/fl-generators

## Recent versions

- 1.0.0 (latest) — 2017-04-23

## README

# :fuelpump: fl-generators

<img align="right" src="https://raw.githubusercontent.com/puffnfresh/fantasy-land/master/logo.png"/>

`fl-generators` provides a declarative API for creating generators.

``` javascript
const Iter = require('fl-generators');

tunedPianos = Iter.of(instruments).filter(isPiano).map(tune);
```
Is equivalent to:
``` javascript
const filter = function*(it, g) {
  for (const x of it()) {
    if (g(x)) {
      yield x;
    }
  }
}

const map = function*(it, g) {
  for (const x of it()) {
    yield g(x);
  }
}

tunedPianos = map(filter(instruments, isPiano), tune);
```

## :warning: Notice

Generators are not optimized in V8 or SpiderMonkey. Using generator functions is significantly slower in most situations.

* [v8-users - Is there intent to allow optimization of generator functions?](https://groups.google.com/forum/#!topic/v8-users/KnnUb-u4rA8)
* [wingolog - generators in firefox now twenty-two times faster](https://wingolog.org/archives/2014/11/14/generators-in-firefox-now-twenty-two-times-faster)

# Iter methods

* cache
* concatAll
* concat
* cycle
* do
* filter
* flatMap
* map
* reduce
* repeat
* run
* scan
* skip
* skipWhile
* slice
* take
* takeWhile
* toArray
* zip

# Examples

## 
``` javascript
const Iter = require('fl-generators');

Iter.range(0, 6)
  .filter(n => n % 2 === 0)
  .map(n => n + 1)
  .flatMap(n => [n - 1, n, n + 1])
  .reduce((x, y) => x + y, 0);

// filter 0
// map 0
// flatMap 1
// filter 1
// filter 2
// map 2
// flatMap 3
// filter 3
// filter 4
// map 4
// flatMap 5
// filter 5
// filter 6
// map 6
// flatMap 7
// reduce 48
```

``` javascript
Array.from({ length: 7 }, (_, i) => i)
  .filter(n => n % 2 === 0)
  .map(n => n + 1)
  .reduce((c, n) =>  c.concat([n - 1, n, n + 1]), [])
  .reduce((x, y) => x + y, 0);

// filter 0
// filter 1
// filter 2
// filter 3
// filter 4
// filter 5
// filter 6
// map 0
// map 2
// map 4
// map 6
// flatMap 1
// flatMap 3
// flatMap 5
// flatMap 7
// reduce 48
```

---
_Source: https://npm.io/package/fl-generators · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
