I'm wondering if there is a more concise and efficient way to achieve the following code:
import { pipe } from 'fp-ts/function'
import * as A from 'fp-ts/Array'
import * as E from 'fp-ts/Option'
pipe(
O.some(['foo', 'bar', 'baz']), // O.Option<string[]>
O.map(A.map(mapFn)), // O.Option<Array<O.Option<string>>>
O.chain(A.sequence(O.Applicative)), // O.Option<Array<string>>
)
Perhaps the root enclosing Option
is detracting from the main point, but I'm basically trying to achieve the following transformation T[] -> Option<T[]>
through mapping with mapFn
which does T -> Option<T>
.
I would be happy with this working example, although I can't help but notice that the A.map
step completes entirely before the A.sequence
step. If I could combine these two steps, then as soon as an element is mapped to O.None
it would break (and not map the following elements unnecessarily).