npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazyarray-lite

v0.2.1

Published

A small lazy array class implementation.

Downloads

6

Readme

lazyarray-lite

A small lazy array class implementation.

Instead of generating a potentially huge array of objects (permutations, subsets, or whatever) only the required items are generated. Items that are not used by the program will never be generated and do not waste time and space in them

Version

0.2.1

Installation

$ npm install lazyarray-lite

Example

var LazyArray = require('lazyarray-lite')

var la = new LazyArray(function (i) {
    return 2 * i
}) // 0, 2, 4, 6, 8...

la.slice(2, 5) // [2, 6, 8]
la.get(5) // 10

API

constructor (options)

Create a new lazy array data type.

options
  • Type: Object

    Options to pass to lazy array constructor

  • Type: Function

    Syntactic sugar for options.get.

    Example:

    var la = new LazyArray(function (position) {
        return position * position
    }) // 0, 1, 4, 9, 16, 25...
    

    For more information see option.get.

options.get(position)

Type: Function

Callback that define the formula to get element of lazy array based on its position.

Example:

var la = new LazyArray({
    get: function (position) {
        return position * position
    }
}) // 0, 1, 4, 9, 16, 25...

It returns element of lazy array or undefined. If options.get(position) returns undefined, it is assumed that there are not more elements and lazy array is finite and maximum length is position.

options.next(predecessors...)

Type: Function

When is not possible to define element of lazy array based on its position, options.next define the formula to get the value based on its predecessors.

options.next must be used with some integer index option that defines the first values of the lazy array.

Example:

// odd list
la = new LazyArray({
    0: 1,
    next: function (n) {
        return n + 2
    }
}) // 1, 3, 5, 7, 9, 11...

or

// Fibonacci list
la = new LazyArray({
    0: 0,
    1: 1,
    next: function (a, b) {
        return a + b
    }
}) // 0, 1, 1, 2, 3, 5, 8, 13...

It returns element of lazy array or undefined. If options.next returns undefined it is assumed that there are not more elements and lazy array is finite.

options.{integer index}

Type: !== undefined

Integer indexs (0, 1, 2, 10, 234, etc) correspond to elements of lazy array that are computed before to get for first time.

options.init

Type: Function

There are sequences that can not be expressed with a formula or based on its predecessors. It possible to use extra values in options.next with this context and initialize this values in options.init.

Example:

// triangular sequence
la = new LazyArray({
    0: 0,
    init: function () {
        this.value = 0
    },
    next: function (n) {
        this.value++
        return n + this.value
    }
}) // 0, 1, 3, 6, 10, 15, 21...

or

// Fibonacci list
la = new LazyArray({
    init: function () {
        this.nextToCheck = 2
    },
    next: function () {
        var toCheck = this.nextToCheck
        while (!isPrime(toCheck)) {
            toCheck++
        }
        this.nextToCheck = toCheck + 1
        return toCheck
    }
}) // 2, 3, 5, 7, 11, 13, 17, 19...

function isPrime (num) {
    var top = Math.floor(Math.sqrt(num))
    for (i = 2; i <= top; ++i) {
        if (num % i === 0) {
            break
        }
    }
    return i > top
}
options.maxLength

Type: Integer?

It defines the maximum length of lazy array. If options.maxLength is undefined or not defined, the lazy array created is infinity in potencial.

Example:

var la = new LazyArray({
    get: function (position) {
        return position * position
    },
    maxLength: 5
}) // 0, 1, 4, 9, 16. (STOP)

la.get(2) // 4
la.get(6) // undefined
Advice:

It is advisable to use options.get on constructor whenever possible because it has a higher performance. Using options.next property function, to get an array element before is necessary to compute all its predecessors.

.get (index)

returns the specified element from a lazy array.

index

Type: Integer

zero-based index to get element of lazy array.

Caution: This method uses memoization. Modify returned element of lazy array can produce unexpected behaviours.

.slice(begin, end)

returns a portion of a lazy array into a new array object.

begin

Type: Integer

zero-based index at which to begin extraction

end

Type: Integer

zero-based index at which to end extraction.

Caution: This method uses memoization. Modify returned elements of lazy array can produce unexpected behaviours.

.maxLength

Type: Integer | +Infinity

gets max length of lazy array.

Development

Want to contribute? Great!

https://github.com/xgbuils/lazyarray-lite

Todo's

...

License


MIT