Skip to content

Instantly share code, notes, and snippets.

@gcanti
Last active March 23, 2018 16:55
Show Gist options
  • Save gcanti/cd94bcb9b8460a5ff7992311064bac22 to your computer and use it in GitHub Desktop.
Save gcanti/cd94bcb9b8460a5ff7992311064bac22 to your computer and use it in GitHub Desktop.
Functional TypeScript: Either vs Validation
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