;; -*-theme-d-*-

;; Copyright (C) 2016, 2021, 2024 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.

;; Expected results: translation and running OK
;; Expected output:
;;   my-proc1: 1
;;   my-proc1: 2
;;   my-proc1: 3
;;   my-proc1: 4
;;   my-proc1: 5
;;   my-proc1: 100
;;   my-proc1: 101
;;   my-proc1: 102
;;   my-proc1: 103
;;   10.5
;;   20.5
;;   -30.5
;;   40.5
;;   50.5
;;   10.5 100
;;   20.5 101
;;   -30.5 102
;;   40.5 103
;;   ((1 . 1) (2 . 2) (3 . 3) (4 . 4) (5 . 5))
;;   ((1 . 10.5) (2 . 20.5) (3 . -30.5) (4 . 40.5) (5 . 50.5))
;;   ((100 . 100) (101 . 101) (102 . 102) (103 . 103))
;;   #t
;;   #f
;;   #f
;;   #t
;;   #t


(define-proper-program (examples nonpure-iterator-example)


  (import (standard-library core)
	  (standard-library basic-math)
	  (standard-library nonpure-iterator)
	  (standard-library console-io))


  (define-simple-method my-proc1 (((i <integer>)) (:pair <integer> <integer>)
				nonpure)
    (console-display "my-proc1: ")
    (console-display-line i)
    (cons i i))


  (define-main-proc (() <none> nonpure)
    (let* ((l1 '(1 2 3 4 5))
	   (l2 '(10.5 20.5 -30.5 40.5 50.5))
	   (v1 (mutable-vector <integer> 100 101 102 103))
	   (iter1 (get-list-nonpure-iterator l1))
	   (l3 (nonpure-iter-map1 my-proc1 iter1))
	   (iter2 (get-list-nonpure-iterator l2))
	   (l4 (nonpure-iter-map2 cons iter1 iter2))
	   (iter3 (get-mutable-vector-nonpure-iterator v1))
	   (l5 (nonpure-iter-map1 my-proc1 iter3))
	   (b1 (nonpure-iter-every1 (lambda (((i <integer>)) <boolean> pure)
			      (>= i 0))
			    iter1))
	   (b2 (nonpure-iter-every1 (lambda (((r <real>)) <boolean> pure)
			      (>= r 0.0))
			    iter2))
	   (b3 (nonpure-iter-every2 <= iter1 iter2))
	   (b4 (nonpure-iter-every2 (lambda (((i <integer>) (r <real>))
					     <boolean> pure)
			      (<= i (r-abs r)))
			    iter1 iter2))
	   (b5 (nonpure-iter-every2 >= iter3 iter2)))
      (nonpure-iter-for-each1 (lambda (((r <real>)) <none> nonpure)
				(console-display-line r))
			      iter2)
      (nonpure-iter-for-each2 (lambda (((r <real>) (i <integer>))
				       <none> nonpure)
				(console-display r)
				(console-display " ")
				(console-display i)
				(console-newline))
			      iter2 iter3)
      (console-display-line l3)
      (console-display-line l4)
      (console-display-line l5)
      (console-display-line b1)
      (console-display-line b2)
      (console-display-line b3)
      (console-display-line b4)
      (console-display-line b5))))
