Skip to content

Instantly share code, notes, and snippets.

@ralsei
Created June 21, 2021 02:07
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 ralsei/dcf94d34cb7335ed60e8171c2b9d6ed3 to your computer and use it in GitHub Desktop.
Save ralsei/dcf94d34cb7335ed60e8171c2b9d6ed3 to your computer and use it in GitHub Desktop.
Ergonomic O(1) arrays for Racket :)
#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"))
@ralsei
Copy link
Author

ralsei commented Jun 21, 2021

sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment