You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
typesetting/csp/helper.rkt

75 lines
2.3 KiB
Racket

10 years ago
#lang racket/base
(require racket/class racket/list (for-syntax racket/base racket/syntax))
10 years ago
(provide (all-defined-out))
10 years ago
(require rackunit)
10 years ago
10 years ago
(define-syntax-rule (for-each-send proc objects)
(for-each (λ(o) (send o proc)) objects))
(define-syntax-rule (make-proc<%> proc-name)
(interface* ()
([prop:procedure
(λ(this . args)
(send/apply this proc-name args))])
proc-name))
10 years ago
(define-simple-check (check-hash-items h1 h2)
(for/and ([(k1 v1) (in-hash h1)])
(equal? (hash-ref h2 k1) v1)))
10 years ago
(define (list-comparator xs ys)
;; For use in sort. Compares two lists element by element.
(cond
[(equal? xs ys) #f] ; elements are same, so no sort preference
[(and (null? xs) (not (null? ys))) #t] ; ys is longer, so #t
[(and (not (null? xs)) (null? ys)) #f] ; xs is longer, so #f makes it sort later
[else (let ([x (car xs)][y (car ys)])
(cond
[(equal? x y) (list-comparator (cdr xs) (cdr ys))]
[(and (real? x) (real? y)) (< x y)]
10 years ago
[(and (symbol? x) (symbol? y)) (apply string<? (map symbol->string (list x y)))]
10 years ago
[(and (string? x) (string? y)) (string<? x y)]
[else (error 'list-comparator (format "Cant compare ~v and ~v" x y))]))]))
(module+ test
(check-false (list-comparator null null))
(check-false (list-comparator (range 2) (range 2)))
(check-true (list-comparator (range 2) (range 4)))
(check-false (list-comparator (range 4) (range 2)))
10 years ago
(check-true (list-comparator '(1 1 "a") '(1 1 "b")))
(check-true (list-comparator '(1 1 a) '(1 1 b))))
10 years ago
10 years ago
(define-syntax-rule (car-pop! xs)
(let ([i (car xs)])
(set! xs (cdr xs))
i))
10 years ago
(define-syntax-rule (py-pop! xs)
(let ([i (last xs)])
(set! xs (drop-right xs 1))
i))
(module+ test
(let ([xs '(1 2 3)])
(check-equal? (py-pop! xs) 3)
(check-equal? xs '(1 2))))
(define-syntax-rule (py-append! xs x)
(set! xs `(,@xs ,x)))
(define-syntax-rule (py-extend! xs x)
(set! xs `(,@xs ,@x)))
(module+ test
(let ([xs '(1 2 3)])
(py-append! xs (range 2))
(check-equal? xs '(1 2 3 (0 1))))
(let ([xs '(1 2 3)])
(py-extend! xs (range 2))
10 years ago
(check-equal? xs '(1 2 3 0 1))))
(define (word-value . xs)
(let ([xs (reverse xs)])
(for/sum ([i (in-range (length xs))])
(* (list-ref xs i) (expt 10 i)))))