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

maybe: add S.fromMaybe_ #316

Merged
merged 1 commit into from
Dec 30, 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
31 changes: 24 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,8 @@
//. Takes a default value and a Maybe, and returns the Maybe's value
//. if the Maybe is a Just; the default value otherwise.
//.
//. See also [`maybeToNullable`](#maybeToNullable).
//. See also [`fromMaybe_`](#fromMaybe_) and
//. [`maybeToNullable`](#maybeToNullable).
//.
//. ```javascript
//. > S.fromMaybe(0, S.Just(42))
Expand All @@ -1310,6 +1311,25 @@
}
S.fromMaybe = def('fromMaybe', {}, [a, $Maybe(a), a], fromMaybe);

//# fromMaybe_ :: (() -> a) -> Maybe a -> a
//.
//. Variant of [`fromMaybe`](#fromMaybe) which takes a thunk so the default
//. value is only computed if required.
//.
//. ```javascript
//. > function fib(n) { return n <= 1 ? n : fib(n - 2) + fib(n - 1); }
//.
//. > S.fromMaybe_(() => fib(30), S.Just(1000000))
//. 1000000
//.
//. > S.fromMaybe_(() => fib(30), S.Nothing)
//. 832040
//. ```
function fromMaybe_(thunk, maybe) {
return maybe.isJust ? maybe.value : thunk();
}
S.fromMaybe_ = def('fromMaybe_', {}, [$.Function, $Maybe(a), a], fromMaybe_);

//# maybeToNullable :: Maybe a -> Nullable a
//.
//. Returns the given Maybe's value if the Maybe is a Just; `null` otherwise.
Expand Down Expand Up @@ -1373,19 +1393,16 @@
//. is only computed if required.
//.
//. ```javascript
//. > global.fib = function fib(n) {
//. . return n <= 1 ? n : fib(n - 2) + fib(n - 1);
//. . }
//. fib
//. > function fib(n) { return n <= 1 ? n : fib(n - 2) + fib(n - 1); }
Copy link
Member Author

Choose a reason for hiding this comment

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

Here you go, Aldwin. :)

Copy link
Member

Choose a reason for hiding this comment

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

\o/

//.
//. > S.maybe_(() => fib(30), Math.sqrt, S.Just(1000000))
//. 1000
//.
//. > S.maybe_(() => fib(30), Math.sqrt, S.Nothing)
//. 832040
//. ```
function maybe_(f, g, maybe) {
return maybe.isJust ? g(maybe.value) : f();
function maybe_(thunk, f, maybe) {
return maybe.isJust ? f(maybe.value) : thunk();
}
S.maybe_ = def('maybe_', {}, [$.Function, $.Function, $Maybe(a), b], maybe_);

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"devDependencies": {
"browserify": "13.1.x",
"doctest": "0.10.x",
"doctest": "0.11.x",
"eslint": "2.9.x",
"fantasy-land": "0.3.0",
"istanbul": "0.4.x",
Expand Down
47 changes: 47 additions & 0 deletions test/fromMaybe_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

var R = require('ramda');

var S = require('..');

var eq = require('./internal/eq');
var throws = require('./internal/throws');


test('fromMaybe_', function() {

eq(typeof S.fromMaybe_, 'function');
eq(S.fromMaybe_.length, 2);

throws(function() { S.fromMaybe_(0); },
TypeError,
'Invalid value\n' +
'\n' +
'fromMaybe_ :: Function -> Maybe a -> a\n' +
' ^^^^^^^^\n' +
' 1\n' +
'\n' +
'1) 0 :: Number, FiniteNumber, Integer, ValidNumber\n' +
'\n' +
'The value at position 1 is not a member of ‘Function’.\n');

throws(function() { S.fromMaybe_(R.always(0), [1, 2, 3]); },
TypeError,
'Invalid value\n' +
'\n' +
'fromMaybe_ :: Function -> Maybe a -> a\n' +
' ^^^^^^^\n' +
' 1\n' +
'\n' +
'1) [1, 2, 3] :: Array Number, Array FiniteNumber, Array NonZeroFiniteNumber, Array Integer, Array ValidNumber\n' +
'\n' +
'The value at position 1 is not a member of ‘Maybe a’.\n');

eq(S.fromMaybe_(R.always(0), S.Nothing), 0);
eq(S.fromMaybe_(R.always(0), S.Just(42)), 42);

var count = 0;
eq(S.fromMaybe_(function() { return count += 1; }, S.Just(42)), 42);
eq(count, 0);

});