Stellarmorphism.Recursion (stellarmorphism v1.0.0)

View Source

Phase 1: Asteroid and Rocket recursion support for Stellarmorphism.

  • asteroid/1 creates eagerly-evaluated recursive structures
  • rocket/1 creates lazily-evaluated recursive structures
  • launch/1 evaluates rockets to get their values

Summary

Functions

Creates an eagerly-evaluated recursive structure.

Deeply launches all rockets in a nested structure.

Evaluates a rocket to get its value.

Creates a lazily-evaluated recursive structure.

Counts the depth of nested rockets without launching them.

Functions

asteroid(expr)

(macro)

Creates an eagerly-evaluated recursive structure.

The expression is evaluated immediately when the asteroid is created, making it suitable for structures that need immediate computation.

Examples

# In a star definition
core Node,
  value: 42,
  left: asteroid(core Leaf, value: 1),
  right: asteroid(core Leaf, value: 3)

# Direct access - no special functions needed
left_value = tree.left.value  # Immediate access

deep_launch(value)

Deeply launches all rockets in a nested structure.

Useful for fully evaluating lazy structures when needed.

launch(rocket_value)

(macro)

Evaluates a rocket to get its value.

If the value is a rocket (tagged tuple), it calls the stored function. If it's not a rocket, it returns the value as-is.

Examples

rocket_value = rocket(fn -> expensive_computation() end)
result = launch(rocket_value)  # Computes and returns result

normal_value = 42
result = launch(normal_value)  # Returns 42 directly

rocket(expr)

(macro)

Creates a lazily-evaluated recursive structure.

The expression is wrapped in a function and only evaluated when launch/1 is called, making it suitable for infinite sequences or expensive computations that should be deferred.

Examples

# In a star definition
core Cons,
  head: 1,
  tail: rocket(fn -> build_next_stream() end)

# Launch rocket to access value
next_values = launch(stream.tail)  # Computed when launched

rocket_depth(value)

Counts the depth of nested rockets without launching them.

Useful for debugging infinite or deeply nested lazy structures.