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))
➔
➔
false
true
Left('Error').isLeft()
Right(10).isLeft()
➔
➔
true
false
Left('Error').isRight()
Right(10).isRight()
➔
➔
false
true
Left('Error').caseOf({ Left: x => x, Right: () => 'No error' })
Right(6).caseOf({ Left: _ => 0, Right: x => x + 1 })
Left('Error').caseOf({ _: () => 0 }) // wildcard
➔
➔
➔
'Error'
7
0
Left('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'))
➔
➔
➔
➔
true
true
false
false
Left('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)
➔
➔
10
0
Left('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: Something
Left('Error').orDefault(0)
Right(5).orDefault(0)
➔
➔
0
5
Left('Error').leftOrDefault('No error')
Right(5).leftOrDefault('No error')
➔
➔
'Error'
'No error'
Left('Error').orDefault(() => 0)
Right(5).orDefault(() => expensiveComputation())
➔
➔
0
5 // expensiveComputation is never called
Left('Error').leftOrDefault(() => 'No error')
Right(5).leftOrDefault(() => 'No error')
➔
➔
'Error'
'No error'
Left('Error').toMaybe()
Right(5).toMaybe()
➔
➔
Nothing
Just(5)
Left('Error').leftToMaybe()
Right(5).leftToMaybe()
➔
➔
Just('Error')
Nothing
Left('Error').ifLeft((err) => console.log(err))
Right(5).ifLeft(() => console.log('Unexpected error'))
➔
➔
// Error
Left('Error').ifRight((result) => console.log(result))
Right(5).ifRight((result) => console.log(result))
➔
➔
// 5
Right(5).extract()
Left('Error').extract()
➔
➔
5
'Error'
Right(5).swap()
Left(5).swap()
➔
➔
Left(5)
Right(5)