shape-runtime 0.3.2

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// Require historical lookback before computing a function result.
///
/// `@warmup(period)` associates a lookback requirement with an annotated
/// function so tooling can reason about the amount of prior data needed before
/// the function should be considered ready.
///
/// @param period Number of prior data points required before the annotated
/// function should be evaluated as warmed up.
/// @note The current stdlib implementation keeps the pre-hook as a no-op and
/// trims post-processed results only when warmup state is present.
/// @see std::finance::annotations::indicator::@indicator
/// @example
/// @warmup(20)
/// @indicator
/// pub fn sma(data: Column<number>, period: number) -> number {
///     return data.rolling(period).mean();
/// }
annotation warmup(period) {
    // Called before each function invocation
    // self = the annotated function (e.g., sma)
    // period = annotation parameter (e.g., 20, or days+1)
    before(args, ctx) {
        // Data source reloading is removed from stdlib.
        // Keep warmup annotation as a no-op pre-hook.
        None
    }

    // Called after each function invocation
    // self = the annotated function
    after(args, result, ctx) {
        let orig_len = ctx.get("state").get("original_length")

        // If no state (before didn't extend), return as-is
        if orig_len == None {
            return result
        }

        // Trim result back to original length
        result.slice(result.len() - orig_len, orig_len)
    }

    // Return static metadata
    metadata() {
        return {
            warmup_period: period,
            requires_historical_data: true
        }
    }
}