maybeCombine<T> method

Maybe<T> maybeCombine<T>({
  1. Maybe<T> firstJust(
    1. K
    )?,
  2. Maybe<T> secondJust(
    1. J
    )?,
  3. Maybe<T> bothJust(
    1. K,
    2. J
    )?,
  4. Maybe<T> bothNothing()?,
})

Use it to combine two different Maybe's into a new one. Input firstJust to map case where only the first value is Just, secondJust to map case where only the second value is Just, bothJust to map case where both first and second value are Just and bothNothing to map case where both are Nothing Example:


    Maybe<Number> combined = (testString, testInt).maybeCombine<Number>(
      bothJust: (val, number) => Just(Number(val, number, '$number$val',)),
      firstJust: (val) => Just(Number(val, -1, '-1$val',)),
      secondJust: (number) => Just(Number('not a trivia', number, 'NonTrivia',)),
      bothNothing: () => Just(Number('not a trivia', -1, 'NonTrivia',)),
    );


Implementation

Maybe<T> maybeCombine<T>({
  /// Used to map case where only the first value is [Just]
  Maybe<T> Function(K)? firstJust,

  /// Used to map case where only the second value is [Just]
  Maybe<T> Function(J)? secondJust,

  /// Used to map case where both values are [Just]
  Maybe<T> Function(K, J)? bothJust,

  /// Used to map case where both values are [Nothing]
  Maybe<T> Function()? bothNothing,
}) {
  return $1.when(
    nothing: () => $2.when(
      nothing: () => bothNothing != null ? bothNothing() : Nothing<T>(),
      just: (J secondData) =>
          secondJust != null ? secondJust(secondData) : Nothing<T>(),
    ),
    just: (firstData) => $2.when(
      nothing: () => firstJust != null ? firstJust(firstData) : Nothing<T>(),
      just: (J secondData) =>
          bothJust != null ? bothJust(firstData, secondData) : Nothing<T>(),
    ),
  );
}