Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grunge

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grunge - npm Package Compare versions

Comparing version 0.0.6 to 0.1.0

139

index.js

@@ -18,7 +18,9 @@ (function(root, factory){

var Grunge = function(start, step){
// only pass in x when step is a function
// pass in x for memory concerns if you don't always need the whole array.
var Grunge = function(start, step, x){
// protection against using the class without the new keyword
if(!(this instanceof Grunge)){
return new Grunge(start, step);
return new Grunge(start, step, x);
}

@@ -37,2 +39,22 @@

// accepting a 'step' function. Takes a value and returns the next value. gets three values (value, index, arrayOfLast 10 values)
if(typeof step === 'function'){
//x = (x && x > 0) ? x : 10;
this.generator = function* (){
var currValue = start;
var index = 0;
var valuesSoFar = [];
while(true){
yield currValue;
valuesSoFar.push(currValue);
if(x && x > 0 && valuesSoFar.length > x){
valuesSoFar.shift();
}
currValue = step(currValue, index, valuesSoFar);
index ++;
};
};
return this;
}
//if an array is passed in iterate over the elements.

@@ -74,3 +96,3 @@ if(Array.isArray(start)){

} catch (e){
return;
return e;
}

@@ -106,13 +128,40 @@ };

}
var that = this;
// Old version with for-of loops
// var newGrunge = new Grunge(function* (){
// var i = 0;
// for(let elem of that.generator()){
// i++;
// if(i > num){
// yield elem;
// }
// }
// });
var newGrunge = new Grunge(function* (){
var i = 0;
for(let elem of that.generator()){
i++;
if(i > num){
yield elem;
try {
var startIterator = that.generator();
var elem = startIterator.next();
var i = 0;
while(elem.done === false){
if(i >= num){
yield elem.value;
}
elem = startIterator.next();
i++;
}
if(i > num) {
return elem.value;
} else {
return;
}
} catch (e){
return e;
}
});
if(!!this.length){

@@ -126,3 +175,3 @@ newGrunge.length = Math.min(this.length - num, 0);

Grunge.prototype.step = function(num){
if(!num || num === 1){
if(!num || num === 0){
return this;

@@ -169,7 +218,26 @@ }

}
// Old version with for-of loops
// var newGrunge = new Grunge(function* (){
// for(let elem of that.generator()){
// yield func(elem);
// }
// });
var newGrunge = new Grunge(function* (){
for(let elem of that.generator()){
yield func(elem);
try {
var startIterator = that.generator();
var i = 0;
var elem = startIterator.next();
while(elem.done === false) {
yield func(elem.value, i);
elem = startIterator.next();
i++;
}
return func(elem.value, i);
} catch (e){
return e;
}
});
newGrunge.length = this.length;

@@ -185,9 +253,31 @@ return newGrunge;

}
// Old version with For-of loops
// var newGrunge = new Grunge(function* (){
// for(let elem of that.generator()){
// if(!!func(elem)){
// yield elem;
// }
// }
// });
var newGrunge = new Grunge(function* (){
for(let elem of that.generator()){
if(!!func(elem)){
yield elem;
try {
var startIterator = that.generator();
var i = 0;
var elem = startIterator.next();
while(elem.done === false){
if(!!func(elem.value, i)){
yield elem.value;
}
i++;
elem = startIterator.next();
}
if(!!func(elem.value, i)){
return elem.value;
}
} catch (e){
return e;
}
});
newGrunge.length = this.length;

@@ -229,4 +319,21 @@ return newGrunge;

}
for(let elem of this.generator()){
func(elem);
// old version with for-of loops
// for(let elem of this.generator()){
// func(elem);
// }
try {
var startIterator = this.generator();
var i = 0;
var elem = startIterator.next();
while(elem.done === false){
func(elem.value, i);
elem = startIterator.next();
i++;
}
// not sure why this is happening
//func(elem.value, i);
} catch (e){
return e;
}

@@ -233,0 +340,0 @@ };

2

package.json
{
"name": "grunge",
"version": "0.0.6",
"version": "0.1.0",
"description": "A generator-based sequence generator and utility.",

@@ -5,0 +5,0 @@ "main": "index.js",

Grunge
======
A generator-based sequence generator and utility.
A generator-based underscore and Range like utility library that helps you deal with finite and infinite sequences.

@@ -13,8 +13,19 @@ ## About

Grunge is also a simple utility for composing recursive functions of the time where the solution for n depends on the solution for n-1.
**Note:** Grunge is an experimental library in an early stage of development. There probably will be bugs, and it is by no means ready for production. Tests, bug-fixes and enhancements are welcome.
## Installation
On the browser, you can just download the index.js file from right here and use it.
For Node
```
npm install grunge
```
should work!
## Why Generators?
Generators are slowly finding support and are already available in Chrome, Firefox and Node 0.11.x. Moreover with many transpilers it's easy to convert Grunge into ES5 compliant code. (I will soon add a ES5 distribution ready to use, and available on various Package Managers)
Grunge *could* be written without generators, but the code would be longer and more complicated. And again, with the availability of transpilers, I think the expressiveness of the code is more important.
Grunge *could* be written without generators. In fact, for finite sequences, Lazy.js does a great job at achieving lazy evaluation. Generators however, make whole new set of things possible.
And again, with the availability of transpilers, I think the expressiveness of the code is more important.

@@ -30,8 +41,25 @@ ## Show me some examples

// Make a sequence starting at 1 and a step function 1
var result = Grunge(1,1).map(function(n){return n*n;}).map(function(n){return n+1}).filter(isPrime).take(5).toArray();
var result = Grunge(1,1).map(function(n){return n*n;}).map(function(n){return n+1}).filter(isPrime).take(5).forEach(doSomething);
```
Things can get more interesting when you start feeding generator functions into Grunge
Things can get more interesting when you start feeding step functions into Grunge
```
var rps = Grunge([['rock'], ['paper'], ['scissors']], function(el){
var results = [];
for(var i = 0; i < el.length; i++){
results.push(['rock'].concat(el[i]));
results.push(['paper'].concat(el[i]));
results.push(['scissors'].concat(el[i]));
}
return results;
}).skip(2).take(1).toArray();
```
Here are you are solving for all possible moves in a game of Rock Paper Scissors in three games, by starting with the base case and iterating rather that recursing.
You can even go all out and feed Generator functions into Grunge.
```
var fibinacciSquaredFiltered = Grunge(function*(){

@@ -51,3 +79,3 @@ var first = 1;

Here you have an infinite sequence of fibinacci numbers, you can map each of them to squares, filter out all the even numbers, then pick every third element, and finally get hundred such elements.
Here you have an infinite sequence of fibonacci numbers, you can map each of them to squares, filter out all the even numbers, then pick every third element, and finally get hundred such elements.

@@ -54,0 +82,0 @@ Another situation where this might be useful is generating Prime numbers.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc