Skip to content

Instantly share code, notes, and snippets.

@BlessYAHU
Last active December 23, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlessYAHU/6644051 to your computer and use it in GitHub Desktop.
Save BlessYAHU/6644051 to your computer and use it in GitHub Desktop.
Code has been changed to pseudocode, but intent is still there. Original code is using Underscore, fmap from drboolean's typeclasses library and autocurry from wu.js. Original code gets a list of filter commands and a list of products. Filter views are rendered only if products have properties matching filtercommand field names.
var lookupExecute = function( products, processCommands) {
this.commandArray = processCommands;
this.lookupArray = products;
};
// unwrapped values in functor, apply given function over each command object that has lookup items that meet the condition
lookupExecute.prototype.fmap = function(fn) {
var self = this,
mappedResults = map(this.commandArray, function(command) {
var matchedItems = self.lookupArray.filter(function(item) {
return item.has(command.FieldName);
});
return isEmpty(matchedItems) ? command : fn(command);
});
// wrap the values back up.
return new lookupExecute(this.lookupArray, this.commandArray);
};
// array is needed in the method
var executeCommand = function(arry, command) {
//code that executes command
return command;
}.autoCurry();
// possible use: render a view using items in collection, only if fieldname of command matches some items
fmap(executeCommand(prodTypeCollection), new lookupExecute(prodTypeCollection, filterViewCmdArray));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment