I am still learning and playing with fp-ts and can't figure this out.
I have an array of Either<any, number>[]
and I would like to get an Either<any, number[]>
.
I have looked at Apply.sequenceT
and the example sequenceToOption and it looks close.
import { NumberFromString } from 'io-ts-types/lib/NumberFromString'
import { Either } from 'fp-ts/lib/Either'
const a:Either<any,number>[] = ['1','2','3'].map(NumberFromString.decode)
console.log(a)
// [ { _tag: 'Right', right: 1 },
// { _tag: 'Right', right: 2 },
// { _tag: 'Right', right: 3 } ]
I want instead either an error or array of numbers.
To go from Array<Either<L, A>>
to Either<L, Array<A>>
you can use sequence
:
import { array } from 'fp-ts/lib/Array'
import { either } from 'fp-ts/lib/Either'
array.sequence(either)(arrayOfEithers)
Your example can also be further simplified using traverse
array.traverse(either)(['1','2','3'], NumberFromString.decode)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With