-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.lisp
82 lines (68 loc) · 1.83 KB
/
examples.lisp
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
(defpackage #:examples
(:use #:cl #:cl-gen))
(in-package #:examples)
(defuncc print-cc (&rest args)
(cc (print args)))
(defuncc random-2 (max)
(cc (random max) (random max)))
(defuncc hello-cc (max)
(cc-bind (x y) (random-2 max)
(print-cc x y)))
(cc-context (hello-cc 10))
(defuncc execute-later ()
(print cl-gen::$cc) ; Don't do that
(cc-bind (x) cl-gen::$cc
(print cl-gen::$cc)
(cc-bind (y z) (random-2 x)
(print cl-gen::$cc)
(print-cc x y z))))
(cc-context
(let ((f (execute-later)))
(funcall f 30)))
(defuncc lazy-loop ()
(labels ((rec (x)
(format t "Iterated ~A times :O~%" x)
(cc-bind (&rest args) cl-gen::$cc
(format t "Received ~A~%" args)
(rec (1+ x)))))
(rec 0)))
(cc-context
(do ((x 0 (1+ x))
(f (lazy-loop) (funcall f (random 15))))
((= 100 x))))
;; Generator basic usage
(defgen generator ()
(yield-bind () "Lorem"
(yield-bind () "Ipsum"
(yield-bind () "Dolor"))))
(cc-context
(let ((gen (generator)))
(next-bind (x) (gen)
(print x)
(next-bind (y) (gen)
(print y)
(next-bind (z) (gen)
(print z))))))
(cc-context
(let ((gen (generator)))
(next-bind (x) (gen)
(print x)
(next-bind (y) (gen)
(print y)
(next-bind (z) (gen)
(print z)
(print (concatenate 'string x y z)))
;; May be called again on a previous point
(next-bind (y) (gen)
(print y)
(next-bind (z) (gen)
;; Returns last form
t))))))
(defgen your-name ()
(yield-bind (first-name) ()
(yield-bind (second-name) ()
(format t "Hello, ~A ~A!~%" first-name second-name))))
(cc-context
(start-let ((gen (your-name)))
(next-bind () (gen (read-line))
(next-bind () (gen (read-line))))))