diff --git a/index.js b/index.js index 44023d86..23310be3 100644 --- a/index.js +++ b/index.js @@ -2274,6 +2274,7 @@ return n < 0 || negativeZero(n) ? Nothing() : slice(0, -n, xs); }); + // ArrayLike :: TypeClass var ArrayLike = $.TypeClass( 'ArrayLike', @@ -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 diff --git a/test/append.js b/test/append.js new file mode 100644 index 00000000..2cc891c1 --- /dev/null +++ b/test/append.js @@ -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]); + }); + +});