Skip to main content

Crate khive_fold

Crate khive_fold 

Source
Expand description

khive-fold: Cognitive primitives — Fold, Anchor, Objective, Selector.

Four cognitive primitives that form the “paper-folding” operation:

  • Fold: entries → derived state (deterministic reduce)
  • Anchor: causal graph traversal (provenance chains)
  • Objective: score candidates and select best
  • Selector: budget-constrained pack (many → subset)

Plus deterministic ordering primitives, composition combinators, and common strategies (Recency, Relevance, Weighted, etc.).

§Quick Start

use khive_fold::{fold_fn, Fold, FoldContext};

let counter = fold_fn(
    |_ctx| 0usize,
    |count, _entry: &i32, _ctx| count + 1,
);

let entries = [1, 2, 3, 4, 5];
let result = counter.derive(entries.iter(), &FoldContext::new());
assert_eq!(result.state, 5);

Re-exports§

pub use anchor::Anchor;
pub use anchor::AnchorGraph;
pub use anchor::AnchorRef;
pub use anchor::BfsAnchor;
pub use selector::GreedySelector;
pub use selector::Selector;
pub use selector::SelectorInput;
pub use selector::SelectorOutput;
pub use selector::SelectorWeights;
pub use objective::builtin::FirstMatchObjective;
pub use objective::builtin::HasImportance;
pub use objective::builtin::HasTimestamp;
pub use objective::builtin::ImportanceObjective;
pub use objective::builtin::MaxScoreObjective;
pub use objective::builtin::RecencyObjective;
pub use objective::builtin::RelevanceObjective;
pub use objective::builtin::ThresholdObjective;
pub use objective::compose::ConsensusObjective;
pub use objective::compose::NegateObjective;
pub use objective::compose::PriorityObjective;
pub use objective::compose::ScaleObjective;
pub use objective::compose::UnionObjective;
pub use objective::compose::WeightedObjective;
pub use objective::error::ObjectiveError;
pub use objective::error::ObjectiveResult;
pub use objective::objective_fn;
pub use objective::DeterministicObjective;
pub use objective::Objective;
pub use objective::ObjectiveContext;
pub use objective::Selection;
pub use ordering::canonical_f32;
pub use ordering::canonical_f64;
pub use ordering::cmp_asc_score_then_id;
pub use ordering::cmp_desc_score_then_id;
pub use ordering::HasId;
pub use ordering::ScoredEntry;

Modules§

anchor
Anchor: causal structure.
objective
Objective function framework — scoring, selection, composition.
ordering
Deterministic ordering primitives for objective functions
selector
Selector: many → subset under budget.

Structs§

AnyFold
A zero-allocation existential fold with a function-pointer predicate.
CountFold
A zero-allocation count fold.
DeterministicScore
DualFold
Dual fold — run two folds independently over the same entries.
FilterCountFold
A zero-allocation count fold with a function-pointer predicate.
FilterFold
Filter fold — only process entries matching a predicate.
FnFold
Helper to create a fold from closures.
FoldContext
Context for fold operations.
FoldOutcome
Outcome of a fold operation.
MapFold
Map fold — transform entries before folding.
QuantKey
8-byte packed sort key: i32 quantized score + u32 ID prefix.
Ranked
Ranked item: score descending, ID ascending for ties.
SequentialFold
Sequential fold — run one fold, then use its output to inform another.
SharedJson
Shared JSON value backed by Arc<serde_json::Value>.
SumI64Fold
A zero-allocation i64 summation fold with a function-pointer projection.

Enums§

CommonFold
Enum-dispatch fold for common patterns that would otherwise use Box<dyn Fold>.
CommonFoldState
Unified state returned by CommonFold.
FoldError
Errors that can occur in fold operations
FoldFailure
Failure returned by fallible fold operations.

Traits§

Fold
Core fold trait for deriving state from entries.
TryFold
Fallible fold step API for reducers that can reject invalid state shapes.

Functions§

cmp_asc_then_id
Compare scores ascending, lower ID wins ties.
cmp_desc_then_id
Compare scores descending, lower ID wins ties.
filter
Helper to create a filter fold.
fold_fn
Create a fold from just initial and step functions (no finalize).
map
Helper to create a map fold.

Type Aliases§

BoxedFold
A boxed fold for dynamic dispatch.
FoldResult
Result type for fold operations
FoldResultType
Result type for fold operations