Git Product home page Git Product logo

array-comprehension's Introduction

Array comprehension in javascript.

Declaratively construct arrays.

comprehend(arrays..., guard_function, function)

Where 'arrays...' is any number of arrays to pull data from.

Both 'guard_function' and 'function' take the elements of each of the arrays as parameters.

The boolean returned from the 'guard_function' determines whether or not to use the data for those elements. 'guard_function' is optional.

The return value of 'function' produces each element in the resulting array.

var comprehend = require('array-comprehension');

comprehend([1,2,3,4,5,6], [4,3,2,1], function(x, y) { return x + y; });
> [5, 4, 3, 2, 6, 5, 4, 3, 7, 6, 5, 4, 8, 7, 6, 5, 9, 8, 7, 6, 10, 9, 8, 7]

comprehend(range(1, 10, 'inclusive'), function(x) { return x * 2; });
> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

comprehend(range(1, 10, 'inclusive'), function(x) { return x * 2 >= 12; }, function(x) { return x * 2; });
> [12, 14, 16, 18, 20]

var id = function(x) { return x; };
comprehend(range(50, 100, 'inclusive'), function(x) { return x % 7 == 3; }, id);
> [52, 59, 66, 73, 80, 87, 94]

comprehend(range(5, 15, 'inclusive'), function(x) {
		if (x < 10) return 'BOOM!';
		else return 'BANG!';
	});
> ["BOOM!", "BOOM!", "BOOM!", "BOOM!", "BOOM!", "BANG!", "BANG!", "BANG!", "BANG!", "BANG!", "BANG!"

comprehend([2,5,10], [8,10,11], function(x,y) { return x * y; });
> [16, 20, 22, 40, 50, 55, 80, 100, 110]

comprehend([2,5,10], [8,10,11], [1,2,3], function(x,y,z) { return x * y * z; });
> [16, 32, 48, 20, 40, 60, 22, 44, 66, 40, 80, 120, 50, 100, 150, 55, 110, 165, 80, 160, 240, 100, 200, 300, 110, 220, 330]

var nouns = ['hobo', 'frog', 'pope'];
var adjs = ['lazy', 'grouchy', 'scheming'];
comprehend(adjs, nouns, function(adj, noun) { return adj + " " + noun; });
> ["lazy hobo", "lazy frog", "lazy pope", "grouchy hobo", "grouchy frog", "grouchy pope", "scheming hobo", "scheming frog", "scheming pope"]

array-comprehension's People

Contributors

jayrbolton avatar

Watchers

James Cloos avatar Jay R. Bolton - now @jayrbolton avatar

Forkers

commitchange

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.