aboutsummaryrefslogtreecommitdiffhomepage
path: root/gtwiwtg.lisp
blob: 4e433470a5df9bd15831c0dae640f97a1db9fc39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
(defpackage #:gtwiwtg (:use #:cl))
(in-package :gtwiwtg)

;;; Generator Protocol ;;;

(defgeneric next (gen)
  (:documentation "gets next if available. Throws an error otherwise."))

(defgeneric has-next-p (gen)
  (:documentation "returns true if next can be called on this generator!"))

(defgeneric stop (gen)
  (:documentation "stops the generator"))

;;; Base Generator Class ;;; 

(defclass generator! ()
  ((dirty-p
    :accessor dirty-p
    :initform nil
    :documentation "Indicates whether or not this any of this
    generator has been consumed, or should behave as if it has been.")
   (stopped-p
    :accessor stopped-p
    :initform nil
    :documentation "Indicates whether or not this generator has been
    explicitly stopped.")))

(defmethod stop ((g generator!))
  (setf (stopped-p g) t))

(defun make-dirty (g) (setf (dirty-p g) t))

;;; Utility Class Builder ;;; 

(eval-when (:compile-toplevel :load-toplevel :execute)
  (defun make-keyword (symb)
    (read-from-string (format nil ":~a" symb))))

(defmacro a-class (name supers &rest slots)
  `(defclass ,name ,supers
     ,(mapcar (lambda (def)
                 (if (consp def)
                     `(,(car def)
                        :initarg ,(make-keyword (car def))
                        :initform ,(second def))
                     `(,def :initarg ,(make-keyword def)
                            :initform nil)))
               slots)))

;;; Generator Classes ;;; 

(a-class range-backed-generator! (generator!)
         (at 0) to (by 1) inclusive (comparator #'<))

(defmethod has-next-p ((g range-backed-generator!))
  (with-slots (to current comparator by at stopped-p) g
    (unless stopped-p
      (or (not to)
          (funcall comparator
                   (+ by at)
                   to)))))

(defmethod next ((g range-backed-generator!))
  (with-slots (at by) g
    (incf at by)
    at))

(a-class sequence-backed-generator! (generator!)
         sequence index)

(defmethod has-next-p ((g sequence-backed-generator!))
  (with-slots (stopped-p index sequence) g
    (unless stopped-p
      (< index (1- (length sequence))))))

(defmethod next ((g sequence-backed-generator!))
  (with-slots (index sequence) g
    (incf index)
    (elt sequence index)))

(a-class list-backed-generator! (generator!)
         list)

(defmethod has-next-p ((g list-backed-generator!))
  (with-slots (stopped-p list) g
    (unless stopped-p (consp list))))

(defmethod next ((g list-backed-generator!))
  (pop (slot-value g 'list)))

(a-class thunk-backed-generator! (generator!)
         next-p-fn
         next-fn
         stop-fn)

(defmethod has-next-p ((g thunk-backed-generator!))
  (with-slots (stopped-p next-p-fn) g
    (unless stopped-p (funcall next-p-fn))))

(defmethod next ((g thunk-backed-generator!))
  (funcall (slot-value g 'next-fn)))

(defmethod stop :after ((g thunk-backed-generator!))
  (with-slots (stop-fn) g
    (when stop-fn
      (funcall stop-fn))))

(a-class stream-backed-generator! (generator!)
         stream reader)

(defmethod has-next-p ((g stream-backed-generator!))
  (with-slots (stopped-p stream) g
    (unless stopped-p
      (open-stream-p stream))))

(defmethod next ((g stream-backed-generator!))
  (with-slots (reader stream) g
    (let ((read-value (funcall reader stream)))
      (unless read-value
        (close stream))
      read-value)))

(defmethod stop :after ((g stream-backed-generator!))
  (close (slot-value g 'stream)))         

;;; CONSTRUCTORS

(defun range (&key (from 0) to (by 1) inclusive)
  "Create a generator that produces a series of numbers between FROM
and TO with step size of BY.

When INCLUSIVE is non NIL, then TO will be produced by the generator
if it would be the last member of generate series.  

E.g. 

> (collect (range :to 10))
 
 (0 1 2 3 4 5 6 7 8 9) 

> (collect (range :to 10 :inclusive t))

 (0 1 2 3 4 5 6 7 8 9 10)

> (collect (range :to 10 :by 2 :inclusive t))

 (0 2 4 6 8 10)

> (collect (range :to 10 :by 3 :inclusive t))

 (0 3 6 9)

If TO is NIL, then the generator produces an infinite sequence.

"
  (let ((comparator (if (plusp by)
                        (if inclusive #'<= #'<)
                        (if inclusive #'>= #'>))))
    (make-instance 'range-backed-generator!
                   :comparator comparator
                   :inclusive inclusive
                   :at (- from by)
                   :to to
                   :by by)))

(defun times (n)
  "Shorthand for (RANGE :TO N)"
  (range :to n))

(defun seq (sequence &key (start 0))
  "Turns a sequecne (a list, vector, string, etc) into a
generator. The resulting generator will generate exactly the memebers
of the sequence."
  (assert (typep sequence 'sequence))
  (if (consp sequence)
      (make-instance 'list-backed-generator!
                     :list (nthcdr start sequence))
      (make-instance 'sequence-backed-generator!
                     :sequence sequence
                     :index (1- start))))

(defun from-thunk-until (thunk &optional (until (constantly nil)) clean-up)
  "Creates a generator that produces a series of value by successively
calling (FUNCALL THUNK).  The iterator stops whenever (FUNCALL UNTIL)
is non null.

If a CLEAN-UP thunk is supplied, it will be run after consumption of
the new generator has finished. I.e. when passing this form to a
consumer such as FOR, FOLD, COLLECT, etc.

By default, UNTIL is the function (CONSTANTLY NIL). I.e. it will
generate forever."
  (assert (and (every #'functionp (list thunk until))
               (or (null clean-up) (functionp clean-up))))
  (make-instance 'thunk-backed-generator! 
                 :stop-fn clean-up
                 :next-p-fn (complement until)
                 :next-fn thunk))


(defun from-thunk (thunk)
  "Creates a generator that produces an inifinte series of values that
are the return value of (FUNCALL THUNK). 

If you need to create a stopping condition on your thunk-backed
generator, see FROM-THUNK-UNTIL."
  (from-thunk-until thunk))


(defun from-thunk-times (thunk times)
  "Creates a generator that produces its values by calling 
  (FUNCALL THUNK) exactly TIMES times."
  (let ((thunk-proxy (lambda (ignore) (declare (ignore ignore)) (funcall thunk))))
    (map! thunk-proxy (times times))))

(defun from-recurrence (rec n-1 &rest n-m)
  "Creates a generator from a recurrence relation.

REC is a function of K arguments.

The Nth value of the series generated by the new generator is the result of
calling REC on the previoius K results.

N-1 and N-M are used to initialize the recurrence. (1+ (LENGTH N-M))
should be K, the number of arguments acepted by REC.

Example

> (let ((fibs (from-recurrence #'+ 1 0)))
     (take 10 fibs))

(1 2 3 5 8 13 21 34 55 89)

"
  (let* ((history (cons n-1 n-m))
         (thunk  (lambda ()
                   (let ((nth (apply rec history)))
                     (setf history (cons nth (butlast history)))
                     nth))))
    (from-thunk thunk)))


(defun repeater (&rest args)
  "Produces a generator that produces an infinite series consisting in
the values passed as ARGS looped forever."
  (let ((state (copy-list args)))
    (from-thunk
     (lambda ()
       (unless state
         (setf state (copy-list args)))
       (pop state)))))


(defun noise (&optional (arg 1.0))
  "Creates a generator that produces an infinite series of random
  numbers that are the result of calling (RANDOM ARG)."
  (from-thunk (lambda () (random arg))))


(defun from-input-stream (stream stream-reader)
  "Create a generator from a STREAM.

You must supply as STREAM-READER function that accepts the stream as
its only argument and returns NIL if the stream has run out of input,
Non-NIL otherwise.

A quirk is that the last value returned from this generator is NIL.

Consumers of the new generator will ensure that the stream is properly
closed..

Here is an example:

 (take 2 (from-input-stream
          (open \"hey.txt\")
          (lambda (s) (read-char s nil nil))))

 (#\\h #\\e)

"
  (make-instance 'stream-backed-generator!
                 :stream stream
                 :reader stream-reader))


(defun file-lines (file)
  "Creates a generator that produces the lines of a file.  The stream
to the file is closed when the generator finishes.

FILE is either a path to a file.

Returns NIL on the last iteration.

"
  (from-input-stream (open file)
                     (lambda (stream) (read-line stream nil nil))))

(defun file-chars (file)
  "Creates a generator that produces the characters of a file. The
stream to the file is closed when the generator finishes.

FILE is either a path to a file.

Returns NIL on the last iteration.
"
  (from-input-stream (open file)
                     (lambda (stream) (read-char stream nil nil))))

(defun file-bytes (file)
  "Creates a generator that produces the bytes of a file. The
stream to the file is closed when the generator finishes.

FILE is either a path to a file.

Returns NIL on the last iteration.
"
  (from-input-stream (open file :element-type '(unsigned-byte 8))
                     (lambda (stream) (read-byte stream nil nil))))

;;; Some utilities

(defun make-queue ()
  (cons nil nil))

(defun enqueue (x q) 
  (push x (car q)))

(defun dequeue (q)
  (when (and (car q) (null (cdr q)))
    (setf (cdr q) (reverse (car q))
          (car q) nil))
  (when (cdr q) (pop (cdr q))))
             
(defun queue-empty-p (q)
  (and (null (car q))
       (null (cdr q))))

;;; Some assertion tests

(defun all-different (things)
  (= (length things) (length (remove-duplicates things))))

(defun all-clean (gens)
  (every (complement #'dirty-p) gens))

(defun all-good (gens)
  (and (all-clean gens) (all-different gens)))

(defun sully-when-clean (gens)
  (assert (all-good gens))
  (dolist (g gens) (make-dirty g)))

;;; MODIFIERS and COMBINATORS

(defun map! (map-fn gen &rest gens)
  "Maps a function over a number of generators, returning a generator
that produces values that result from calling MAP-FN on those
generators' elements, in sequence.

The resulting generator will stop producing values as soon as any one
of the source generators runs out of arguments to pass to
MAP-FN. I.e. The mapped generator is as long as the shortest argument
generators.

Error Conditions:
 - If any of the generators compare EQL an error will be signalled
 - If any of the generators have been used elsewhere, an error will be signalled.
"
  (let ((all-gens (list* gen gens)))
    (sully-when-clean all-gens)
    (from-thunk-until
     (lambda ()
       (apply map-fn (mapcar #'next all-gens)))
     (lambda ()
       (some (complement #'has-next-p) all-gens)) ; when at least one has no next value
     (lambda ()
       (dolist (g all-gens) (stop g))))))

(defun filter! (pred gen)
  "Produces a generator that filters out members of GEN that are NIL
when applied to PRED.

Error Condition:
 - If GEN has been used elsewhere, an error will be signalled.
"
  (sully-when-clean (list gen))
  (let (on-deck)
    (from-thunk-until
     (lambda () on-deck) ; consumers always call has-next-p before next
     (lambda ()
       (loop
          :while (has-next-p gen)
          :for candidate = (next gen)
          :when (funcall pred candidate)
          :do (progn
                (setf on-deck candidate)
                (return nil))   ; Don't stop generating, we found one
          :finally (return t))) ; Stop generating, we can't find one.
     (lambda ()
       (stop gen)))))


(defun inflate! (fn gen)
  "FN is expected to be a function that accepts elements of GEN and
returns a new generator.  

The generator (INFLATE! FN GEN) generates each element of an
intermediate generator (FN X) for each X generated by GEN.

Here is an example:

> (let ((keys (seq '(:name :occupation :hobbies)))
        (vals (seq '(\"buckaroo banzai\" 
                     \"rocker\" 
                     (\"neuroscience\" \"particle physics\" \"piloting fighter jets\")))))
     (collect (inflate! #'seq (zip! keys vals))))

 (:NAME \"buckaroo banzai\" 
  :OCCUPATION \"rocker\" 
  :HOBBIES (\"neuroscience\" \"particle physics\" \"piloting fighter jets\"))

Error Conditions:
 - If GEN has been used elsewhere, an error will be signalled.
 - If GEN is an empty generator, an error will be signalled.
"
  (sully-when-clean (list gen))
  (let ((sub-gen (funcall fn (next gen))))
    (from-thunk-until
     (lambda () (next sub-gen))

     (lambda ()
       (loop
          :until (has-next-p sub-gen)
          :while (has-next-p gen)
          :do
            (stop sub-gen)
            (setf sub-gen (funcall fn (next gen))))

       ;; the 'until' thunk must return t when we should stop generating
       ;; hence:
       (not (or (has-next-p sub-gen)
                (has-next-p gen))))

     (lambda ()
       (stop gen)
       (when sub-gen (stop sub-gen))))))


(defun concat! (gen &rest gens)
  "Returns a generator that is the concatenation of the generators
passed as arguments.

Error Conditions:
 - If any of the generators compare EQL, an error will be signalled.
 - If any of the generators has been used elsewhere, an error will be sigalled.
"
  (sully-when-clean (list* gen gens))
  (inflate! #'identity (seq (list* gen gens))))

(defun zip! (gen &rest gens)
  "Is a shortcut for (MAP! #'LIST GEN1 GEN2 ...)"
  (apply #'map! #'list gen gens))


(defun merge! (comparator gen1 gen2 &rest gens)
  "Emulates the behavior of MERGE (in the ANSI standard), but for generators.

The emulation is not perfect, but it holds in the following sense: If
all the inputs are sorted according to COMPARATOR then the output will
also be sorted according to COMPARATOR.

The generator created through a merge has a length that is the sum of
the lengths of the arguments to MERGE!. Hence, if any of the arguments
is an infinite generator, then the new generator is also infinite.

An example:

> (collect (merge! #'< 
                  (times 4) 
                  (range :from 4 :to 10 :by 2)
                  (range :from -10 :to 28 :by 6)))

 (-10 -4 0 1 2 2 3 4 6 8 8 14 20 26)

Error Conditions:
 - If any of the generators compare EQL, an error will be signalled.
 - If any of the generators have been used elsewhere, an error will be signalled.
"
  (let ((all-gens (list* gen1 gen2 gens)))
    (sully-when-clean all-gens)
    (from-thunk-until
     (lambda ()
       (let ((vals (mapcar #'next all-gens)))
         (setq vals (sort vals comparator))

         (setf all-gens
               (delete-if-not #'has-next-p
                              (nconc (when (cdr vals) (list (seq (cdr vals))))
                                     all-gens)))
         (car vals)))

     (lambda ()
       (null all-gens))

     (lambda ()
       (dolist (g all-gens) (stop g))))))


(defun skip! (n gen)
  "EXPERIMENTAL. Might be removed.

Returns the generator GEN but without the first N elements. 

Caveats:

 - It is possible that SKIP! will skip the whole source generator,
   returning an empty gernator.
"
  (dotimes (x n) (when (has-next-p gen) (next gen)))
  (setf (slot-value gen 'dirty-p) nil)
  gen)

(defun skip-while! (pred gen)
  "EXPERIMENTAL. Might be removed.

Returns the generator GEN but without the first N elements for
which PRED is non-nil. 

Caveat:
 - It is possible that, for finite generators, skip-while! will skip
   the whole generated sequence. 
 - If the generator is infinite, then skip-while! MIGHT never return.
"
  (loop :while (and (has-next-p gen)
                    (funcall pred (next gen))))
  (setf (slot-value gen 'dirty-p) nil)
  gen)


(defun nfurcate! (count source-generator)
  "EXERIMENTAL. MAY BE REMOVED

Return a list of COUNT copies of GEN.

Caveat: 

 - The generators produced by NFURCATE! allocate new memory as they
   are consumed. This allocation can be O(COUNT * SIZE) in space
   complexity, where SIZE is the size of GEN. Hence, use with caution
   on infinite generators.
"
  (make-dirty source-generator)
  (let ((qs (loop :for _ :below count :collect (make-queue)))
        (stop-cells (loop :for _ :below count :collect (list nil))))
    (loop
       :for build-q :in qs
       :for stop-cell :in stop-cells  
       :collect
         (let ((local-q build-q)
               (local-stop stop-cell))

           (from-thunk-until
            (lambda ()
              (cond ((not (queue-empty-p local-q))
                     (dequeue local-q))

                    ((has-next-p source-generator)
                     (let ((next-v (next source-generator)))
                       (loop :for q :in qs :do (enqueue next-v q))
                       (dequeue local-q)))

                    (t (error "Attempted to get next from a spent generator."))))

            (lambda ()
              (and (not (has-next-p source-generator))
                   (queue-empty-p local-q)))

            (lambda ()
              (setf (car local-stop) t)
              (when (every #'car stop-cells)
                (stop source-generator))))))))

(defun partition! (pred gen)
  "EXPERIMENTAL. MAY BE REMOVED. 

Return a list of two generators that looks like (PASSING FAILING)

PASSING is a generator that produces the values of GEN that pass the
predicate PRED 

FAILING is a generator that produces the values of GEN that do not
pass the predicate PRED

Caveat: 

 - The generators produced by PARTITION! allocate new memory as you
   consume them. This allocation may be O(2 * SIZE) in space
   complexity, where SIZE is the size of GEN. Hence, use with caution
   on infinite generators.
"
  (destructuring-bind (gen1 gen2) (nfurcate! 2 gen)
    (list (filter! pred gen1)
          (filter! (complement pred) gen2))))

;;; CONSUMERS

(defmacro for (var-exp gen &body body)
  "The basic generator consumer.

VAR-EXP can be either a symbol, or a form suitable for using as the
binding form in a DESTRUCTURING-BIND.

GEN is an expression that should evaluate to a generator.

BODY is a list of any forms you like. These forms will be evaluated
for each value produced by GEN.

FOR akes care of running any clean up that the generator
requires. E.g. If the generator is backed by an open stream, the
stream will be closed. E.g. If the generator was built using
FROM-THUNK-UNTIL, then the CLEAN-UP thunk will be run before FOR
exits.

Every other consumer is built on top of FOR, and hence, every other
consumer will also perform clean up.

Example:

 (for (x y) (zip! (repeater 'a 'b 'c) (times 5))
   (format t \"~a -- ~a~%\" x y))

A -- 0
B -- 1
A -- 2
B -- 3
A -- 4

"
  (let* ((gen-var (gensym "generator!"))
         (expr-body (if (consp var-exp)
                       `(destructuring-bind ,var-exp (next ,gen-var) ,@body)
                       `(let ((,var-exp (next ,gen-var))) ,@body))))
    `(let ((,gen-var ,gen))
       (loop
          :while (has-next-p ,gen-var)
          :do
            ,expr-body)
       (stop ,gen-var))))

(defmacro fold ((acc init-val) (var-exp gen) expr)
  "The accumulating generator consumer.

ACC is a symbol and INIT-VAL is any lisp expression.  ACC is where
intermediate results are accmulated. INIT-VAL is evaluated to
initialize ACC.

VAR-EXP can be either a symbol, or a form suitable for using as the
binding form in DESTRUCTURING-BIND.

GEN is an expression that should evaluate to a generator.

EXPR is a sigle lisp expression the value of which becomes bound to
ACC on each iteration.

When iteration has concluded, ACC becomes the value of the FOLD form.

Example: standard summing

> (fold (sum 0) (x (times 10)) (+ sum x))

45

Example: a usless calculation

> (fold (acc 0)
        ((x y) (zip! (times 10) (range :by -1)))
        (sqrt (+ acc (* x y))))

 #C(0.444279 8.986663)

Example: building data 

> (fold (plist nil) 
        ((key val)
         (zip! (seq '(:name :occupation :hobbies))
               (seq '(\"buckaroo banzai\" 
                      \"rocker\" 
                      (\"neuroscience\" \"particle physics\" \"piloting fighter jets\")))))
         (cons key (cons val plist)))

 (:HOBBIES (\"neuroscience\" \"particle physics\" \"piloting fighter jets\")
  :OCCUPATION \"rocker\" :NAME \"buckaroo banzai\")

 "
  `(let ((,acc ,init-val))
     (for ,var-exp ,gen
       (setf ,acc ,expr))
     ,acc))


(defun collect (gen)
  "Consumes GEN by collecting its values into a list."
  (nreverse (fold (xs nil) (x gen) (cons x xs))))

(defun take (n gen)
  "Consumes GEN by collecting its first N values into a list"
  (nreverse (fold (xs nil) (x (zip! gen (times n)))
                  (cons (car x) xs))))

(defun pick-out (indexes gen)
  "Consumes GEN by picking out certain members by their index.

INDEXES is a list of non-negative integers.

Returns a list of values from GEN such that each value was an element
of indexes."
  (let ((acc (make-array (length indexes))))
    (for (x idx) (zip! gen (times (1+ (apply #'max indexes))))
      (when (member idx indexes)
        (loop
           :for i :below (length  indexes)
           :for idx2 :in indexes
           :when (= idx2 idx)
           :do (setf (aref acc i) x))))
    (concatenate 'list acc)))

(defun size (gen)
  "Consumes GEN by calculating its size."
  (fold (n 0) (x gen) (1+ n)))

(defun maximum (gen)
  "Consumes GEN, returning its maximum value."
  (fold (m nil) (x gen)
        (if m (max m x) x)))

(defun minimum (gen)
  "Consumes GEN, returning its minimum value."
  (fold (m nil) (x gen)
        (if m (min m x) x)))

(defun average (gen)
  "Consumes GEN, returning its average value."
  (let ((sum 0)
        (count 0))
    (for x gen
      (incf sum x)
      (incf count))
    (/ sum count)))

(defun argmax (fn gen)
  "Consumes GEN. Returns a pair (X . VALUE) such that (FUNCALL FN X)
is maximal among the values of GEN.  VALUE is the value of (FUNCALL FN X)"
  (fold (am nil)
        (arg gen)
        (let ((val (funcall fn arg)))
          (if (or (not am) (> val (cdr am)))
              (cons arg val)
              am))))

(defun argmin (fn gen)
    "Consumes GEN. Returns a pair (X . VALUE) such that (FUNCALL FN X)
is minimal among the values of GEN.  VALUE is the value of (FUNCALL FN X)"
  (fold (am nil)
        (arg gen)
        (let ((val (funcall fn arg)))
          (if (or (not am) (< val (cdr am)))
              (cons arg val)
              am))))