Created
June 21, 2021 02:07
-
-
Save ralsei/dcf94d34cb7335ed60e8171c2b9d6ed3 to your computer and use it in GitHub Desktop.
Ergonomic O(1) arrays for Racket :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require (for-syntax racket/base | |
racket/syntax) | |
syntax/parse/define) | |
(provide define-aaarray aaarray-ref) | |
(define-syntax (define-aaarray stx) | |
(syntax-parse stx | |
[(_ name:id val:expr ...) | |
#:with (to-def ...) (for/list ([(_ idx) (in-indexed (in-list (attribute val)))]) | |
(format-id #'name "~a-~a" (attribute name) idx)) | |
#'(begin | |
(define to-def val) | |
...)])) | |
(define-syntax (aaarray-ref stx) | |
(syntax-parse stx | |
[(_ name:id idx:expr) | |
#:with v (format-id #'name "~a-~a" | |
(syntax->datum (attribute name)) | |
(syntax->datum (attribute idx))) | |
#'v])) | |
(module+ test | |
(require rackunit) | |
(define-aaarray terrible 1 2 3 "hi" "lol" 5894 'what "/proc/self/mem") | |
(check-equal? (aaarray-ref terrible 2) 3) | |
(check-equal? (aaarray-ref terrible 7) "/proc/self/mem") | |
(check-equal? terrible-2 3) | |
(check-equal? terrible-7 "/proc/self/mem")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sorry