Skip to content

Instantly share code, notes, and snippets.

View jackfirth's full-sized avatar

Jack Firth jackfirth

View GitHub Profile
@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)
;@------------------------------------------------------------------------------
@jackfirth
jackfirth / struct-to-vector.rkt
Created July 4, 2020 22:29
Macro to define converters between structs and vectors.
#lang racket
(require (for-syntax racket/syntax
syntax/parse/class/struct-id)
syntax/parse/define)
;@------------------------------------------------------------------------------
(struct point (x y))
@jackfirth
jackfirth / twixt-pict.rkt
Created February 14, 2020 10:04
Some Twixt and pict stuff
#lang racket
(require pict
rebellion/collection/immutable-vector
rebellion/collection/hash
rebellion/streaming/reducer
rebellion/streaming/transducer
rebellion/type/enum
rebellion/type/record
rebellion/type/tuple)
@jackfirth
jackfirth / transducer-benchmarks.rkt
Last active December 18, 2019 08:36
Atomichron benchmarks for Rebellion's transducers
#lang racket/base
(require (for-syntax racket/base)
atomichron
racket/pretty
racket/set
rebellion/collection/list
rebellion/streaming/reducer
rebellion/streaming/transducer
syntax/parse/define)