How to represent a function with a boolean return type as an Effect for mapping?

Hello.

How do I represent a function that returns a boolean as an Effect, so that I can map or map error based on the result?

const fromPredicateE = <A>(f: () => boolean): Effect.Effect<undefined, PredicateFailedError, A> =>
  Effect.suspend(() =>
    f()
      ? Effect.succeed(undefined)
      : Effect.fail(new PredicateFailedError({ message: 'Predicate failed' })))

const generateDates = (
  timeframe: Timeframe,
  startDate: Date,
  endDate: Date,
  dates: Array<Date> = [],
): Effect.Effect<Array<Date>, Cause.NoSuchElementException | PredicateFailedError, unknown> =>
  pipe(
    fromPredicateE(() =>
      startDate.valueOf() < endDate.valueOf()),
    Effect.flatMap(() =>
      Effect.succeed(dates)),
    Effect.catchAll(() =>
      pipe(
        Effect.Do,
        Effect.bind('ms', () =>
          toMs(timeframe)),
        Effect.bind('newDate', ({ ms }) =>
          add(timeframe, startDate, ms)),
        Effect.flatMap(({ newDate }) =>
          generateDates(timeframe, newDate, endDate, [...dates, newDate])),
      )),
  )


That's how I did it now but I bet there's a better/cleaner way 🙂
Was this page helpful?