Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / sinatra-react.md
Last active January 21, 2025 11:44
How to set up a basic Sinatra + React webapp

How to set up a basic Sinatra + React webapp in 2025

Let's say you want to use Ruby for the backend of a basic webapp but React on the frontend. Here's how.

(Note: All tested on January 13, 2025 with Ruby 3.3, Sinatra 4.1.1, and React 18.3. Configs may change over time.)

First, create the app folder and set up Sinatra:

mkdir my-sinatra-react-app
@peterc
peterc / multiply.swift
Created December 21, 2024 18:20
Multiply two numbers using the GPU via Metal on macOS
import Metal
// Inline Metal shader code
let shaderSource = """
kernel void multiply(const device float* a [[ buffer(0) ]],
const device float* b [[ buffer(1) ]],
device float* result [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]]) {
result[id] = a[id] * b[id];
@peterc
peterc / make_docs.sh
Last active November 26, 2024 20:56
Script to take a remote git repo (e.g. GitHub) and create some basic documentation for it
#!/bin/bash
# Check if a repository URL was provided
if [ $# -ne 1 ]; then
echo "Usage: $0 <repository-url>"
exit 1
fi
REPO_URL=$1
@peterc
peterc / mandelbrot.sql
Created November 24, 2024 21:00
Mandelbrot set in Postgres SQL
WITH RECURSIVE
xaxis(x) AS (
SELECT -2.0
UNION ALL
SELECT x + 0.05 FROM xaxis WHERE x < 1.2
),
yaxis(y) AS (
SELECT -1.0
UNION ALL
SELECT y + 0.1 FROM yaxis WHERE y < 1.0
@peterc
peterc / JSON-README.md
Last active November 7, 2024 21:22
LLM generated documentation for Ruby's "json" gem
@peterc
peterc / README.md
Created August 23, 2024 14:11
Basic assembly Hello World example for macOS on Apple Silicon/arm64

To compile:

as -o hello.o hello.s
ld -o hello hello.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _main -arch arm64

./hello
@peterc
peterc / README
Created July 15, 2024 14:30
Reconstruction of Clément Jean's binary search code from https://clement-jean.github.io/simd_binary_search_tree/
Seems to work, but I'm not an expert with this stuff, so YMMV.
@peterc
peterc / groq_client.rb
Last active April 19, 2024 17:45
Basic Groq API client for Ruby
require 'http'
require 'json'
class GroqClient
def initialize(api_key: nil, api_url: "https://api.groq.com/openai/v1/chat/completions", model: "mixtral-8x7b-32768")
@api_key = api_key || ENV['GROQ_API_KEY']
@api_url = api_url
@model = model
end
@peterc
peterc / Caddyfile
Last active March 16, 2024 11:52
Caddyfile for running Mastodon – November 2022 edition
put.your.domain.here {
@local {
file
not path /
}
log {
output file /var/log/caddy/mastodon.log
}
@peterc
peterc / embedding_store.rb
Last active December 28, 2023 06:27
Using SQLite to store OpenAI vector embeddings from Ruby
# Example of using SQLite VSS with OpenAI's text embedding API
# from Ruby.
# Note: Install/bundle the sqlite3, sqlite_vss, and ruby-openai gems first
# OPENAI_API_KEY must also be set in the environment
# Other embeddings can be used, but this is the easiest for a quick demo
# More on the topic at
# https://observablehq.com/@asg017/introducing-sqlite-vss
# https://observablehq.com/@asg017/making-sqlite-extension-gem-installable