Last active
March 23, 2018 16:55
-
-
Save gcanti/cd94bcb9b8460a5ff7992311064bac22 to your computer and use it in GitHub Desktop.
Functional TypeScript: Either vs Validation
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 { Validation, failure, getApplicative } from 'fp-ts/lib/Validation' | |
import { getArrayMonoid } from 'fp-ts/lib/Monoid' | |
const validation = getApplicative(getArrayMonoid<string>()) | |
const checkAgeV = checkAge(validation, failure) | |
const checkClothesV = checkClothes(validation, failure) | |
const checkSobrietyV = checkSobriety(validation, failure) | |
function costToEnter2(p: Person): Validation<Errors, number> { | |
const price = () => () => (): number => (p.gender === 'Female' ? 0 : 5) | |
return validation.ap(validation.ap(validation.map(checkAgeV(p), price), checkClothesV(p)), checkSobrietyV(p)) | |
} | |
console.log('-- Act Two --') | |
console.log(costToEnter2({ ...dave, sobriety: 'Paralytic' })) // failure(["Too Old!", "Sober Up!"]) | |
console.log(costToEnter2(ruby)) // success(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment