Skip to content

Instantly share code, notes, and snippets.

@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))]
@matthewdowney
matthewdowney / load_class.clj
Created December 12, 2021 02:29
Load a java .class file from disk directly into Clojure ... at your own risk obviously
(ns load-class
(:import (java.lang.reflect Array)
(java.io FileInputStream)))
;; ClassLoader/defineClass is protected, and calling it from the proxy requires
;; reflection.
(def ^:private classloader-define-class
(let [primitive-bytes (.getClass (Array/newInstance Byte/TYPE 0))
args [String primitive-bytes Integer/TYPE Integer/TYPE]]
(doto
@matthewdowney
matthewdowney / web3.clj
Created August 28, 2021 00:18
Call a smart contract from Clojure to query data from the Ethereum blockchain.
(ns web3
"Call an Ethereum smart contract (Uniswap v2) from Clojure.
Requires https://github.com/clj-python/libpython-clj for python interop. Also
make sure to $ pip install web3."
(:require [libpython-clj2.python :as py]
[libpython-clj2.require :refer [require-python]]))
(comment ;; deps.edn