Skip to content

Commit

Permalink
Add S.append
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Pflug committed Apr 26, 2016
1 parent 717438c commit 2804380
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,7 @@
return n < 0 || negativeZero(n) ? Nothing() : slice(0, -n, xs);
});


// ArrayLike :: TypeClass
var ArrayLike = $.TypeClass(
'ArrayLike',
Expand All @@ -2292,6 +2293,24 @@
R.pipe(R[name], Just, R.filter(R.gte(_, 0))));
};

//# append :: a -> Array a -> Array a
//.
//. Takes a value of any type and an array of values of that type, and
//. returns the result of appending the value to the array.
//.
//. ```javascript
//. > S.append(3, [1, 2])
//. [1, 2, 3]
//.
//. > S.append([3, 4], [[1], [2]])
//. [[1], [2], [3, 4]]
//. ```
S.append =
def('append',
{},
[a, $.Array(a), $.Array(a)],
function(x, xs) { return xs.concat([x]); });

//# indexOf :: a -> [a] -> Maybe Integer
//.
//. Takes a value of any type and a list, and returns Just the index
Expand Down
21 changes: 21 additions & 0 deletions test/append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

var eq = require('./utils').eq;
var S = require('..');

describe('append', function() {

it('adds the element to the end of the list', function() {
eq(S.append('c', ['a', 'b']), ['a', 'b', 'c']);
eq(S.append({x: 3}, [{x: 1}, {x: 2}]), [{x: 1}, {x: 2}, {x: 3}]);
});

it('adds a list to a list of lists', function() {
eq(S.append([3, 4], [[1], [2]]), [[1], [2], [3, 4]]);
});

it('works on empty list', function() {
eq(S.append(1, []), [1]);
});

});

0 comments on commit 2804380

Please sign in to comment.