d-/d-.scm

98 lines
2.1 KiB
Scheme
Raw Normal View History

2024-10-23 09:02:27 -05:00
(define-module (d-)
2024-10-25 15:16:04 -05:00
#:use-module (srfi srfi-1)
2024-11-21 23:13:27 -06:00
#:use-module (language tree-il)
2024-10-23 09:02:27 -05:00
#:export
(~>
~>>
2024-11-21 23:13:27 -06:00
as~>
2024-10-25 15:48:01 -05:00
if-not
when-not
2024-11-01 18:05:27 -05:00
if-let
for
2024-10-25 15:16:04 -05:00
partial
2024-10-29 11:41:50 -05:00
argmin
2024-11-21 23:13:27 -06:00
iterate
generator
macro-expand))
2024-10-23 09:02:27 -05:00
(define-syntax ~>
(syntax-rules ()
2024-11-01 18:05:27 -05:00
[(~> v) v]
[(~> v (fn args ...) more ...) (~> (fn v args ...) more ...)]
[(~> v fn more ...) (~> (fn v) more ...)]))
2024-10-23 09:02:27 -05:00
(define-syntax ~>>
(syntax-rules ()
2024-11-01 18:05:27 -05:00
[(~>> v) v]
[(~>> v (fn args ...) more ...) (~>> (fn args ... v) more ...)]
[(~>> v fn more ...) (~>> (fn v) more ...)]))
2024-10-23 09:02:27 -05:00
2024-11-21 23:13:27 -06:00
(define-syntax as~>
(syntax-rules ()
[(_ as v) v]
[(_ as v (fn args ...) more ...)
(let [(as v)]
(as~> as (fn args ...) more ...))]))
2024-10-25 15:48:01 -05:00
(define-syntax-rule (if-not pred body ...)
(if (not pred) body ...))
(define-syntax-rule (when-not pred body ...)
(when (not pred) body ...))
2024-11-01 18:05:27 -05:00
(define-syntax-rule (if-let ([ident test]) expr ...)
(let ([ident test])
(if ident
(begin expr ...)
#f)))
(define-syntax for
(syntax-rules ()
[(for () expr ...) (list (begin expr ...))]
[(for ([ident lst] bindings ...) expr ...)
(let iter ([rest lst])
(if (pair? rest)
(let ([ident (car rest)])
(append (for (bindings ...) expr ...) (iter (cdr rest))))
'()))]))
2024-10-23 09:02:27 -05:00
(define (partial fn . args)
(lambda x (apply fn (append args x))))
2024-10-25 15:16:04 -05:00
2024-11-11 15:38:13 -06:00
(define (argmin arg lt? . vals)
2024-10-25 15:16:04 -05:00
(reduce (lambda (val min)
2024-11-11 15:38:13 -06:00
(if (lt? (arg val) (arg min))
2024-10-25 15:16:04 -05:00
val
min))
#f
vals))
2024-10-29 11:41:50 -05:00
2024-11-21 23:13:27 -06:00
(define (iterate n f v)
(if (zero? n)
v
(iterate (1- n) f (f v))))
2024-10-29 11:41:50 -05:00
;; Shamelessly ripped from https://wingolog.org/archives/2013/02/25/on-generators
(define (make-generator f)
(define tag (make-prompt-tag))
(define (thunk)
(f (lambda (val) (abort-to-prompt tag val))))
(lambda ()
(call-with-prompt tag
thunk
(lambda (k value)
(set! thunk k)
value))))
2024-11-11 22:15:27 -06:00
(define-syntax generator
(lambda (x)
(syntax-case x ()
[(generator expr ...)
(with-syntax ([yield (datum->syntax x 'yield)])
#'(make-generator
(lambda (yield)
expr ...)))])))
2024-11-21 23:13:27 -06:00
;; Why wasn't this included?
(define macro-expand (compose tree-il->scheme macroexpand))