Skip to main content

Module comonad

Module comonad 

Source
Expand description

Comonad operations for Pattern.

This module provides comonad operations (extract and extend) that work with Pattern’s “decorated sequence” semantics, where the value decorates the elements.

§Decorated Sequence Semantics

Pattern is fundamentally a “decorated sequence”:

  • Elements ARE the pattern - the actual content (e.g., ["A", "B", "A"])
  • Value DECORATES the elements - provides information about them (e.g., "sonata")

This is what makes Pattern a natural Comonad:

  • extract: Access the decorative information (the value)
  • extend: Compute new decorative information based on context (the subpattern)

§Examples

use pattern_core::Pattern;

// Create a pattern
let p = Pattern::point("root");

// Extract the decorative value
assert_eq!(p.extract(), &"root");

// Compute new decorations based on context
let depths = p.extend(&|subpattern| subpattern.depth());
assert_eq!(depths.extract(), &0); // Atomic pattern has depth 0