Last active
September 18, 2016 06:02
-
-
Save mofas/c58303d929ef8e316b2a5f95a6911ca3 to your computer and use it in GitHub Desktop.
FP practice
This file contains hidden or 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
const id = 'archie_is_good_guy'; | |
// you may change listSetting for testing. | |
const listSetting = []; | |
//original function. | |
const targetListSetting = listSetting.filter(dd=>{ | |
return id === dd.bfid || id === dd.name_list_id; | |
})[0]; | |
//point-free experssion rewrite | |
//helper fn | |
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x); | |
const flip = fn => (...fns) => fn(...fns.reverse()); | |
const pipe = flip(compose); | |
const first = d => d[0]; | |
const filter = predicate => d => d.filter(predicate); | |
const not = fn => x => !fn(x); | |
const and = fa => fb => x => fa(x) && fb(x); | |
const or = fn => fa => fb => x => fa(x) || fb(x); | |
const props = field => x => x[field]; | |
const map = fn => x => x.map(fn); | |
const filter = fn => x => x.filter(fn); | |
const reduce = fn => x => x.reduce(fn); | |
const some = fn => x => x.some(fn); | |
const every = fn => x => x.every(fn); | |
//business logic | |
const MatchId = equal(id); | |
const getBfid = get('bfid'); | |
const getNameListId = get('name_list_id'); | |
//Q: write this fn | |
const targetListPredicate = ... | |
const getTargetListSetting = first(filter(targetListPredicate)); | |
const targetListSetting = getTargetListSetting(listSetting) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment