Last active
October 10, 2019 18:15
-
-
Save evilsoft/9decb1474e375fc9ecd2aa9b924bd881 to your computer and use it in GitHub Desktop.
Code sample from Algebraic Javascript
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 recs = [ | |
{ learner: 'Joan', score: 84 }, | |
null, | |
{ learner: null, score: 97 }, | |
{ learner: 'Thomas', score: 72 }, | |
22, | |
{ learner: 'Bob', score: 92 } | |
{ learner: 'Joey', score: 62 }, | |
] | |
export default recs |
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
import { | |
and, chain, compose, constant, fanout, | |
getProp, isNumber, isString, liftA2, | |
maybeToArray, merge, propSatisfies, safe | |
} from 'crocks' | |
import data from './data' | |
// gte :: Number -> Number -> Boolean | |
const gte = a => b => | |
b >= a | |
// validScore :: Number -> (a -> Boolean) | |
const validScore = | |
compose(and(isNumber), gte) | |
// passesWith :: Number -> Record -> Maybe Record | |
const passesWith = compose( | |
safe, | |
propSatisfies('score'), | |
validScore | |
) | |
// validLearner :: Record -> Boolean | |
const validLearner = | |
and(isString, propSatisfies('length', gte(4))) | |
// validateLearner :: (Record -> Boolean) -> Record -> Maybe String | |
const validateLearner = test => compose( | |
chain(safe(test)), | |
getProp('learner') | |
) | |
// validRecord :: Record -> Pair (Maybe String) (Naybe Record) | |
const validRecord = fanout( | |
validateLearner(validLearner), | |
passesWith(70), | |
) | |
// validateRecord :: Record -> Maybe String | |
const validateRecord = compose( | |
merge(liftA2(constant)), | |
validRecord | |
) | |
// program :: [ Record ] -> [ String ] | |
const program = | |
chain(maybeToArray(validateRecord)) | |
console.log(data) | |
console.log( | |
program(data) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment