Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active October 15, 2016 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Metaxal/e4ac303d442cbf9fb7f3edc21d8a9019 to your computer and use it in GitHub Desktop.
Save Metaxal/e4ac303d442cbf9fb7f3edc21d8a9019 to your computer and use it in GitHub Desktop.
#lang racket/base
(require plot
racket/list
racket/dict)
(module+ test
(require rackunit))
(define (string-ticks string-list)
(let* ([N (length string-list)]
[inf+sup->low+up
(λ(inf sup)
(when (> inf sup)
(error 'bound-check "inf > sup in string-ticks; inf=~e, sup=~e" inf sup))
(values (max 0 (inexact->exact (ceiling inf)))
(min (+ 1 (floor (inexact->exact sup))) N)))])
(ticks (λ(inf sup)
(define-values (low up) (inf+sup->low+up inf sup))
(if (< low up)
(for/list ([i (in-range low up)])
(pre-tick i #t))
'()))
(λ(inf sup pre-ticks)
(for/list ([t pre-ticks])
(define idx (pre-tick-value t))
(if (and (exact-nonnegative-integer? idx)
(< idx N))
(list-ref string-list idx)
""))))))
(module+ test
(let ([s '("a" "b" "c" "d" "e")])
(check-equal? (length (ticks-generate (string-ticks s) .1 .9)) 0)
(check-equal? (length (ticks-generate (string-ticks s) -3 -.1)) 0)
(check-equal? (length (ticks-generate (string-ticks s) 0 2)) 3)
(check-equal? (length (ticks-generate (string-ticks s) .1 2)) 2)
(check-equal? (length (ticks-generate (string-ticks s) 0 10)) 5)
))
(parameterize ([plot-y-ticks (string-ticks '("a" "b" "c" "d" "e"))])
(plot
(function (λ(x)x) -1 1)
#:y-min -2 #:y-max 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment