Constimport * as E from 'fp-ts/Either'
const unsafeHead = <A>(as: ReadonlyArray<A>): A => {
if (as.length > 0) {
return as[0]
} else {
throw new Error('empty array')
}
}
const head = <A>(as: ReadonlyArray<A>): E.Either<Error, A> =>
E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error')))
assert.deepStrictEqual(head([]), E.left(new Error('empty array')))
assert.deepStrictEqual(head([1, 2, 3]), E.right(1))
Constructs a new
Eitherfrom a function that might throw.See also
tryCatchK.