Skip to content

Instantly share code, notes, and snippets.

@matthewdowney
matthewdowney / doubly_linked_list.rs
Created April 5, 2025 21:33
Doubly linked list in Rust
/// Doubly-linked list that, instead of pointers, uses handles, which are indices
/// into an object pool.
struct LinkedList<T> {
head: Option<LinkedListNodeHandle>,
tail: Option<LinkedListNodeHandle>,
pool: ObjectPool<LinkedListNode<T>>,
}
struct LinkedListNode<T> {
data: T,
@matthewdowney
matthewdowney / mmap_stdin.rs
Created March 20, 2025 18:58
mmap stdin in Rust, supports Mac OS (ARM) and Linux
// Conditional extern definitions for Linux/Android
#[cfg(any(target_os = "linux", target_os = "android"))]
#[link(name = "c")]
unsafe extern "C" {
fn mmap(addr: *mut u8, len: usize, prot: i32, flags: i32, fd: i32, offset: i64) -> *mut u8;
fn __errno_location() -> *const i32;
fn lseek(fd: i32, offset: i64, whence: i32) -> i64;
fn open(path: *const u8, oflag: i32) -> i32;
}
@matthewdowney
matthewdowney / micrograd.clj
Created May 3, 2023 18:17
karpathy's micrograd values in Clojure
(ns micrograd
"autograd engine for scalar values and operations in the style of
https://github.com/karpathy/micrograd
but with immutable values.")
; Wrap a scalar `data` with some additional context
(defrecord Value [data grad children op op-deriv])
@matthewdowney
matthewdowney / deploy.clj
Created March 11, 2023 01:19
Using the AWS CDK from Clojure
(ns deploy
(:require [clojure.java.io :as io])
(:import (software.amazon.awscdk App CfnOutput$Builder Stack)
(software.amazon.awscdk.services.apigatewayv2.alpha AddRoutesOptions HttpApi$Builder HttpMethod)
(software.amazon.awscdk.services.apigatewayv2.authorizers.alpha HttpIamAuthorizer)
(software.amazon.awscdk.services.apigatewayv2.integrations.alpha HttpAlbIntegration)
(software.amazon.awscdk.services.ec2 Vpc$Builder)
(software.amazon.awscdk.services.ecs Cluster$Builder ContainerImage)
(software.amazon.awscdk.services.ecs.patterns ApplicationLoadBalancedFargateService$Builder ApplicationLoadBalancedTaskImageOptions)
(software.amazon.awscdk.services.iam AnyPrincipal)
@matthewdowney
matthewdowney / transducer-state.clj
Created February 20, 2023 17:19
Transducers, but with state stored inside the "result" instead of in atoms / volatiles.
;;; Transducers, but with state stored inside the "result" instead of in atoms
;;; / volatiles.
(defn map1 [f]
(fn [[rf idx]]
[(fn
([] (rf))
([result] (rf result))
([result input] (rf result (f input))))
(inc idx)]))
@matthewdowney
matthewdowney / hack-captive-portal.bb
Created January 5, 2023 18:39
Script to bypass captive portals from https://miloserdov.org/?p=1088, ported to Babashka and improved
#!/usr/bin/env bb
; =========================================================================== ;
; FILE: hack-captive-portal.bb ;
; USAGE: sudo bb hack-captive-portal.bb ;
; ;
; DESCRIPTION: This script helps to pass through the captive portals in ;
; public Wi-Fi networks. It hijacks IP and MAC from somebody ;
; who is already connected and authorized on captive portal. ;
; Tested in Ubuntu 16.04 with different captive portals in ;
; airports and hotels all over the world. ;
@matthewdowney
matthewdowney / react-financial-charts-candles-clojurescript.clj
Last active September 9, 2022 14:42
Clojurescript + Reagent port of react-financial-charts sample candles code (StockChart.tsx). See https://react-financial.github.io/react-financial-charts/?path=/story/features-full-screen--daily.
(ns stock-chart-example
"Clojurescript + Reagent port of react-financial-charts StockChart.tsx
candles example[1][2].
Assumes a project generated via https://github.com/bhauman/figwheel-main-template
with Reagent included, and https://github.com/react-financial/react-financial-charts
required via https://figwheel.org/docs/npm.html.
See also
- The BasicCandlestick.tsx[3] example
@matthewdowney
matthewdowney / aws-api-gateway-to-fargate.clj
Created August 10, 2022 22:32
Clojure: deploy an AWS API Gateway endpoint with a VPC private link to a Fargate task using the CDK.
(ns deploy
(:import (software.amazon.awscdk App CfnOutput$Builder Stack)
(software.amazon.awscdk.services.apigatewayv2.alpha AddRoutesOptions HttpApi$Builder HttpMethod)
(software.amazon.awscdk.services.apigatewayv2.authorizers.alpha HttpIamAuthorizer)
(software.amazon.awscdk.services.apigatewayv2.integrations.alpha HttpAlbIntegration)
(software.amazon.awscdk.services.ec2 Vpc$Builder)
(software.amazon.awscdk.services.ecs Cluster$Builder ContainerImage)
(software.amazon.awscdk.services.ecs.patterns ApplicationLoadBalancedFargateService$Builder ApplicationLoadBalancedTaskImageOptions)
(software.amazon.awscdk.services.iam AnyPrincipal)))
@matthewdowney
matthewdowney / env.clj
Created August 1, 2022 00:14
Clojure hack to overwrite the JVM's System/getenv map for environment variables without touching the actual environment. Definitely not safe.
(ns env
"Clojure hack to overwrite the JVM's System/getenv map for environment variables without touching the actual environment. Definitely not safe."
(:import (java.util Collections Map)))
(defn -unsafe-set-env [new-env]
(try
(let [clazz (Class/forName "java.lang.ProcessEnvironment")]
(doseq [field ["theEnvironment" "theCaseInsensitiveEnvironment"]]
(let [env-field (doto (.getDeclaredField clazz field) (.setAccessible true))]
(.putAll ^Map (.get env-field nil) new-env))))
@matthewdowney
matthewdowney / poisson-sample.clj
Created June 8, 2022 13:33
Sample from Poisson distribution in Clojure
(defn poisson
"Return a function that samples from a Poisson distribution using Knuth's
algorithm[^1], given `lambda`, the expected number of occurrences per
interval.
[^1]: https://en.wikipedia.org/wiki/Poisson_distribution#Random_drawing_from_the_Poisson_distribution"
([lambda]
(poisson lambda (Random.)))
([lambda ^Random r]
(let [l (Math/exp (- lambda))]