Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tstone committed Feb 1, 2012
0 parents commit 036b11a
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008-2011 Pivotal Labs

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
105 changes: 105 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

Array Extras for Javascript
===========================

A few little things I find myself using a lot.


Array.clone()
-------------
Create a new copy of an array.

```javascript
var a = [1, 2, 3];
var b = a.clone();
// b == [1, 2, 3]
```

Array.cons()
------------
Remove the first array element from the array (head) and return an object containing both the head and remainder of the array (tail). Cons doesn't mutate the source array.

```javascript
var a = [1, 2, 3];
var b = a.cons();
// a == [1, 2, 3];
// b.head = 1;
// b.tail = [2, 3];
```

Array.sets()
------------
Returns a new array containing consecutive sets from the source array.

```javascript
var a = [1, 2, 3];
var b = a.sets();
// b == [ [1, 2], [2, 3] ]
```

Array.pairs()
------------
Returns a new array containing consecutive pairs from the source array.

```javascript
var a = [1, 2, 3, 4].pairs();
// a == [ [1, 2], [3, 4] ]
var b = [1, 2, 3].pairs();
// b == [ [1, 2], [3] ]
```

Array.triples()
------------
Returns a new array containing consecutive triples from the source array.

```javascript
var a = [1, 2, 3, 4, 5, 6].pairs();
// a == [ [1, 2, 3], [4, 5, 6] ]
var b = [1, 2, 3, 4].pairs();
// b == [ [1, 2, 3], [4] ]
```

Array.ipop()
-----------
Same thing as .cons() but works on the last element. Head in this case is the remainder array.

```javascript
var a = [1, 2, 3];
var b = a.ipop();
// a == [1, 2, 3];
// b.head = [1, 2];
// b.tail = 3;
```

Array.ipush()
-------------
Same thing as .push() but does not mutate the source array. Returns the new array.

```javascript
var a = [1, 2, 3];
var b = a.push(4);
// a == [1, 2, 3];
// b == [1, 2, 3, 4];
```

Array.ireverse()
-------------
Same thing as .reverse() but does not mutate the source array. Returns the new array.

```javascript
var a = [1, 2, 3];
var b = a.reverse();
// a == [1, 2, 3];
// b == [3, 2, 1];
```

Array.iunshift()
-------------
Same thing as .unshift() but does not mutate the source array. Returns the new array.

```javascript
var a = [1, 2, 3];
var b = a.unshift(0);
// a == [1, 2, 3];
// b == [0, 1, 2, 3];
```
92 changes: 92 additions & 0 deletions array-extras.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

(function(){

var immutableAction = function(x){
return function() {
var na = this.clone();
na[x].apply(na, Array.prototype.slice.call(arguments, 0));
return na;
}
};

var extras = {

// Array.clone()
clone: function() { return this.slice(0); },

// Array.cons()
cons: function() {
var na = this.clone();
return { head: na.shift(), tail: na };
},

// Array.pop()
pop: function() {
var na = this.clone();
var tail = na.pop();
return { head: na, tail: tail };
},

// Array.sets()
sets: function() {
var xs = this;
var loop = function(acc, i) {
if (i == xs.length || !xs[i+1]) { return acc; }
acc.push([xs[i], xs[i+1]]);
return loop(acc, i + 1);
}
return loop([], 0);
},

// Array.pairs();
pairs: function() {
var xs = this;
var loop = function(acc, i) {
if (i >= xs.length) { return acc; }
var pair = [xs[i]];
if (xs[i+1]) { pair.push(xs[i+1]); }
return loop(acc.ipush(pair), i + 2);
}
return loop([], 0);
},

// Array.triples();
triples: function() {
var xs = this;
var loop = function(acc, i) {
if (i >= xs.length) { return acc; }
var pair = [xs[i]];
if (xs[i+1]) {
pair.push(xs[i+1]);
if (xs[i+2]) { pair.push(xs[i+2]); }
}
return loop(acc.ipush(pair), i + 3);
}
return loop([], 0);
},

// Immutable variations of standard functions
ipush: immutableAction('push'),
ireverse: immutableAction('reverse'),
isort: immutableAction('sort'),
isplice: immutableAction('splice'),
iunshift: immutableAction('unshift')

};

// Extend
for (var key in extras) {
if (extras.hasOwnProperty(key) || !Array.prototype[key]) {
Array.prototype[key] = extras[key];
}
}
}());

// ------------------------------------------

var stops = [1, 3, 5];
var steps = 6;
var sets = stops.sets();
var pairs = stops.triples();

console.log(pairs);

0 comments on commit 036b11a

Please sign in to comment.