2.0.5 • Published 4 years ago

lazily-async v2.0.5

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

lazily-async

Implements common array methods in a lazy manner. Nothing more.

There's a synchronous version of this library as well, called lazily. See https://github.com/isotropy/lazily

Installation

npm install lazily-async

Create a sequence

import { Seq } from "lazily-async";

const seq = Seq.of([1, 2, 3])
for await (const i of seq) {
  console.log(i)
}

It's lazy

Sequences are lazy. For example, in the following example only one map() action is performed irrespective of the length of the sequence.

const seq = await Seq.of([1, 2, 3])
  .map(async x => x * 2)
  .first();

toArray()

await Seq.of([1, 2, 3])
  .toArray()
// [1, 2, 3]

map(fn)

await Seq.of([1, 2, 3])
  .map(async x => x * 2)
  .toArray()
// [2, 4, 6]

flatMap(fn)

await Seq.of([1, 2, 3])
  .flatMap(async x => [x*10, x*20])
  .toArray()
// [11, 21, 12, 22, 13, 23]

filter(predicate)

await Seq.of([1, 2, 3, 4])
  .filter(async x => x > 2)
  .toArray()
//[3, 4]

exit(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .exit(async x => x > 3)
  .toArray()
// [1, 2, 3]

exitAfter(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .exitAfter(async x => x > 3)
  .toArray()
// [1, 2, 3, 4]

find(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .find(x => x * 10 === 30)
// 3

reduce(fn)

await Seq.of([1, 2, 3, 4, 5])
  .reduce(async (acc, x) => acc + x, 0)
// 15

short-circuited reduce(fn, initialValue, stopPredicate)

await Seq.of([1, 2, 3, 4, 5])
  .reduce(async (acc, x) => acc + x, 0, async acc => acc > 6)
// 10

first()

await Seq.of([1, 2, 3, 4, 5])
  .first();
// 1

first(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .first(async x => x > 3);
// 4

last()

await Seq.of([1, 2, 3, 4, 5])
  .last();
// 5

last(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .last(async x => x < 3);
// 2

every(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .every(async x => x <= 5);
// true

some(predicate)

await Seq.of([1, 2, 3, 4, 5])
  .some(x => x === 3);
// true

includes(item)

await Seq.of([1, 2, 3, 4, 5])
  .includes(3);
// true

concat(seq)

await Seq.of([1, 2, 3, 4, 5])
  .concat(Seq.of([6, 7, 8)]))
  .toArray();
// [1, 2, 3, 4, 5, 6, 7, 8]

reverse()

await Seq.of([1, 2, 3, 4, 5])
  .reverse()
  .toArray();
// [5, 4, 3, 2, 1]

slice(begin, end)

await Seq.of([1, 2, 3, 4, 5])
  .slice(2, 4)
  .toArray();
// [3, 4, 5]
2.0.5

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.33

7 years ago

0.0.32

7 years ago

0.0.31

8 years ago

0.0.30

8 years ago

0.0.29

8 years ago

0.0.28

8 years ago

0.0.27

8 years ago

0.0.26

8 years ago

0.0.25

8 years ago

0.0.24

8 years ago

0.0.23

8 years ago

0.0.22

8 years ago

0.0.21

8 years ago

0.0.20

8 years ago

0.0.19

8 years ago

0.0.18

8 years ago

0.0.17

8 years ago

0.0.16

8 years ago

0.0.15

8 years ago

0.0.14

8 years ago

0.0.13

8 years ago

0.0.12

8 years ago

0.0.10

8 years ago

0.0.9

8 years ago

0.0.8

8 years ago

0.0.7

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago