2.1.0 • Published 3 years ago

lazzy.ts v2.1.0

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

GitHub Workflow Status npm GitHub release (latest by date including pre-releases) GitHub GitHub repo size npm bundle size

Installation

You can use one of the following package managers:

  • Node package manager npm:
npm install lazzy.ts
  • Yarn package manager yarn:
yarn add lazzy.ts

Try it online!

You can use codesandbox.io to try Lazzy.ts.

Follow these steps to create a new project:

  • Click on the link above;
  • Click on the 'Create Sandbox' button;
  • Choose 'Vanilla Typescript';
  • In the left sidebar, we have a section called 'Dependencies'. Type 'lazzy.ts' in the input field 'Add Dependency' and hit enter to add the Lazzy.ts library.
  • Delete the autogenerated code in the index.ts file.

Now you can try all the examples shown here. Just copy one of the examples and paste it in your newly created project.

How to use it?

You have several options:

  • you can import the entire Lazy object which combines all functions in one place and gives the ability to chain them together:
import Lazy from "lazzy.ts";

const sum = Lazy.from([1, 2, 3, 4, 5, 6])
  .map(n => n * 3)
  .filter(n => n % 2 === 0)
  .sum();

console.log(sum); // 36
  • or you can import each function separately and use them without chaining:
import { toLazy, map, filter, sum } from "lazzy.ts";

// Without chaining:
const lazy = toLazy([1, 2, 3, 4, 5, 6]);
const transformed = map(lazy, n => n * 3);
const filtered = filter(transformed, n => n % 2 === 0);
const result = sum(filtered);

console.log(result); // 36
import { toLazy, map, filter, sum } from "lazzy.ts";

// Nested:
const result = sum(
    filter(
        map(toLazy([1, 2, 3, 4, 5, 6]), (n) => n * 3),
        (n) => n % 2 === 0
    )
);

console.log(result); // 36
  • or you can combine them:
import Lazy, { filter, sum } from "lazzy.ts";

const iterator = Lazy.from([1, 2, 3, 4, 5, 6])
  .map(n => n * 3)
  .toIterator();

const result = sum(filter(iterator, n => n % 2 === 0));

console.log(result); // 36

As you can see we can achieve the same thing with three different approaches.

Introduction

What is lazy? What does this mean?

Let's see one simple example to illustrate what lazy is and why it's so useful and powerful.

So let's assume that we have an array of numbers and we want to multiply each number by 3, then we want to filter only the even numbers and finally to take the total sum of the even numbers.

In a traditional JavaScript this logic will look like this:

const source = [1, 2, 3, 4];
const result = source
    .map(n => n * 3)
    .filter(n => n % 2 === 0)
    .reduce((prev, next) => prev + next, 0);

console.log(result); // 18

With lazzy.ts it will look like this:

import Lazy from "lazzy.ts";

const source = [1, 2, 3, 4];
const result = Lazy.from(source)
    .map(n => n * 3)
    .filter(n => n % 2 === 0)
    .sum();

console.log(result); // 18

So what is the difference? The result is the same, right? Why do we need this lazzy.ts library?

The answer is very simple. Because it is Lazy. In the first example each function (map, filter and reduce) produces a new array or value, which means that we will iterate through each array. In this case we have the initial array and two newly created arrays (one from the 'map' function and one from the 'filter' function) and finally we will produce a new value with the 'reduce' function. This is very expensive! Don't do this! You can do it better!

With the lazzy.ts example we will iterate only once through the initial array and we will apply all operations for each value. This means that we will not produce new arrays. We will produce only the final result, which is 18 in this case. This is much better, right?

So let's rewrite these two examples with 'for' loops to see what happens under the hood:

// The first example will execute something like this
const source = [1, 2, 3, 4];

const resultAfterMap = [];                            // map
for (let i = 0; i < source.length; i++) {
    resultAfterMap.push(source[i] * 3);
}

const resultAfterFilter = [];                         // filter
for (let i = 0; i < resultAfterMap.length; i++) {
    if (resultAfterMap[i] % 2 === 0) {
        resultAfterFilter.push(resultAfterMap[i]);
    }
}

let result = 0;                                       // sum
for (let i = 0; i < resultAfterFilter.length; i++) {
    result += resultAfterFilter[i];
}

console.log(result); // 18

So we have two newly created arrays and three 'for' loops.

// The second example will execute something like this
const source = [1, 2, 3, 4];

let result = 0;
for (let i = 0; i < source.length; i++) {
    const newValue = source[i] * 3;       // map
    if (newValue % 2 === 0) {             // filter
        result += newValue;               // sum
    }
}

console.log(result); // 18

In this case we will iterate only once through the initial array and we will produce only the final result, without using any extra memory. It is much much better!

API Reference

We have a bunch of lazy functions that you can see here!

Generators

Consumers

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT