Lazily finding first item in an array of TaskEithers #1670
Unanswered
markgaylard
asked this question in
Q&A
Replies: 1 comment
-
Could use import {
alt,
function as F,
option as O,
readonlyArray as A,
taskEither as TE,
} from 'fp-ts';
declare const getNumber: (id: string) => TE.TaskEither<Error, O.Option<number>>;
const getFirstSuccess: (ids: string[]) => TE.TaskEither<Error, number> = F.flow(
A.map(F.flow(
getNumber,
TE.chain(TE.fromOption(() => new Error('no number'))),
)),
alt.altAll (TE.Alt) (TE.left(new Error('empty array'))),
); Note that in order to play well, you'd want to lift and flat nested |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi. Given the following functions:
I'm trying to work out how to (lazily) find the first
O.some
result. My initial idea was to implementgetFirstSuccess
with something likepipe(ids, A.findFirstMap(getNumber))
but that doesn't work becausefindFirstMap
wants a function that returns an Option. I think I can make it work by usingA.map(getNumber)
thenTE.sequenceArray
then folding the result and doing aA.findFirst
on the result, but ideally I'd like this to only callgetNumber
until i get the first non-none result.Is this possible? If so, how? :)
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions