Can you memoize as TaskEither? #1667
-
How to effectively memoize a TaskEither? I have an array with about 1000 elements. For each element I need to perform an api call, but there are about 2-5 different results across these 1000 elements. So I would like to memoize the api call, but I haven't found a way to do this yet. Given the following code, the task either is lazily evaluated. How can I memoize the import { pipe } from 'fp-ts/lib/function';
import * as TE from 'fp-ts/lib/TaskEither';
import * as ROA from 'fp-ts/ReadonlyArray';
type A = any;
type Transaction = { importId: string };
// how to memoize getApiResult?
const getApiResult = (importId: string): TE.TaskEither<Error, A> =>
TE.tryCatch(
() => {
// calls some api, but I can't memoize the api function
},
() => new Error(''),
);
const convert =
(t: Transaction) =>
(apiResult: A): TE.TaskEither<Error, string> => {
throw new Error('Not implemented');
};
const main = (transactions: readonly Transaction[]) =>
pipe(
transactions,
ROA.map((t) => pipe(t.importId, getApiResult, TE.chain(convert(t)))),
);
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Why call |
Beta Was this translation helpful? Give feedback.
-
IORef 1 is easiest hack to place inside Footnotes |
Beta Was this translation helpful? Give feedback.
IORef 1 is easiest hack to place inside
getApiResult
.StateReaderTaskEither 2 is fp-ish way to go.
Footnotes
https://gcanti.github.io/fp-ts/modules/IORef.ts.html ↩
https://gcanti.github.io/fp-ts/modules/StateReaderTaskEither.ts.html ↩