<commit_before>;;; -*- Gerbil -*-
;;; © vyzo
;;; Lazy evaluation
package: std

(export
  (rename: delay-lazy lazy)
  (rename: delay-eager delay)
  (rename: force* force)
  lazy?)

(defstruct lazy (e)
  final: #t unchecked: #t)

(defrules delay-lazy ()
  ((_ expr)
   (make-lazy (cons 'lazy (lambda () expr)))))

(defrules delay-eager ()
  ((_ expr)
   (delay-lazy (make-lazy (cons 'eager expr)))))

(def (force* obj)
  (if (lazy? obj)
    (force-lazy obj)
    (force obj)))

(def (force-lazy p)
  (declare (not safe))
  (let (content (&lazy-e p))
    (case (##car content)
      ((eager)
       (##cdr content))
      ((lazy)
       (let* ((p* ((##cdr content)))
              (content (&lazy-e p)))
         (unless (eq? (##car content) 'eager) ; reentrance test
           (if (lazy? p*)<commit_msg>std/lazy: finetune mixing base promises and lazy ones<commit_after>;;; -*- Gerbil -*-
;;; © vyzo
;;; Lazy evaluation
package: std

(export
  (rename: delay-lazy lazy)
  (rename: delay-eager delay)
  (rename: force* force)
  lazy?)

(defstruct lazy (e)
  final: #t unchecked: #t)

(defrules delay-lazy ()
  ((_ expr)
   (make-lazy (cons 'lazy (lambda () expr)))))

(defrules delay-eager ()
  ((_ expr)
   (delay-lazy (make-lazy (cons 'eager expr)))))

(def (force* obj)
  (if (lazy? obj)
    (force-lazy obj)
    (force obj)))

(def (force-lazy p)
  (declare (not safe))
  (let (content (&lazy-e p))
    (case (##car content)
      ((resolved)
       (##cdr content))
      ((eager)
       (let (val (force* (##cdr content)))
         (if (eq? (##car content) 'eager) ; reentrance test
           (begin
             (##set-car! content 'resolved)
             (##set-cdr! content val)
             val)
           (##cdr content))))
      ((lazy)
       (let* ((p* ((##cdr content)))
              (content (&lazy-e p)))
         (when (eq? (##car content) 'lazy) ; reentrance test
           (if (lazy? p*)
{'fraction_non_alphanumeric': 0.19234800838574423, 'fraction_numerical': 0.0, 'mean_word_length': 2.4963369963369964, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6563378', 'n_tokens_mistral': 722, 'n_tokens_neox': 728, 'n_words': 201}