Skip to content

Commit

Permalink
array: add S.prepend
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed May 1, 2016
1 parent b149c57 commit eda1ff5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 46 deletions.
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2450,19 +2450,35 @@
//. 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.
//.
//. See also [`prepend`](#prepend).
//.
//. ```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]); });

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

//# find :: (a -> Boolean) -> Array a -> Maybe a
//.
//. Takes a predicate and an array and returns Just the leftmost element of
Expand Down
79 changes: 36 additions & 43 deletions test/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,49 @@ 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('is a binary function', function() {
eq(typeof S.append, 'function');
eq(S.append.length, 2);
});

it('adds a list to a list of lists', function() {
eq(S.append([3, 4], [[1], [2]]), [[1], [2], [3, 4]]);
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('works on empty list', function() {
eq(S.append(1, []), [1]);
it('appends an element to an array', function() {
eq(S.append(3, []), [3]);
eq(S.append(3, [1, 2]), [1, 2, 3]);
eq(S.append([5, 6], [[1, 2], [3, 4]]), [[1, 2], [3, 4], [5, 6]]);
});

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

});
57 changes: 57 additions & 0 deletions test/prepend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

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

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


describe('prepend', function() {

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

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

throws(function() { S.prepend('1', [2, 3]); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'prepend :: a -> Array a -> Array a\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "1" :: String\n' +
'\n' +
'2) 2 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
' 3 :: 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('prepends an element to an array', function() {
eq(S.prepend(1, []), [1]);
eq(S.prepend(1, [2, 3]), [1, 2, 3]);
eq(S.prepend([1, 2], [[3, 4], [5, 6]]), [[1, 2], [3, 4], [5, 6]]);
});

it('is curried', function() {
eq(S.prepend(1).length, 1);
eq(S.prepend(1)([2, 3]), [1, 2, 3]);
});

});

0 comments on commit eda1ff5

Please sign in to comment.