;; Pipeline Macros Example
;; Demonstrates a small macro-driven data pipeline

(import assert :refer :all)
(import binding :refer :all)
(import control :refer :all)
(import display :refer :all)
(import looping :refer :all)
(import threading :refer :all)

;; Pipeline macro: threads a value through a sequence of steps
;;' @description Thread value through steps (last argument).
(defmacro pipeline (value . steps)
  (if (null? steps)
      value
      (let ((step (car steps))
            (rest-steps (cdr steps)))
        (if (list-or-pair? step)
            `(pipeline (,(car step) ,@(cdr step) ,value) ,@rest-steps)
            `(pipeline (,step ,value) ,@rest-steps)))))

(define data (list 1 2 3 4 5 6 7 8 9))

(define pipeline-result
  (pipeline data
            (filter (lambda (x) (> x 0)))
            (map (lambda (x) x))
            (reduce +)))

(println "=== Pipeline Output ===")
(assert-equal (list 1 2 3 4 5 6 7 8 9) data)
(println (string-concat "Input: " data))
(assert-equal 45 pipeline-result)
(println (string-concat "Pipeline result: " pipeline-result))

(define expanded (macroexpand-1
                  '(pipeline data
                             (filter (lambda (x) (> x 0)))
                             (map (lambda (x) x))
                             (reduce +))))

(println "\nExpanded form:")
(println expanded)

(println "\nExample complete!")
