2017-08-21 14:29:45 +05:30
|
|
|
(library
|
|
|
|
(utils)
|
2017-10-04 14:49:46 +05:30
|
|
|
(export inc! dec!
|
|
|
|
|
2017-08-25 14:16:56 +05:30
|
|
|
swap!
|
|
|
|
slurp-file
|
2017-08-29 13:48:39 +05:30
|
|
|
chomp
|
|
|
|
hotpatch-sym
|
|
|
|
indirect-lambda
|
2017-09-04 17:01:39 +05:30
|
|
|
set-lambda!
|
2017-10-04 14:49:46 +05:30
|
|
|
dlambda
|
|
|
|
|
|
|
|
all? some? none?)
|
2017-08-29 13:48:39 +05:30
|
|
|
|
2017-08-25 14:16:56 +05:30
|
|
|
(import (chezscheme)
|
|
|
|
(only (srfi s1 lists) drop-while))
|
2017-08-21 14:29:45 +05:30
|
|
|
|
|
|
|
(define-syntax inc!
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ v) (set! v (+ 1 v)))
|
|
|
|
((_ v n) (set! v (+ n v)))))
|
|
|
|
|
|
|
|
(define-syntax dec!
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ v) (set! v (- v 1)))
|
|
|
|
((_ v n) (set! v (- v n)))))
|
2017-08-23 15:19:36 +05:30
|
|
|
|
|
|
|
(define-syntax swap!
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ x y)
|
|
|
|
(let ((tmp x))
|
|
|
|
(set! x y)
|
|
|
|
(set! y tmp)))))
|
2017-08-25 14:16:56 +05:30
|
|
|
|
|
|
|
(define (slurp-file path)
|
|
|
|
(define (slurp)
|
|
|
|
(let ((output (get-string-all (current-input-port))))
|
|
|
|
(if (eof-object? output)
|
|
|
|
output
|
|
|
|
(chomp output))))
|
|
|
|
|
|
|
|
(with-input-from-file path slurp))
|
|
|
|
|
|
|
|
(define (chomp line)
|
|
|
|
(list->string
|
|
|
|
(reverse
|
|
|
|
(drop-while char-whitespace?
|
|
|
|
(reverse (string->list line))))))
|
|
|
|
|
2017-08-29 13:48:39 +05:30
|
|
|
(define hotpatch-sym (gensym))
|
|
|
|
|
|
|
|
(define-syntax indirect-lambda
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ params b1 b2 ...)
|
|
|
|
(let ((this (lambda params b1 b2 ...)))
|
|
|
|
(lambda args
|
|
|
|
(if (and (= (length args) 2)
|
|
|
|
(eq? (car args) hotpatch-sym))
|
|
|
|
(set! this (cadr args))
|
|
|
|
(apply this args)))))))
|
|
|
|
|
|
|
|
(define (set-lambda! fn new-fn)
|
|
|
|
(fn hotpatch-sym new-fn))
|
|
|
|
|
2017-09-04 17:01:39 +05:30
|
|
|
(define-syntax dlambda
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ (name params b1 b2 ...) ...)
|
|
|
|
(lambda (m . args)
|
|
|
|
(apply (case m
|
|
|
|
[(name) (lambda params b1 b2 ...)] ...)
|
|
|
|
args)))))
|
2017-10-04 14:49:46 +05:30
|
|
|
|
|
|
|
;; FIXME: why aren't these in core scheme? what do people use instead?
|
|
|
|
(define (all? pred xs)
|
|
|
|
(let loop ((xs xs))
|
|
|
|
(if (null? xs)
|
|
|
|
#t
|
|
|
|
(if (pred (car xs))
|
|
|
|
(loop (cdr xs))
|
|
|
|
#f))))
|
|
|
|
|
|
|
|
(define (some? pred xs)
|
|
|
|
(let loop ((xs xs))
|
|
|
|
(if (null? xs)
|
|
|
|
#f
|
|
|
|
(if (pred (car xs))
|
|
|
|
#t
|
|
|
|
(loop (cdr xs))))))
|
|
|
|
|
|
|
|
(define (none? pred xs)
|
|
|
|
(not (all? pred xs)))
|
2017-08-21 14:29:45 +05:30
|
|
|
)
|