Applies a function wrapped in a Resource to the Data
value, supporting applicative functor operations.
For Data
, applies the function if the input is Data
; otherwise, returns unchanged.
const fnRes = Resource.Data((x: number) => x + 1, { id: '123' });
const dataRes = Resource.Data(42, { id: '123' });
const result = fnRes.ap(dataRes); // Data<43, { id: '123' }>
const failureFn = Resource.Failure<(x: number) => number>(['fn error']);
const result2 = failureFn.ap(dataRes); // Failure<number> (propagates failure from function resource)
const failureVal = Resource.Failure<number>(['val error']);
const result3 = fnRes.ap(failureVal); // Failure<number> (propagates failure from value resource)
Chains a function that returns a Resource, enabling monadic composition.
For Data
, applies the function; for others, returns unchanged.
Returns the Data
value or a fallback value for non-Data
states.
Transforms the Data
value using a function, preserving the Resource structure.
Acts as a functor operation, similar to Array.map
. Non-Data
variants return unchanged.
Safely transforms the Data
value, catching errors and returning Failure
if the function throws.
Useful for error-prone operations (e.g., parsing JSON).
Matches the Resource state with a pattern, returning a value.
Enables declarative state handling, similar to a switch
statement.
Object with functions for each Resource state.
The result of the matching function.
Matches the Resource state with a partial pattern, performing side effects.
Partial object with functions for some Resource states.
Lifts a value into a Data
Resource, preserving existing params
(if any).
Acts as the monadic pure
operation.
Transitions to a Query
state with new parameters.
Interface for Resource methods, implementing functor, applicative functor, and monad operations.