Skip to content

Instantly share code, notes, and snippets.

@JakeCoxon
JakeCoxon / tokeniser.ts
Created December 2, 2022 15:56
Tokenize Python-like whitespace language
function tokenize(input: string) {
const regexes = {
KEYWORD:
/^(?:and|assert|as|break|class|continue|def|elif|else|false|for|if|import|in|is|lambda|null|not|or|pass|return|try|while|with)/,
IDENTIFIER: /^[a-zA-Z_][a-zA-Z_0-9]*/,
LITERAL: /^(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')/,
SPECIALNUMBER: /^0[xXbB][0-9a-zA-Z_]+/,
NUMBER: /^-?[0-9][0-9_]*(\.[0-9_]+)?/,
COMMENT: /^#.+(?=\n)/,
@JakeCoxon
JakeCoxon / aoc2021.py
Last active March 8, 2023 23:05
Advent of Code 2021
# Python 2 only because they removed lambda argument unpacking in 3.0
import math, sys
from functools import reduce
from itertools import izip_longest
# AOC Day 1 part 1
print(len((lambda nums: [1 for x, y in zip(nums[:], nums[1:]) if y > x])
([int(x) for x in open("input1.txt").read().split()])))
# AOC Day 1 part 2
@JakeCoxon
JakeCoxon / distribute.py
Created December 24, 2020 21:32
Blender addon to distribute objects along an axis
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# https://github.com/uBlockOrigin/uAssets/pull/3517
twitch-videoad.js application/javascript
(function() {
if ( /(^|\.)twitch\.tv$/.test(document.location.hostname) === false ) { return; }
var realFetch = window.fetch;
window.fetch = function(input, init) {
if ( arguments.length >= 2 && typeof input === 'string' && input.includes('/access_token') ) {
var url = new URL(arguments[0]);
url.searchParams.forEach(function(value, key) {
url.searchParams.delete(key);
#!/bin/bash
# forked from https://github.com/andreyvit/bulk-rename/blob/master/bulk-rename
usage() {
echo "Rename/move/copy files by editing in your editor"
echo
echo "Example - rename all md files"
echo " /path/to/bulk-rename *.md"
echo
@JakeCoxon
JakeCoxon / index.html
Created June 4, 2020 16:23
Procedural symbols
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<link rel='stylesheet' href="styles.css"/>
</head>
<body>
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script>
@JakeCoxon
JakeCoxon / github.js
Last active December 4, 2019 14:36
Github PRs Highlight name
let username = document.querySelector('meta[name="user-login"]').getAttribute('content');
document.querySelectorAll('.js-issue-row').forEach(row => {
let userLink = row.querySelector('a[data-hovercard-url="/users/' + username + '/hovercard"]')
if (!userLink) return;
Object.assign(userLink.style, {
background: '#3940c3',
borderRadius: '5px',
@JakeCoxon
JakeCoxon / effects.py
Created July 21, 2019 17:41
Algebraic Effects in Python
import asyncio
from greenlet import greenlet
from dataclasses import dataclass
class Effect:
pass
def run_effect(eff):
import { useRef, useCallback, useEffect } from 'react';
// A callback that always closes over the latest data but keeps the same
// identity and will not be called after component unmounts
const useStableCallback = callback => {
const callbackRef = useRef();
const memoCallback = useCallback(
(...args) => callbackRef.current && callbackRef.current(...args),
[],
@JakeCoxon
JakeCoxon / example1.js
Created February 17, 2019 17:29
React navigation header hooks example
function MyScreen({}) {
const [numSelected setNumSelected] = useState(false);
useHeaderTitle(numSelected > 0 ? `${numSelected} tasks selected` : 'Task selection');
return ...
}