Skip to content

Instantly share code, notes, and snippets.

@vamsiampolu
Created August 28, 2016 10:09
Show Gist options
  • Save vamsiampolu/6241f487cd781ca35f05db67c726b0c6 to your computer and use it in GitHub Desktop.
Save vamsiampolu/6241f487cd781ca35f05db67c726b0c6 to your computer and use it in GitHub Desktop.
Unwrapping our badly written deeply nested code to use currying and composition
var curry = require('lodash/fp/curry')
var compose = require('lodash/fp/compose')
var map = require('lodash/fp/map')
var filter = require('lodash/fp/filter')
var reduce = require('lodash/fp/reduce')
var zip = require('lodash/fp/zip')
function getTasksExist(taskId,task) {
return task.taskId === taskId;
}
getTasksExist = curry(getTasksExist)
function getSubgroupsExist(subgroupId,subgroup) {
return subgroup.subgroupId === subgroupId;
}
getSubgroupsExist = curry(getSubgroupsExist)
function getGroupsExist(groupId,group) {
return group.groupId === groupId;
}
getGroupsExist = curry(getGroupsExist)
function getReduceTasks(taskId,state,tasks) {
state[taskId] = performImpressiveOperation(tasks)
return state;
}
getReduceTasks = curry(getReduceTasks)
function getTasksById(taskId) {
const tasksExist = getTasksExist(taskId)
const reduceTasks = getReduceTasks(taskId)
return compose(
reduce(reduceTasks,{})
map(createConsumableTasks),
filter(tasksExist)
)
}
function getReduceSubgroups(subgroupId,state,subgroup) {
const taskIds = subgroup.allTasks;
const tasks = map(getTasksById,taskIds)
state[subgroupId] = zip(taskIds,tasks)
return state;
}
getReduceSubgroups = curry(getReduceSubgroups)
function getSubgroupsById(subgroupId) {
const subgroupsExist = getSubgroupsExist(subgroupId);
const reduceSubgroups = getReduceSubgroups(subgroupId)
return compose(
reduceSubgroups,
map(createConsumableSubgroups),
filter(subgroupsExist)
)
}
function getReduceGroups(groupId,state,group) {
const subgroupIds = subgroup.allSubgroups;
const subgroups = map(getSubgroupsById)
state[groupId] = zip(subgroupIds,subgroups)
return state
}
getReduceGroups = curry(getReduceGroups)
function groupsById(groupId) {
const groupsExist = getGroupsExist(groupId)
const reduceGroups = getReduceGroups(groupId)
return compose(
reduce(reduceGroups,{})
map(createConsumableGroups),
filter(groupsExist)
)
}
const acceptsData = compose(
map(groupsById)
uniq(uniqByGroupId),
map(createGroupIdObjects)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment