Skip to content

Instantly share code, notes, and snippets.

@gwatt
gwatt / chez-library-extras.sls
Created January 14, 2020 23:09
Some functions to work with libraries
#!chezscheme
(library (chez-library-extras)
(export reimport load-internals)
(import (chezscheme))
(define (loaded? libspec)
(let ([name (extract-library-name libspec)])
(exists (lambda (builtin)
(equal? name builtin))
@gwatt
gwatt / string-interpolation.scm
Created May 3, 2019 00:59
Dollar-sign style string interpolation in R6RS
;;;
;;; Usage:
;;;
;;; ($"some normal text: $with $identifiers substituted in by the $dollar-sign")
;;; ($"You can also $(embed full expressions)")
;;; ($"Because scheme has $very-inclusive-identifier.rules, $(this) is the same as $this")
;;; ($"However, if you have $(multiple items) inside the parentheticals, $multiple is called with $item as the argument")
;;; ($"A zero argument $((function)) will be called like that")
;;; ($"Yes, this means $(funcion call) and $((function call)) are the same")
@gwatt
gwatt / returning!.ss
Created February 23, 2019 16:25
Library re-exporting standard mutation procedures to always return the mutated object
(library (returning!)
(export set! set-car! set-cdr! vector-set! string-set!
hashtable-set! hashtable-update! hashtable-delete! hashtable-clear!
bytevector-copy! bytevector-fill!
bytevector-s8-set! bytevector-u8-set!
bytevector-s16-native-set! bytevector-s16-set! bytevector-u16-native-set! bytevector-u16-set!
bytevector-s32-native-set! bytevector-s32-set! bytevector-u32-native-set! bytevector-u32-set!
bytevector-s64-native-set! bytevector-s64-set! bytevector-u64-native-set! bytevector-u64-set!)
(import (prefix (rnrs) r6rs:)
(prefix (rnrs mutable-pairs) r6rs:)
@gwatt
gwatt / let+.ss
Last active February 23, 2019 16:22
Extended let
;;;
;;; This acts like a letrec*, with the ability to bind multiple values and destructure lists and vectors
;;;
;;; usage:
;;;
;;; (let+ ((a 1)
;;; (b (+ a 1))
;;; ((values c d) (values (+ a b) (* a b)))
;;; ((vector e f g h) (vector a b c d))
(library (documentation)
(export define/document define-syntax/document
document describe)
(import (rnrs))
(define doc-list '())
(define (document thing description)
(set! doc-list (cons (cons thing (syntax->datum description)) doc-list)))
@gwatt
gwatt / future.ss
Last active March 21, 2018 21:05
awaitable futures for ChezScheme
#!chezscheme
(library (future)
(export spawn sync let-futures)
(import (chezscheme))
(define-record-type future
(fields (immutable lock)
(mutable completion)
(mutable result))
@gwatt
gwatt / fn-examples.ss
Last active March 1, 2018 21:57
allows defining functions with constraints on arguments as well as default values
(define iota
(fn ((count :: integer? exact? nonnegative?)
(start :: number? = 0)
(step :: number? = 1))
(if (zero? count)
'()
(cons start
(iota (- count 1) (+ start step) step)))))
(define member
@gwatt
gwatt / param-args.ss
Last active February 10, 2018 17:51
Store command line arguments in parameters
(define-syntax param-args
(syntax-rules ()
[(_ arg-list (opt param) ...)
(let loop ([args arg-list])
(if (null? args)
'()
(case (car args)
[(opt) (if (null? (cdr args))
(errorf 'param-args "Missing required argument for ~a" opt))
(param (cadr args))
(library (html)
(export html display-html)
(import (rnrs))
(define short-tags (make-enumeration '(br img link meta)))
(define (short-tag? t)
(enum-set-member? t short-tags))
(define (->string obj)
@gwatt
gwatt / curry.scm
Last active February 14, 2017 00:56
define-curried/lambda-curried for r6rs scheme
(library (curry)
(export define-curried lambda-curried)
(import (rnrs))
(define-syntax lambda-curried
(lambda (x)
(syntax-case x ()
[(_ () b b* ...) #'(lambda () b b* ...)]
[(_ arg* b b* ...) (identifier? #'arg*) #'(lambda arg* b b* ...)]
[(_ (arg* ...) b b* ...)