Skip to content

Instantly share code, notes, and snippets.

Avatar
🧐

Simon Eskildsen sirupsen

🧐
View GitHub Profile
@sirupsen
sirupsen / sidenotes.tsx
Last active Jan 5, 2022
Sidenotes and table of contents on MDX with React.
View sidenotes.tsx
function isColliding(a: DOMRect, b: DOMRect) {
return !(
((a.y + a.height) < (b.y))
|| (a.y > (b.y + b.height))
|| ((a.x + a.width) < b.x)
|| (a.x > (b.x + b.width))
);
}
// Move the footnotes from the bottom to become sidenotes if the viewport is
View monty_hall.rb
switch_strategy = 0
no_switch_strategy = 0
n_simulations = 1_000_000
n_simulations.times do
winner_door = rand(3)
chosen_door = rand(3)
if winner_door == chosen_door
switch_strategy += 0
View simulator.rb
n_shards = 128
n_simulations = 1_000_000
processed_booted_for_minimum_two_per_shard = []
n_simulations.times do |i| # Number of simulations
booted = 0
booted_by_shard = [0] * n_shards
done = [false] * n_shards
done_count = 0
View ping.rb
require 'socket'
def internet_checksum(packet) # https://tools.ietf.org/html/rfc1071
packet += [0].pack('C') if packet.size.odd? # since we read 2 bytes at once, append 0 if odd
sum = packet.unpack("n#{packet.size/2}").sum
sum = (sum & 0xffff) + (sum >> 16) while (sum >> 16 > 0) # fold 32 bits -> 16
~sum & 0xffff # 1s complement
end
@sirupsen
sirupsen / queuing.rb
Created Feb 14, 2020
Queuing theory exercise
View queuing.rb
class Node
attr_accessor :edges, :jobs, :id
def initialize(id, arrivals_per_tick)
@id = id
@jobs = 0.0
@tick = 0
@edges = []
@arrivals_per_tick = arrivals_per_tick
end
View zettelkasten-search.rb
require "sqlite3"
require 'set'
require 'byebug'
# Will be rebuilt at any time. Nice and incremental.
db = SQLite3::Database.new "index.db"
# Keep prefix indexes for "mos*" searches.
#
# TODO: It doesn't seem like SQLITE FTS5 supports synonyms well. That's ok, but
# we're going to want that. We can download this database from Princeton, write
View black_box.rs
use std::io::{Read, Seek, SeekFrom, Write};
use std::slice;
use std::time::Instant;
use std::{fs::OpenOptions, io::Result};
use std::ptr;
use std::mem::forget;
const BUF_SIZE: usize = 1024 * 32;
const FILE_SIZE: usize = 1024 * 1024 * 512;
const QUEUE_DEPTH: usize = 32;
View sequantial_io.rs
use std::{fs::OpenOptions, io::Result};
use std::io::{Read, Write, Seek, SeekFrom};
use std::slice;
use std::time::Instant;
const BUF_SIZE: usize = 1024 * 32;
const FILE_SIZE: usize = 1024 * 1024 * 512;
const QUEUE_DEPTH: usize = 32;
fn main() -> Result<()> {
@sirupsen
sirupsen / ping_less.rb
Created Mar 7, 2019
Ping less patch for MySQL.
View ping_less.rb
# frozen_string_literal: true
# By default, ActiveRecord will issue a PING command to the database to check
# if it is active at various points. This overhead is unnecessary; we instead
# attempt to issue queries without checking the connection, then if the query
# returns an error and the connection was closed, try to reconnect.
# This also allows for reconnection during a single UoW, improving resiliency
# under transient connection failure (e.g. ProxySQL restarts).
#
# To avoid amplifying load when a database is intermittently down, the attempt
@sirupsen
sirupsen / book.rb
Last active Jun 24, 2022
Script to import books from Instapaper to Airtable. Will not work out of the box.
View book.rb
class Book < Airrecord::Table
class Endorser < Airrecord::Table
self.base_key = ""
self.table_name = "Endorser"
end
self.base_key = ""
self.table_name = "Books"
has_many :endorsements, class: 'Book::Endorser', column: 'Endorsements'