Skip to content

Instantly share code, notes, and snippets.

View samdphillips's full-sized avatar

Sam Phillips samdphillips

  • Oakland, California
View GitHub Profile
@samdphillips
samdphillips / ivar.rkt
Created August 19, 2022 18:09
One shot synchronizable variables.
#lang racket/base
(require racket/contract
racket/undefined)
(provide ivar?
exn:fail:ivar?
make-ivar
(contract-out
[ivar-put! (-> ivar? any/c any)]
@samdphillips
samdphillips / waiter.rkt
Last active July 27, 2022 23:00
One-shot type condition variable
#lang racket/base
(require racket/async-channel
racket/function)
(struct waiter (thd req-ch notify-ch))
(define (make-waiter)
(define req-ch (make-channel))
(define notify-ch (make-channel))
@samdphillips
samdphillips / static-exception-checking.rkt
Created April 12, 2022 01:07
not even close to being a full handling for checked exceptions
#lang racket/base
(require (for-syntax racket/base
racket/format
syntax/parse/lib/function-header)
(rename-in racket/base [raise base:raise])
racket/stxparam
syntax/parse/define)
(begin-for-syntax
#lang racket/base
(define (apples a-string)
(define string-size (string-length a-string))
(define (once current count i)
(cond
[(= i string-size)
(if current
(list (cons current count))
null)]
@samdphillips
samdphillips / holder.rkt
Created April 1, 2022 20:42
threaded box thingy
#lang racket/base
(require racket/match)
(struct holder (thd req-ch))
(define (make-holder initial-value)
(define req-ch (make-channel))
(define thd
(thread
#lang racket
(require job-queue
racket/async-channel
threading)
(define ssh-bin (find-executable-path "ssh"))
;; adjust for your level of safety
(define ssh-args
@samdphillips
samdphillips / numbers.c
Created March 11, 2022 17:50
Another simple ffi example
#include <stdint.h>
#include <stdlib.h>
void
get_numbers(int64_t *numbers, uint64_t size)
{
for(int i=0; i < size; i++) {
numbers[i] = rand();
}
@samdphillips
samdphillips / defer-test.rkt
Created February 24, 2022 15:43
Go style defer in Racket
#lang racket/base
(require "defer.rkt")
(define (x)
(define f (open-input-file "/dev/random"))
(defer (begin (println "closing f") (close-input-port f)))
(displayln (bytes->list (read-bytes 32 f))))
#lang racket/base
(module stack racket/base
(provide build-it)
(define (build-it n)
(if (zero? n)
null
(cons n (build-it (sub1 n))))))
(module accumulate racket/base
@samdphillips
samdphillips / arrow-array.rkt
Created January 4, 2022 01:52
Barely using Arrow array library from Racket
#lang racket/base
(require ffi/unsafe
gir)
(define arrow-lib (gi-ffi "Arrow"))
(define (make-arrow-buffer buf size)
(arrow-lib 'Buffer 'new buf size))