2.0.5 • Published 3 years ago

lazily v2.0.5

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

lazily

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

There's an asynchronous version of this library as well, called lazily-async. See https://github.com/isotropy/lazily-async

Installation

npm install lazily

Create a sequence

import { Seq } from "lazily";

const seq = Seq.of([1, 2, 3])
for (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 = Seq.of([1, 2, 3])
  .map(x => x * 2)
  .first();

toArray()

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

map(fn)

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

flatMap(fn)

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

filter(predicate)

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

exit(predicate)

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

exitAfter(predicate)

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

find(predicate)

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

reduce(fn)

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

short-circuited reduce(fn, initialValue, stopPredicate)

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

first()

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

first(predicate)

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

last()

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

last(predicate)

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

every(predicate)

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

some(predicate)

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

includes(item)

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

concat(seq)

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

reverse()

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

slice(begin, end)

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

3 years ago

2.0.4

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.26

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

7 years ago

0.0.17

7 years ago

0.0.16

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago