Skip to content

Instantly share code, notes, and snippets.

@alisey
alisey / README.md
Created June 23, 2022 08:08
Detect React hooks where dependencies change on every render, making memoization ineffective

Usage

// This import should come before React import
import './react-amnesia-checker.ts';

import React from 'react';
import ReactDOM from 'react-dom';

ReactDOM.render(...);
@alisey
alisey / README.md
Last active December 8, 2021 13:12
Show GitHub notification count in your macOS menu bar

GitHub Notifications

This little app shows the number of unread GitHub notifications in macOS menu bar.

There are other similar apps, but they either run a full fucking Electron, or have too many other features and look busy.

Installation

  1. Download and install xbar (beta version is fine). It's a small app that allows you to add output of any script (Bash, Python, JS) to the menu bar.
  2. Launch it, go to the menu, enable "Start at Login".
@alisey
alisey / rotation.py
Created December 24, 2014 17:55
Rotate matrix in Python
def swap(matrix, r1, c1, r2, c2):
matrix[r1][c1], matrix[r2][c2] = matrix[r2][c2], matrix[r1][c1]
def transpose(matrix):
for row in range(len(matrix)):
for col in range(row):
swap(matrix, row, col, col, row)
def flip_vertical(matrix):
size = len(matrix)
@alisey
alisey / http-server-2.py
Created August 8, 2014 12:28
Quick and dirty HTTP server in Python
from http.server import HTTPServer, SimpleHTTPRequestHandler
# In Python 2 http.server is called BaseHTTPServer
class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
command = self.path[1:]
if command == 'wakeup':
response = 'Time to wake up!'
else:
@alisey
alisey / http-server.py
Created August 8, 2014 11:32
Quick and dirty HTTP server in Python based on socketserver lib
import socketserver # In Python 2 use "import SocketServer" instead
class RequestHandler(socketserver.StreamRequestHandler):
def handle(self):
command = self.rfile.readline()[5:-11].decode('UTF-8')
print(command)
if command == 'sleep':
self.respond('Going to sleep')
elif command == 'wakeup':
import Data.Bits
collatzLen :: Int -> Int -> Int
collatzLen len x
| x == 1 = len
| testBit x 0 = collatzLen (len + 2) (shiftR (3*x + 1) 1)
| otherwise = collatzLen (len + 1) (shiftR x 1)
main = print . snd . maximum $ [(collatzLen 0 x, x) | x <- [500000..1000000]]
@alisey
alisey / marble_puzzle.js
Created May 23, 2013 21:15
Dropping marbles from the building until they break.
// Minimize the number of drops required in the worst case and return [floor, drops] -
// the floor we should choose and the number of drops we'll have to make in the
// worst case.
function minMaxDrops(buildingHeight, marbles, cache) {
if (marbles == 0) {
return [0, Infinity];
}
if (marbles == 1) {
// If we have just 1 marble, the only option is to start at the bottom floor
@alisey
alisey / parse_query_string.js
Last active December 11, 2015 05:28
Parsing URL query string in JavaScript.
// Examples:
// parseQueryString('a=1&a=2') => {a: '2'}
// parseQueryString('a=1', true) => {a: ['1']}
// parseQueryString('a=1&a=2', true) => {a: ['1', '2']}
// parseQueryString('a[][]=1&a[][]=2', true) => {'a[][]': ['1', '2']}
// parseQueryString('?a;b#c') => {'?a': '', 'b#c': ''}
function parseQueryString(query, groupByName) {
var parsed, hasOwn, pairs, pair, name, value;
if (typeof query != 'string') {
@alisey
alisey / scrollSelectionIntoView.js
Created September 18, 2012 17:45
Scroll selection into view
// Scroll to selection focus, but only if it's out of view. Align selection
// focus with the top or bottom edge of its scroll-container. Return true
// on success.
// * there might be several nested scroll-containers, including window
// * must not try to scroll overflow:hidden and overflow:visible elements
// * no scrolling should happen if selection focus is visible
// * selection is not necessarily collapsed
// * range.getBoundingClientRect doesn't work for collapsed ranges
// * Opera reports incorrect startOffset and endOffset for collapsed ranges
// outside of text nodes (e.g. between 2 <br> elements), range.insertNode
@alisey
alisey / regexp_matcher.py
Created May 25, 2012 19:03
Python port of regexp matcher from 'The Practice of Programming'
# Python port of Rob Pike's regexp matcher from 'The Practice of Programming'
# Idea for match() stolen from https://github.com/darius/sketchbook/blob/master/misc/pike.py
def match(re, text):
"""
Search for regexp anywhere in text
c - any literal character
. - any single character
^ - the beginning of the input string
$ - the end of the input string