Code Snippets Racket

Short texts Racket 🧑‍💻 Code snippets Racket ✍️ Reading lists Racket 👀

Asteroids game implementation in Racket

#lang racket ;; An Asteroids game in Racket ;; Copyright (c) 2021 Alex Harsányi ([email protected]) ;; Permission is hereby granted, free of charge, to any person obtaining a ;; copy of this software and associated documentation files (the “Software”), ;; to deal in the Software without restriction, including without limitation ;; the rights to use, […]

POC for a SQL query working with someone in chat

#lang at-exp racket/base (require db racket/format) (define dbc (sqlite3-connect #:database ‘memory)) (query-exec dbc @~a{ CREATE TABLE orders (id INTEGER PRIMARY KEY, email STRING, date TEXT); }) (query-exec dbc @~a{ CREATE INDEX email_i ON orders (email); }) (query-exec dbc @~a{ CREATE INDEX date_i ON orders (date DESC); }) (define (add-order! id email date) (define q “INSERT […]

google authentication base on time

# google authentication base on time in Racket Programming Language ## How to use following these steps below: 1. install package `racket-base32`: “`shell raco pkg install https://github.com/sackpost/racket-base32.git “` 2. test it: “` raco test google-auth.rkt “` #lang racket/base (require base32 file/sha1 web-server/stuffers/hmac-sha1 racket/contract) (define/contract (get-code key) (-> bytes? integer?) (let* ([key (base32-decode key)] [code (hex-string->bytes […]

Experiment expanding the basic nature of Racket structs to allow reflection and dynamic reference to fields. Also, use the struct as a procedure for get and set.

#lang racket (require racket/generic (for-syntax racket/syntax racket/function syntax/parse syntax/parse/define)) (provide struct* fields name get set) (module+ test (require rackunit)) (define-generics reflective-struct (fields reflective-struct) (name reflective-struct) (get reflective-struct . field-symbol) (set reflective-struct field-symbol value)) (define-syntax (struct* stx) (syntax-parse stx [(_ struct-name:id [struct-prop:id …] extra-properties …) (define (getter-name prop-stx) (format-id prop-stx “~a-~a” #’struct-name prop-stx)) (with-syntax ([orig stx] […]

DrRacket Comment Keybinding

#lang s-exp framework/keybinding-lang (define (comment-or-uncomment editor event) (define selected-line-start-pos (get-line-start-ponsition editor)) (define selected-line-end-pos (get-line-end-ponsition editor)) (if (= selected-line-start-pos selected-line-end-pos) ;; if on blank line and without selection, just insert “;” (send editor insert “;”) (comment-or-uncomment-at editor selected-line-start-pos))) ;; returns the first selected line’s starting position, ;; if no line is selected, returns the current line’s […]