Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array: add S.prepend #205

Merged
merged 1 commit into from
May 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2451,19 +2451,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
23 changes: 8 additions & 15 deletions test/append.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,31 @@ describe('append', function() {
'\n' +
'The value at position 1 is not a member of ‘Array a’.\n'));

throws(function() { S.append('c', [1, 2]); },
throws(function() { S.append('3', [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' +
'1) "3" :: 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('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'); },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we test the type checking of the final argument by adding a test that uses the placeholder as the first arg?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you have in mind, Stefano? We test these failures:

prepend :: a -> Array a -> Array a
                ^^^^^^^
prepend :: a -> Array a -> Array a
           ^          ^

We don't test these failures:

prepend :: a -> Array a -> Array a
                           ^^^^^^^
prepend :: a -> Array a -> Array a
           ^                     ^

Assuming our function is implemented correctly, there's no way to test invalid return values. ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mind me, I'm losing my mind, I completely misread the first test there.

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

});