Skip to content

Instantly share code, notes, and snippets.

@jackfirth
jackfirth / syntax-format.rkt
Created August 15, 2024 09:57
`fmt`-style pretty printing, but of syntax objects
#lang racket
;; This module defines a Racket syntax formatter that uses pretty-expressive as
;; the underlying formatting engine. The formatter takes *syntax objects* as
;; input and produces a pretty-expressie document as output. Formatting occurs
;; in steps, similar to macro expansion: each step analyzes a syntax object and
;; returns a *partially formatted* syntax object, which consists of strings
;; interleaved with unformatted syntax objects representing the subforms of the
;; input object. Formatting begins with the outermost syntax object and
@jackfirth
jackfirth / dispatch-benchmark.rkt
Last active November 8, 2023 05:32
Measuring the performance of various ways of dispatching on the type of something in Racket
#lang racket
(require racket/generic)
(define-generics vable
(get-v vable))
(struct a (v)
#:methods gen:vable
[(define (get-v a)
@jackfirth
jackfirth / transducer.effekt
Last active August 26, 2022 21:47
An Effekt implementation of transducers and stream pipelines.
// This is my attempt at translating the stream pipeline and transducer library I wrote for Racket
// into Effekt, using algebraic effects to model the actions of consuming and emitting values in a
// stream pipeline. The stream pipeline library I wrote is part of my package Rebellion, see its docs
// here: https://docs.racket-lang.org/rebellion/Streaming_Computations.html. The basics are that a
// stream pipeline combines an initial source (a lazy stream, sequence, or other similar object) and
// a terminal sink, called a *reducer*, along with any number of intermediate transformations called
// *transducers*.
type Option[A] { Present(value: A); Absent() }
class Map<K, V> {
   ...

    V getOrSignal(K key) {
        if containsKey(key) {
            return get(key);
        } else {
            MissingKeySignal signal = throw new MissingKeyException(this, key);
            return switch signal {
@jackfirth
jackfirth / collection-api-sketch.md
Last active May 26, 2022 09:49
Rhombus collection API ideas

Mandatory APIs:

interface List {
  size
  add(x)
  get(i)
  set(i, x)
  removeAt(i)
  insert(i, x)
@jackfirth
jackfirth / mutable-red-black-tree-pict.rkt
Created February 16, 2022 02:57
A debugging tool that draws a pict of a mutable red black tree. Useful for debugging Rebellion's RB trees in mutable sorted collection implementations.
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[mutable-rb-tree-pict (-> mutable-rb-tree? pict?)]))
@jackfirth
jackfirth / static-continuations.rkt
Last active December 10, 2021 08:22
A "static continuation" system for Racket macros.
#lang racket/base
;; This is a "static continuation" system, which I developed as a foundation for building macros like
;; the following:
;;
;; - The guard, guard-match, and guard-define statement macros in rebellion/private/guarded-block
;; - A (parameterize! id expr) statement macro that sets a parameter for the rest of the block
;; - An (open! disposable-expr) expression macro that allocates a resource and closes it at the end of
;; the block
@jackfirth
jackfirth / generic-list.rkt
Created July 21, 2021 03:20
Generic interface for Racket lists
#lang racket/base
(require racket/generic
(prefix-in racket/list. racket/list)
racket/match
racket/stream
racket/struct
racket/unsafe/ops
(prefix-in racket/vector. racket/vector)
#lang racket/base
(provide sdfn-register-id scalar-sdfn-register-id vec3-sdfn-register-id (struct-out sdfn-register))
(require syntax/parse)
(struct sdfn-register (size offset initial-values) #:transparent)
@jackfirth
jackfirth / how-to-design-functions.md
Last active July 16, 2021 16:01
The Function Design Recipe from How to Design Programs (second edition)

The Function Design Recipe

The function design recipe is a series of steps to follow when creating a function. The primary purpose of these steps is to help you think through the problem you're trying to solve. A secondary purpose is to make you write down explanations of what you're trying to do so that other programmers, especially programmers you ask for help, can understand your code.

In this article, we will be applying the function design recipe to the following example problem:

The state of Tax Land has created a three-stage sales tax to cope with its budget deficit. Inexpensive items, those costing less than $1,000, are not taxed. Luxury items, with a price of more than $10,000, are taxed at the rate of eight percent (8.00%). Everything in between comes with a five percent (5.00%) markup.

Design a function for a cash register that, given the price of an item, computes the sales tax.