-
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add S.mean that calculates mean of values of a foldable
- Loading branch information
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
var throws = require('assert').throws; | ||
|
||
var eq = require('./utils').eq; | ||
var errorEq = require('./utils').errorEq; | ||
var S = require('..'); | ||
|
||
|
||
describe('mean', function() { | ||
|
||
it('is a unary function', function() { | ||
eq(typeof S.mean, 'function'); | ||
eq(S.mean.length, 1); | ||
}); | ||
|
||
it('type checks its arguments', function() { | ||
throws(function() { S.mean('xxx'); }, | ||
errorEq(TypeError, | ||
'Type-class constraint violation\n' + | ||
'\n' + | ||
'mean :: Foldable f => f -> Maybe FiniteNumber\n' + | ||
' ^^^^^^^^^^ ^\n' + | ||
' 1\n' + | ||
'\n' + | ||
'1) "xxx" :: String\n' + | ||
'\n' + | ||
'‘mean’ requires ‘f’ to satisfy the Foldable type-class constraint; the value at position 1 does not.\n')); | ||
|
||
throws(function() { S.mean([1, 2, 'xxx']); }, | ||
errorEq(TypeError, | ||
'Type-variable constraint violation\n' + | ||
'\n' + | ||
'mean :: Foldable f => f -> Maybe FiniteNumber\n' + | ||
' ^\n' + | ||
' 1\n' + | ||
'\n' + | ||
'1) [1, 2, "xxx"] :: Array ???\n' + | ||
'\n' + | ||
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n')); | ||
|
||
throws(function() { S.mean([1, Infinity]); }, | ||
errorEq(TypeError, | ||
'Invalid value\n' + | ||
'\n' + | ||
'mean :: Foldable f => f -> Maybe FiniteNumber\n' + | ||
' ^^^^^^^^^^^^\n' + | ||
' 1\n' + | ||
'\n' + | ||
'1) Infinity :: Number, ValidNumber\n' + | ||
'\n' + | ||
'The value at position 1 is not a member of ‘FiniteNumber’.\n')); | ||
}); | ||
|
||
it('returns the mean of a non-empty array of numbers', function() { | ||
eq(S.mean([1, 2, 3]), S.Just(2)); | ||
eq(S.mean([0.1, 0.3]), S.Just(0.2)); | ||
eq(S.mean([-0]), S.Just(0)); | ||
eq(S.mean([-0, 0]), S.Just(0)); | ||
}); | ||
|
||
it('returns nothing when applied to an empty array', function() { | ||
eq(S.mean([]), S.Nothing()); | ||
}); | ||
|
||
it('can be applied to Maybes', function() { | ||
eq(S.mean(S.Nothing()), S.Nothing()); | ||
eq(S.mean(S.Just(42)), S.Just(42)); | ||
}); | ||
|
||
it('can be applied to Eithers', function() { | ||
eq(S.mean(S.Left('xxx')), S.Nothing()); | ||
eq(S.mean(S.Right(42)), S.Just(42)); | ||
}); | ||
|
||
}); |