Skip to content

Instantly share code, notes, and snippets.

View clarkmcc's full-sized avatar

Clark McCauley clarkmcc

View GitHub Profile
@clarkmcc
clarkmcc / scale-tensorflow-rust.go
Created October 16, 2023 13:35
Decoding and running a Scale function
sf := new(scalefunc.Schema)
err := sf.Decode(model)
if err != nil {
panic(err)
}
s, err := scale.New(scale.NewConfig(signature.New).WithFunction(sf))
if err != nil {
panic(err)
}
instance, err := s.Instance()
@clarkmcc
clarkmcc / model.rs
Created May 20, 2023 03:58
In trying to wrap my head around the basics of machine learning, this example really helped me to get a small taste for how machine learning frameworks like TensorFlow work their magic under the hood.
use rand::random;
/// In this example, we'll attempt to train a single-neuron model to fit
/// a linear line. The slope of this line is 2, and can by expressed by
/// the equation y = 2x. We'll use gradient descent to find the slope of
/// the line, and then we'll use the slope to predict the output for
/// a given input.
///
/// The model won't be able to perfectly fit the line, but it will be
/// able to get very close. The idea here is we want the model to be
@clarkmcc
clarkmcc / use-query-params.ts
Created May 3, 2022 14:45
A React hook to store and retrieve JSON values to the query parameters using React Router v6
import { useSearchParams } from "react-router-dom";
import { useCallback, useEffect } from "react";
import { isFunction } from "lodash";
/**
* Setter represents a type that can be passed to the setter function returned
* from this hook. It can either accept a value to set, or a callback function
* with access to the current value.
*
* @example

Keybase proof

I hereby claim:

  • I am clarkmcc on github.
  • I am shardblade (https://keybase.io/shardblade) on keybase.
  • I have a public key whose fingerprint is 2E6B 9202 AFE9 1B1B 1D9A 024C AA8A FADB 0CC6 2973

To claim this, I am signing this object:

@clarkmcc
clarkmcc / diffie-hellman.js
Created January 28, 2022 15:55
A naive and basic implementation of the Diffie-Hellman key exchange to more easily understand how the math works
// Diffie-Hellman asymetric key exchange allows two
// parties to cooperatively create a shared secret
// key without ever exchanging the shared secret.
// This means that it will be nearly impossible for
// a malicious party observing the creation of the
// shared secret to determine the secret key.
// Randomly create a generator number and a number p
// These two numbers are shared between both parties
// in the public space which means they're potentially
@clarkmcc
clarkmcc / cloudbuild.yaml
Created January 2, 2022 21:32
Builds, pushes, and deploys a container image to Google Cloud Run, just set the $PROJECT_NAME variable
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/$PROJECT_NAME:$SHORT_SHA', '--cache-from', 'gcr.io/$PROJECT_ID/$PROJECT_NAME', '.']
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/$PROJECT_NAME:$SHORT_SHA']
# Deploy to Google Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
@clarkmcc
clarkmcc / delete-shutdown-pods.sh
Last active May 11, 2022 12:15
Deletes Kubernetes pods with a status of Shutdown that are usually left over after a GKE pre-emptible node goes offline.
#!/bin/sh
for namespace in $(kubectl get ns | awk '{print $1}' | grep -v NAME); do
for pod in $(kubectl get pod -n $namespace --field-selector=status.phase!=Running | grep Shutdown | awk '{print $1}' | grep -v NAME); do
kubectl delete pod -n "$namespace" "$pod"
done
done
@clarkmcc
clarkmcc / main.go
Created March 16, 2021 18:43
Get all filenames inside an Golang embedded filesystem.
func getAllFilenames(fs *embed.FS, path string) (out []string, err error) {
if len(path) == 0 {
path = "."
}
entries, err := fs.ReadDir(path)
if err != nil {
return nil, err
}
for _, entry := range entries {
fp := filepath.Join(path, entry.Name())
@clarkmcc
clarkmcc / batcher.go
Last active September 16, 2020 23:44
A zero dependency, callback based batcher
package reflector
import (
"sync"
)
// Batcher knows how to batch a slice of objects, calling do for each batch
// until Next returns false.
//
// batcher.Next(func(objs []interface{}) {
@clarkmcc
clarkmcc / expiringerrorcounter.go
Created July 25, 2020 15:10
ExpiringErrorCounter maintains a registry of counter errors that expires after a specified amount of time. This package also supports registering even handlers that will be called if the counter of a particular error crosses a threshold in the specified amount of time.
package threadsafe
import (
"sync"
"time"
)
// Based on https://gist.github.com/clarkmcc/2c40db4bb7b3aea412d78c8a490f030e
// ExpiringErrorCounter maintains a registry of counter errors that expires
// after a specified amount of time. For example if the expiration were set