Skip to content

Commit

Permalink
Merge pull request #847 from null-a/use-helper-to-parse-kernel-opts
Browse files Browse the repository at this point in the history
Minor clean-up in kernel option parsing.
  • Loading branch information
stuhlmueller committed May 16, 2017
2 parents 1b5a0bf + 9d0a763 commit 3a51c0a
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/inference/kernels.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,14 @@ module.exports = function(env) {
// Expects either a kernel name or an object containing a single
// key/value pair where the key is a kernel name and the value is
// an options object. e.g. 'MH' or { MH: { ... } }

function isKernelOption(obj) {
return _.isString(obj) && _.has(kernels, obj) ||
_.size(obj) === 1 && _.has(kernels, _.keys(obj)[0]);
}

if (!isKernelOption(obj)) {
throw new Error('Unrecognized kernel option: ' + JSON.stringify(obj));
}

var name = _.isString(obj) ? obj : _.keys(obj)[0];
var options = _.isString(obj) ? {} : _.values(obj)[0];
return kernels[name](options);
return util.getValAndOpts(obj, function(name, options) {
if (!_.has(kernels, name)) {
throw new Error(name + ' is not a valid kernel. ' +
'The following kernels are available: ' +
_.keys(kernels).join(', ') + '.');
}
return kernels[name](options);
});
}

// Combinators for kernel functions.
Expand Down

0 comments on commit 3a51c0a

Please sign in to comment.