Skip to content

Instantly share code, notes, and snippets.

View kuczmama's full-sized avatar
🔥
Building Cool Stuff

Mark Kuczmarski kuczmama

🔥
Building Cool Stuff
View GitHub Profile
@kuczmama
kuczmama / snow-consensus.js
Last active December 24, 2021 20:40
Avalanche Snow consensus in javascript
class Validator {
constructor(id, preference) {
this.id = id;
this.preference = preference;
this.decided = false;
}
}
const describeValidators = (validators) => {
const preferences = {};
@kuczmama
kuczmama / bencode.js
Last active July 24, 2020 12:28
A zero dependency bencode encoder and decoder in javascript
const encode = (data) => {
if(data == null) return null;
if(typeof data === 'number') {
return `i${data}e`;
}
if(typeof data === 'string') {
return `${data.length}:${data}`;
}
@kuczmama
kuczmama / async-callbacks.js
Created June 14, 2020 10:57
Async Callbacks in javascript
function fetchSports(prefix, callback) {
console.log("prefix", prefix);
const sports = ["vikings", "vipers"];
setTimeout(() => callback(sports), 500);
}
function fetchNews(prefix, callback) {
const news = ["victory", "votes"];
setTimeout(() => {
callback(news);
@kuczmama
kuczmama / decision_tree.rb
Last active March 5, 2020 12:11
A decision tree written in ruby
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'set'
require 'pry'
require 'csv'
require 'optparse'
require 'time'
# https://deeplizard.com/learn/video/0VCOG8IeVf8
import torch
import torch.nn as nn
import torch.nn.functional as F
import os.path
# Optimizer to update the weights
import torch.optim as optim
import torchvision
function connect() {
const URL = 'wss://localhost:8080';
const RECONNECT_DELAY = 1000;
const WAIT_FOR_NO_MESSAGE = 30000;
const handler = null;
var ws = new WebSocket(URL);
ws.onmessage = function(e) {
clearTimeout(handler);
setTimeout(() => ws.close(), WAIT_FOR_NO_MESSAGE);
@kuczmama
kuczmama / increase-swap.sh
Created October 3, 2019 05:37
Increase swap space in ubuntu 18.04
sudo fallocate -l 16G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo mount -a
swapon -s
@kuczmama
kuczmama / schema.sh
Created May 21, 2019 03:03
Heroku to RDS migration script from single to multi-tenant
#/bin/sh
usage() { echo "Usage: $0 APP_NAME DESTINATION SCHEMA_NAME" 1>&2; exit 1; }
if [ "$#" -ne 3 ]; then
usage
fi
function pathParts() {
url=$1
@kuczmama
kuczmama / markov_chain.rb
Last active May 16, 2019 03:23
n level markov chain to randomly generate text
class String
def blank?
self.nil? || self.empty?
end
def present?
!self.blank?
end
end
@kuczmama
kuczmama / id_to_uuid.rake
Last active July 24, 2022 22:06
Migrate a rails project to use uuids
# Inspired by http://www.madebyloren.com/posts/migrating-to-uuids-as-primary-keys
task id_to_uuid: :environment do
puts "[START] Convert id to uuid"
ActiveRecord::Base.connection.enable_extension 'uuid-ossp' unless ActiveRecord::Base.connection.extensions.include? 'uuid-ossp'
ActiveRecord::Base.connection.enable_extension 'pgcrypto' unless ActiveRecord::Base.connection.extensions.include? 'pgcrypto'
table_names = ActiveRecord::Base.connection.tables - ["schema_migrations", "ar_internal_metadata", "migration_validators"]
table_names.each do |table_name|
puts "[CREATE] uuid column for #{table_name}"