import { Maybe, Just, Nothing } from 'purify-ts/Maybe'
// Hard to chain additional transformations
// Doesn't protect against falsy values like empty string or 0
const config: Config | null = getConfig()
const port = config && config.port
? parseInt(config.port)
: 8080
// Still hard to chain additional transformations
// This is a valid alternative if you don't mind the syntax
const port = parseInt(getConfig()?.port ?? '8080')
const port = getConfig()
.chain(x => x.port)
.map(parseInt)
.orDefault(8080)
Just(10)
➔
Just(10) // Maybe<number>
Nothing
➔
Nothing // Maybe<never>
Maybe.of(10)
➔
Just(10)
Maybe.empty()
➔
Nothing
Maybe.zero()
➔
Nothing
Maybe.fromNullable(null)
Maybe.fromNullable(10)
➔
➔
Nothing
Just(10)
Maybe.fromFalsy('')
Maybe.fromFalsy(0)
➔
➔
Nothing
Nothing
Maybe.fromPredicate(x => x > 0, 5)
Maybe.fromPredicate(x => x > 0, -1)
➔
➔
Just(5)
Nothing
Maybe.catMaybes([Just(5), Nothing, Just(10)])
➔
[5, 10]
Maybe.mapMaybe(x => isNaN(x) ? Nothing : Just(parseInt(x)), ['1', 'Apple', '3'])
➔
[1, 3]
Maybe.sequence([Just(1), Just(5), Just(10)])
Maybe.sequence([Just(1), Nothing, Just(10)])
➔
➔
Just([1, 5, 10])
Nothing
Maybe.encase(() => { throw Error('Always fails') })
Maybe.encase(() => 10)
➔
➔
Nothing
Just(10)
Maybe.isMaybe(null)
Maybe.isMaybe(Just(10))
➔
➔
false
true
Just(5).isJust()
Nothing.isJust()
➔
➔
true
false
Just(5).isNothing()
Nothing.isNothing()
➔
➔
false
true
Just(5).caseOf({ Just: x => x + 1, Nothing: () => 0 })
Nothing.caseOf({ Just: x => x + 1, Nothing: () => 0 })
Nothing.caseOf({ _: () => 'anything'}) // wildcard
➔
➔
➔
6
0
'anything'
Just(5).equals(Just(5))
Just(5).equals(Just(10))
Just(5).equals(Nothing)
➔
➔
➔
true
false
false
Just(5).map(x => x + 1)
Nothing.map(x => x + 1)
➔
➔
Just(6)
Nothing
Just(5).ap(Just(x => x + 1))
Just(5).ap(Nothing)
Nothing.ap(Just(x => x + 1))
Nothing.ap(Nothing)
➔
➔
➔
➔
Just(6)
Nothing
Nothing
Nothing
Just(5).alt(Just(6))
Just(5).alt(Nothing)
Nothing.alt(Just(5))
Nothing.alt(Nothing)
➔
➔
➔
➔
Just(5)
Just(5)
Just(5)
Nothing
Just(5).chain(x => Just(x + 1))
Nothing.chain(x => Just(x + 1))
➔
➔
Just(6)
Nothing
Just({prop: null}).chainNullable(x => x.prop)
Just(5).chainNullable(x => x + 1)
➔
➔
Nothing
Just(6)
Just(Just(5)).join()
Nothing.join()
➔
➔
Just(5)
Nothing
Just(5).reduce((acc, x) => x * acc, 2)
Nothing.reduce((acc, x) => x * acc, 0)
➔
➔
10
0
Just(5).extend(x => x.isJust())
Nothing.extend(x => x.isJust())
➔
➔
Just(true)
Nothing
Just(5).unsafeCoerce()
Nothing.unsafeCoerce()
➔
➔
5
// Uncaught Error: Maybe#unsafeCoerce was ran on a Nothing
Just(5).orDefault(0)
Nothing.orDefault(0)
➔
➔
5
0
Just(5).orDefaultLazy(() => expensiveComputation())
Nothing.orDefaultLazy(() => 0)
➔
➔
5 // expensiveComputation is never called
0
Just(5).mapOrDefault(x => x + 1, 0)
Nothing.mapOrDefault(x => x + 1, 0)
➔
➔
6
0
Just(5).filter(x => x > 1)
Just('apple').filter(x => x === 'banana')
➔
➔
Just(5)
Nothing
Just(5).extract()
Nothing.extract()
➔
➔
5
undefined
Just(5).extractNullable()
Nothing.extractNullable()
➔
➔
5
null
Just(5).toList()
Nothing.toList()
➔
➔
[5]
[]
Just(5).toEither('Error')
Nothing.toEither('Error')
➔
➔
Right(5)
Left('Error')
Just(5).ifJust(() => console.log('success'))
Nothing.ifJust(() => console.log('success'))
➔
➔
// success
Just(5).ifNothing(() => console.log('failure'))
Nothing.ifNothing(() => console.log('failure'))
➔
➔
// failure