Skip to content

Instantly share code, notes, and snippets.

View elliotchance's full-sized avatar
🤓
Building awesome stuff with V

Elliot Chance elliotchance

🤓
Building awesome stuff with V
View GitHub Profile
next_xid = 1
active_xids = set()
records = []
def new_transaction():
global next_xid
next_xid += 1
active_xids.add(next_xid)
return Transaction(next_xid)
@elliotchance
elliotchance / example.go
Last active December 3, 2023 21:07
Capturing grouping for regex function replace in Go
func main() {
str := "abc foo:bar def baz:qux ghi"
re := regexp.MustCompile("([a-z]+):([a-z]+)")
result := ReplaceAllStringSubmatchFunc(re, str, func(groups []string) string {
return groups[1] + "." + groups[2]
})
fmt.Printf("'%s'\n", result)
}
package main
import (
"fmt"
"time"
)
func BatchStrings(values <-chan string, maxItems int, maxTimeout time.Duration) chan []string {
batches := make(chan []string)
@elliotchance
elliotchance / 1.js
Last active March 21, 2023 04:11
Generate list index
// separator is what to put between each link. Examples:
// - New line: '\n'
// - Comma: ', '
// - Pipe: ' | '
const separator = '\n';
// The link for the URL. It must contain a slash at the end.
// You cannot use the [List123] links.
const listURL = 'https://rateyourmusic.com/list/echance/a-state-of-trance-1/';
@elliotchance
elliotchance / 1.js
Created March 19, 2023 21:00
Sort list items by artist/title
let done;
do {
done = true;
Array.from(document.getElementsByClassName('box'))
.sort((a, b) => {
const [x, y] = [a.innerText, b.innerText];
const cmp = x.localeCompare(y);
if (cmp < 0) {
done = false;
document.getElementById(a.id).parentNode.insertBefore(document.getElementById(a.id), document.getElementById(b.id));
@elliotchance
elliotchance / bowling.py
Created May 3, 2016 11:39
Kata: The Bowling Game
import pytest
class Game:
def __init__(self):
self.frames = [[]]
self.allow_bonus_roll = False
@property
def last_frame(self):
return self.frames[-1]
@elliotchance
elliotchance / generate_tests.py
Created October 9, 2016 21:21
Parameterized Tests in Swift
import sys
import re
import glob
import os
files = []
for arg in sys.argv[1:]:
if os.path.isdir(arg):
arg += "/*.swift"
@elliotchance
elliotchance / elevator.py
Created September 10, 2016 08:36
The Elevator Game
import random
import time
class Person:
WAITING = 0
IN_ELEVATOR = 1
DONE = 2
def __init__(self, from_floor, to_floor):
self.from_floor = from_floor
package main
import (
"fmt"
"strings"
"sync"
"time"
)
type ChannelPerf struct {
@elliotchance
elliotchance / transactions.py
Created February 19, 2017 08:23
Implementing all four SQL transaction isolation levels in Python
# -*- coding: utf8 -*-
from __future__ import print_function
class LockManager:
def __init__(self):
self.locks = []
def add(self, transaction, record_id):
if not self.exists(transaction, record_id):