Skip to content

Instantly share code, notes, and snippets.

#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.

@jackfirth
jackfirth / typed.rkt
Created April 28, 2021 03:40
twitter-user-@ladyaeva.md
#lang typed/racket
(define-type Vec2 (Vector Flonum Flonum))
(: mad (Flonum Flonum Flonum . -> . Flonum))
(define (mad a b c)
(+ (* a b) c))
(: vec2-mad (Vec2 Vec2 Vec2 . -> . Vec2))
(define (vec2-mad a b c)
@jackfirth
jackfirth / pretty-printing.rkt
Created April 15, 2021 00:18
Pretty printing sequences with indentation and without any delimiters.
#lang racket/base
(require racket/contract/base)
(provide
(contract-out
[sequence-markup? predicate/c]
[sequence-markup
@jackfirth
jackfirth / the-worst-string-program.rkt
Last active December 15, 2020 04:53
The worst racket string program
#lang racket/base
(require racket/unsafe/ops)
(define s1 (string-append))
(define s2 (string-append))
(immutable? s1) ; false
(immutable? s2) ; false
(eq? s1 s2) ; true (!!!)
@jackfirth
jackfirth / promisegraph.hs
Created October 7, 2020 10:52
A hypothetical Haskell API for PromiseGraph.
-- an asynchronous computation in the monad m that produces an a or dies with SomeException
Promise m a
immediatePromise : a -> Promise m a
immediatePromiseM : m a -> Promise m a
immediateFailedPromise : Exception e => e -> Promise m Void
immediateFailedPromiseM : Exception e => m e -> Promise m Void
unitPromise : Promise m Unit
then : Promise m a -> (a -> b) -> Promise m b
thenM : Promise m a -> (a -> m b) -> Promise m b
#lang racket/base
(require racket/list
racket/match
rebellion/base/option
rebellion/collection/list
rebellion/streaming/reducer
rebellion/streaming/transducer
rebellion/type/enum
fancy-app)
@jackfirth
jackfirth / matter-reference-example.rkt
Created July 26, 2020 12:13
An example of how I would write some code that came up in the racket slack.
#lang racket/base
; See the Racket Style Guide for details on why this file is organized the way
; it is: https://docs.racket-lang.org/style/Units_of_Code.html
; We have to import racket/contract/base first in order to use contract-out.
(require racket/contract/base)
@jackfirth
jackfirth / rewrite-rule-idea.rkt
Created July 5, 2020 22:13
Hypothetical sketch of a system for adding rewrite rules to APIs.
;; This is a hypothetical syntax for defining rewrite rules that can be used to create custom optimizations and
;; refactoring suggestions. This example assumes there's some sort of geometry library that accepts angles in terms of
;; either degrees or radians using wrapper structs like `(struct degrees (value))` and `(struct radians (value))`. If
;; the wrapping is done right where the function is called, it allocates garbage that specialized degree-specific and
;; radian-specific functions wouldn't allocate. But we don't want to double the API surface, so we instead create inlining
;; rules that fire when the wrapping is done at the call site.
(define-rewrite-rule rotate-degrees-specialization
@explanation{Using the @racket[rotate-degrees] function avoids allocating the intermediate @racket[degrees] value.}
#:surface-as hidden-optimization
@jackfirth
jackfirth / more-struct-converters.rkt
Created July 4, 2020 22:50
Converting structs to vectors and json (in the form of jsexprs).
#lang racket
(require (for-syntax racket/sequence
racket/symbol
racket/syntax
rebellion/collection/vector
syntax/parse/class/struct-id)
syntax/parse/define)
;@------------------------------------------------------------------------------