-
-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert Either to Validation #76
Comments
For context, I am using io-ts which returns an I'd also be curious to know why io-ts uses |
@OliverJAsh
Usage (with array):
|
@OliverJAsh
Usage
|
That's a good question!
That "then" requires the power of a monad and its import { left, right } from 'fp-ts/lib/Either'
// any -> number
const isNumber = (input: any) => typeof input === 'number' ? right<string, number>(input) : left<string, number>('Not a number')
// number -> integer
const isNumberInteger = (input: number) => input % 1 === 0 ? right<string, number>(input) : left<string, number>('Not an integer')
// now I can compose the validations
// any -> integer
const isInteger = (input: any) => isNumber(input).chain(isNumberInteger)
console.log(isInteger('s')) // Left("Not a number")
console.log(isInteger(1.1)) // Left("Not an integer")
console.log(isInteger(1)) // Right(1) I used the term "compose" because monads are a way to make composable a group of functions which naturally don't compose. Those functions, called Kleisli arrows, have the following general signature
where
From this point of view monads are pretty easy to explain: under the hood is just function composition (well to be precise is morphism composition). This is a repo where I try to explain all these things (I put up a functional programming meet up here in Milano and the repo contains a series of lectures I gave during the last months) Alas is in italian.. |
Super useful and insightful replies. Thank you! :-) |
Is it possible to write a function that converts an Either to a Validation?
I presume this would only work if the
Left
type was a semigroup, as that is required by Validation?Here was my attempt, however it fails to compile:
The text was updated successfully, but these errors were encountered: