Replies: 2 comments
-
I would probably try to separate these things a little bit. First define your // These.ts
import * as S from 'fp-ts/Semigroup';
import { isLeft, These, getSemigroup } from 'fp-ts/These';
export const theseGetAlt = <E>(s: S.Semigroup<E>) => {
return <A>(b: Lazy<These<E, A>>) => {
const sgt = getSemigroup(s, S.first<A>());
return (a: These<E, A>): These<E, A> => (isLeft(a) ? sgt.concat(a, b()) : a);
};
}; Then try to mirror the implementation of // TheseT.ts
import * as S from 'fp-ts/Semigroup';
import { Monad } from 'fp-ts/Monad';
import { These } from 'fp-ts/These';
import { HKT } from 'fp-ts/HKT';
import { getAlt } from './These';
export const getAlt =
<M>(M: Monad<M>) =>
<E>(s: S.Semigroup<E>) =>
<A>(second: Lazy<HKT<M, These<E, A>>>) => {
const alt = theseGetAlt(s);
return (first: HKT<M, These<E, A>>) =>
M.chain(first, (t) =>
isLeft(t) ? M.chain(second(), (t2) => M.of(alt(() => t2)(t))) : M.of(t),
);
}; And finally inject // TaskThese.ts
import * as Task from 'fp-ts/Task';
import * as TheseT from './TheseT';
export const getAlt = TheseT.getAlt(Task.Monad); However, be aware that this is your definition of what an |
Beta Was this translation helpful? Give feedback.
-
After thinking about this a bit more, you are probably doing too much for an If anything at all, you could make Meaning export const theseAltW: <E2, B>(that: Lazy<These<E2, B>>) => <E1, A>(fa: These<E1, A>) => These<E1 | E2, A | B> =
(that) => (fa) => isLeft(fa) ? that() : fa;
export const theseAlt: <E, A>(that: Lazy<These<E2, B>>) => (fa: These<E, A>) => These<E, A> = theseAltW;
export const theseTAlt =
<M>(M: Monad<M>) =>
<A>(second: Lazy<HKT<M, These<E, A>>>) =>
(first: HKT<M, These<E, A>>) =>
M.chain(first, (t) => isLeft(t) ? second() : M.of(t));
export const taskTheseAlt = theseTAlt(Task.Monad); However, this really isn't what you described in your question, and if you do want/need what you described, I would suggest you call it a |
Beta Was this translation helpful? Give feedback.
-
Does this implementation make sense for
TaskThese#alt
implementation? Or maybe have I reinvented a wheel and can do this using existing method?Beta Was this translation helpful? Give feedback.
All reactions