Skip to content

Instantly share code, notes, and snippets.

View ameykusurkar's full-sized avatar

Amey Kusurkar ameykusurkar

View GitHub Profile
# `items` table pre-populated with millions or rows
class Item < ApplicationRecord; end
# Worker loop finds rows and deletes them
def consume(n)
loop do
start = ::Utils::Timing.monotonic_now
count = Item.transaction do
# Lock first N rows
items = Item.limit(n).lock("FOR UPDATE SKIP LOCKED").order(:id).to_a
-- DEMO: TRANSACTIONS
--
-- Create table
CREATE TABLE messages (
id int PRIMARY KEY,
payload text
);
-- Insert 3 tuples atomically
BEGIN;
module FizzBuzz where
import Data.Maybe (fromMaybe)
fizzbuzz n = fromMaybe (show n) ((fizz <> buzz) n)
where fizz = say 3 "Fizz"
buzz = say 5 "Buzz"
say :: Int -> String -> Int -> Maybe String
say x phrase n = if mod n x == 0 then Just phrase else Nothing
@ameykusurkar
ameykusurkar / square_root.py
Created October 2, 2021 12:48
A simple algorithm to calculate square roots of integers
import itertools
from math import sqrt
def estimate_root(x: int) -> int:
return next(i for i in itertools.count() if i * i > x)
def root(x: int) -> float:
guess = estimate_root(x)
return narrow_guess(x, lower=guess-1, upper=guess)

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@ameykusurkar
ameykusurkar / bounce.html
Created March 28, 2018 14:52
A bouncing re-sizing ball using vanilla js
<canvas id="bounce" width="800" height="400" style="border:1px solid"></canvas>
<script>
const frameRate = 60;
window.onload = function() {
canvas = document.getElementById("bounce");
ctx = canvas.getContext("2d");
document.addEventListener("keydown", keyPressed);
setInterval(game, 1000/frameRate);
@ameykusurkar
ameykusurkar / wavefront.c
Created November 21, 2017 15:03
Skews the loop iteration to account for wavefront-like dependencies
#include <stdio.h>
int min(int a, int b)
{
if (a < b) return a;
else return b;
}
int max(int a, int b)
{