Last active
March 23, 2018 16:55
-
-
Save gcanti/eae874f2d9f9de398416e69e051a5ab5 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 { traverse } from 'fp-ts/lib/Array' | |
const checkGenderV = (p: Person) => (p.gender !== 'Male' ? failure(['Men only']) : validation.of(p)) | |
function costToEnter3(p: Person): Validation<Errors, number> { | |
const price = (p: Person) => p.age + 1.5 | |
const checks = [checkAgeV, checkClothesV, checkSobrietyV, checkGenderV] as Array< | |
(p: Person) => Validation<Errors, Person> | |
> | |
return traverse(validation)(checks, f => f(p)).map(() => price(p)) | |
} | |
console.log('-- Act Three --') | |
const bob = mkPerson('Male', 59, ['Jeans'], 'Paralytic') | |
console.log(costToEnter3(ken)) // success(29.5) | |
console.log(costToEnter3(ruby)) // failure(["Men only"]) | |
console.log(costToEnter3(bob)) // failure(["Too Old!", "Smarten Up!", "Sober Up!"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment