Skip to content

Commit

Permalink
feat(Cause): add Cause#stripSomeDefects
Browse files Browse the repository at this point in the history
  • Loading branch information
0x706b committed Jun 16, 2022
1 parent eecc62c commit dd771bc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-buttons-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fncts/base": patch
---

feat(Cause): add Cause#stripSomeDefects
33 changes: 33 additions & 0 deletions packages/base/src/data/Cause/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,39 @@ export function stripFailures<A>(self: Cause<A>): Cause<never> {
});
}

/**
* Remove all `Halt` causes that the specified partial function is defined at,
* returning `Just` with the remaining causes or `Nothing` if there are no
* remaining causes.
*
* @tsplus fluent fncts.Cause stripSomeDefects
*/
export function stripSomeDefects<E>(self: Cause<E>, p: Predicate<unknown>): Maybe<Cause<E>> {
return self.fold({
Empty: () => Just(Empty()),
Fail: (e, trace) => Just(Fail(e, trace)),
Halt: (t, trace) => (p(t) ? Nothing() : Just(Halt(t, trace))),
Interrupt: (fiberId, trace) => Just(Interrupt(fiberId, trace)),
Then: (l, r) =>
l.isJust() && r.isJust()
? Just(Then(l.value, r.value))
: l.isJust()
? Just(l.value)
: r.isJust()
? Just(r.value)
: Nothing(),
Both: (l, r) =>
l.isJust() && r.isJust()
? Just(Then(l.value, r.value))
: l.isJust()
? Just(l.value)
: r.isJust()
? Just(r.value)
: Nothing(),
Stackless: (causeOption, stackless) => causeOption.map((cause) => Stackless(cause, stackless)),
});
}

function sequenceCauseEitherEval<E, A>(self: Cause<Either<E, A>>): Eval<Either<Cause<E>, A>> {
switch (self._tag) {
case CauseTag.Empty: {
Expand Down

0 comments on commit dd771bc

Please sign in to comment.