Coder Social home page Coder Social logo

junq's Introduction

JUNQ - A library for doing lazy operations on collections in JavaScript

Junq is a library for doing operations like enumerating, mapping, filtering on collections. The peculiarity is that it does it without creating new collections (arrays) every time an operations is performed. For example you can safely run the following:

junq.range(Number.POSITIVE_INFINITY)
    .where(function(n){
        return n % 2 === 1;
    })
    .map(function(n){
        return n*n;
    })
    .contains(81);

The range() method will generate an enumerator (also called iterator in some languages) from 0 to infinity. The enumerator will provide a new element every time it is requested. Since the enumerations is stopped as soon as the value 81 is found, the first enumerator will only have to iterate 10 times (from 0 to 9).

You could even store the query in a variable and use it later on even different times:

var q = junq.range(Number.POSITIVE_INFINITY)
    .where(function(n){
        return n % 2 === 1;
    })
    .map(function(n){
        return n*n;
    });

    q.contains(81)
    true
    q.contains(121)
    true

Junq will enumerate on Arrays, other enumerables (see later), or anything object with a length property. The simplest way tu use junq is to create en enumerable object (a junqy) by calling the junq() function, like this:

junq([1,2,3,4,5])

Subsequent operations are performed on the object returned by the previous one, in a fluent way:

junq([1,2,3,4,5]).max();

This is a functional oriented library, so the majority of methods takes a function as argument:

junq([1,2,3,4,5]).forEach(function(n){
    console.log(n.toString());
});

Creating custom enumerables

An enumerator is any object that implements a method

getEnumerator()

This method should return an enumerator object, which is any object that implements the following methods:

moveNext()
getCurrent()

junq's People

Contributors

canna71 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.