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 27, 2016
1 parent 717438c commit f36353f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,24 @@

//. ### Array

//# 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]); });

//# find :: (a -> Boolean) -> Array a -> Maybe a
//.
//. Takes a predicate and an array and returns Just the leftmost element of
Expand Down
64 changes: 64 additions & 0 deletions test/append.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

var throws = require('assert').throws;

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


describe('append', function() {

it('is a binary function', function() {
eq(typeof S.append, 'function');
eq(S.append.length, 2);
});

it('type checks its arguments', function() {
throws(function() { S.append('c', 'ab'); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
'append :: a -> Array a -> Array a\n' +
' ^^^^^^^\n' +
' 1\n' +
'\n' +
'1) "ab" :: String\n' +
'\n' +
'The value at position 1 is not a member of ‘Array a’.\n'));

throws(function() { S.append('c', [1, 2]); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'append :: a -> Array a -> Array a\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "c" :: String\n' +
'\n' +
'2) 1 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
' 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

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]);
});

it('is curried', function() {
eq(S.append('c').length, 1);
eq(S.append('c')(['a', 'b']), ['a', 'b', 'c']);
});

});

0 comments on commit f36353f

Please sign in to comment.