import { Either, Left, Right } from 'purify-ts/Either'const getPort = () => {
const config: Config | null = getConfig()
if (config && config.port) {
return config.port
}
throw Error("Couldn't parse port from config")
}
let port: number
try {
port = parseInt(getPort())
} catch (e) {
loggingService.log(e.message)
port = 8080
}const getPort = () => getConfig() // Maybe makes a great combo with Either
.chain(x => x.port)
.toEither(new Error("Couldn't parse port from config"))
const port: number = getPort() // Either<Error, number>
.ifLeft((e) => loggingService.log(e.message))
.map(parseInt)
.orDefault(8080)// Inferred type is randomEither: () => Either<never, number> | Either<string, never>
const randomEither = () =>
Math.random() > 0.5 ? Right(1) : Left('Error')
randomEither().map(x => x)
// ~~~
// This expression is not callable.
// Each member of the union type ... has signatures,
// but none of those signatures are compatible with each other.Left('Error')➔Left('Error') // Either<string, never>Right(10)➔Right(10) // Either<never, number>Either.of(5)➔Right(5)Either.lefts([Left('Server error'), Left('Wrong password'), Right('foo@bar.com')])➔['Server error', 'Wrong password']Either.rights([Right(10), Left('Invalid input'), Right(5)])➔[10, 5]Either.encase(() => { throw Error('Always fails') })Either.encase(() => 10)➔➔Left(new Error('Always fails'))Right(10)Either.sequence([Right(1), Right(2)]))Either.sequence([Right(1), Left('Error')]))➔➔Right([1, 2])Left('Error')Either.isEither('Something')Either.isEither(Right(10))➔➔falsetrueLeft('Error').isLeft()Right(10).isLeft()➔➔truefalseLeft('Error').isRight()Right(10).isRight()➔➔falsetrueLeft('Error').caseOf({ Left: x => x, Right: () => 'No error' })Right(6).caseOf({ Left: _ => 0, Right: x => x + 1 })Left('Error').caseOf({ _: () => 0 }) // wildcard➔➔➔'Error'70Left('Error').bimap(x => x + '!', x => x + 1)Right(5).bimap(x => x + '!', x => x + 1)➔➔Left('Error!')Right(6)Left('Error').map(x => x + 1)Right(5).map(x => x + 1)➔➔Left('Error')Right(6)Left('Error').mapLeft(x => x + '!')Right(5).mapLeft(x => x + '!')➔➔Left('Error!')Right(5)Right(5).ap(Right(x => x + 1))Right(5).ap(Left('Error'))Left('Error').ap(Right(x => x + 1))Left('Error').ap(Left('Function Error'))➔➔➔➔Right(6)Left('Error')Left('Error')Left('Function Error')Left('Error').equals(Left('Error'))Right(5).equals(Right(5))Left(10).equals(Right(10))Right(5).equals(Left('Error'))➔➔➔➔truetruefalsefalseLeft('Error').chain(x => Right(x + 1))Right(5).chain(x => Right(x + 1))➔➔Left('Error')Right(6)Left('Error').chainLeft(x => Right(''))Right(5).chainLeft(x => Right(999))➔➔Right('')Right(5)Right(Right(5)).join()Left(Left('Error')).join()➔➔Right(5)Left(Left('Error'))Left('Error').alt(Left('Error!'))Left('Error').alt(Right(5))Right(5).alt(Left('Error'))Right(5).alt(Right(6))➔➔➔➔Left('Error!')Right(5)Right(5)Right(5)Right(5).reduce((acc, x) => x * acc, 2)Left('Error').reduce((acc, x) => x * acc, 0)➔➔100Left('Error').extend(x => x.isRight())Right(5).extend(x => x.isRight())➔➔Left('Error')Right(true)Right(5).unsafeCoerce()Left('Error').unsafeCoerce()Left(Error('Something')).unsafeCoerce()➔➔➔5// Uncaught Error: Either#unsafeCoerce was ran on a Left// Uncaught Error: SomethingLeft('Error').orDefault(0)Right(5).orDefault(0)➔➔05Left('Error').leftOrDefault('No error')Right(5).leftOrDefault('No error')➔➔'Error''No error'Left('Error').orDefault(() => 0)Right(5).orDefault(() => expensiveComputation())➔➔05 // expensiveComputation is never calledLeft('Error').leftOrDefault(() => 'No error')Right(5).leftOrDefault(() => 'No error')➔➔'Error''No error'Left('Error').toMaybe()Right(5).toMaybe()➔➔NothingJust(5)Left('Error').leftToMaybe()Right(5).leftToMaybe()➔➔Just('Error')NothingLeft('Error').ifLeft((err) => console.log(err))Right(5).ifLeft(() => console.log('Unexpected error'))➔➔// ErrorLeft('Error').ifRight((result) => console.log(result))Right(5).ifRight((result) => console.log(result))➔➔// 5Right(5).extract()Left('Error').extract()➔➔5'Error'Right(5).swap()Left(5).swap()➔➔Left(5)Right(5)