-
Notifications
You must be signed in to change notification settings - Fork 0
/
katana.lisp
63 lines (38 loc) · 1.64 KB
/
katana.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
;;;; katana.lisp
;;;;
;;;; Higher level routines and syntatic sugars that are built on top
;;;; of the swiss-sword library.
;;;;
;;;; Author: BreakDS <breakds@gmail.com>
(in-package #:breakds.basicl.katana)
;;;; ---- Lazy Evaluation
(defun force (lazy-obj)
"force the lazy-object to evaluate and return the value"
(funcall lazy-obj))
;; Define lazy-object dispatch macro #l. It has an optional number
;; argument that if provided with 1 (and only 1), the macro creates a
;; lazy-object that supports memorization. Otherwise, it creates a
;; lazy-object that can be "forced" only once.
(eval-when (compile load eval)
(set-dispatch-macro-character
#\# #\L (lambda (stream sub-char numarg)
(declare (ignorable sub-char))
(if (null numarg)
`(lambda ()
,(read stream))
(if (= numarg 1)
(with-gensyms (result)
`(alet (,result)
(lambda ()
(setq ,result ,(read stream)
this (lambda () ,result))
,result)))
(error "Invalid number argument for #l (should be 1 or nil)"))))))
;;;; ---- Lazy Sequence Shortcuts
(defun car$ (x)
(car (force x)))
(defun cdr$ (x)
(cdr (force x)))
(defun mapcar$ (fun &rest args)
#1L(cons (apply fun (mapcar #'car$ args))
(apply #'mapcar$ fun (mapcar #'cdr$ args))))