Skip to content

Instantly share code, notes, and snippets.

private static Form form;
private static Thread thread;
private static event Action showRequest;
public static void OpenForm()
{
if (thread == null)
{
thread = new Thread(() =>
{
// ==UserScript==
// @name Stop gif animations on escape
// @namespace http://github.com/johan/
// @description Implements the "stop gif animations on hitting the escape key" feature that all browsers except Safari and Google Chrome have had since forever. Now also for Google Chrome!
// ==/UserScript==
document.addEventListener('keydown', freeze_gifs_on_escape, true);
function freeze_gifs_on_escape(e) {
if (e.keyCode == 27 && !e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey) {
@LukaHorvat
LukaHorvat / packing
Created February 11, 2014 11:19
Rectangle packing in F#
namespace Firefly2.Functional.RectanglePacking
type IRectangle =
abstract member X : double with get, set
abstract member Y : double with get, set
abstract member Width : double
abstract member Height : double
abstract member Id : int
type internal Spot(x: double, y: double, width: double, height: double) =
{-# LANGUAGE PatternSynonyms #-}
module Reduction where
import Data.Maybe
import Function
pattern NumFrac n d = Number n :/: Number d
isConst :: Expression -> Bool
isConst (Number _) = True
type Grid = [String]
validGrid :: Grid -> Bool
validGrid [] = False
validGrid x = allEqual $ map length x
validateGrid :: Grid -> Grid
validateGrid g@(validGrid -> True) = g
validateGrid _ = error "Broken grid!"
--Converts a move into a bitmask
naiveMove :: Int -> Int --Size of the grid
-> Int -> Int --Coordinates of the move
-> Integer --Resulting grid data
naiveMove width height moveX moveY = foldl set 0 $ (moveX, moveY) : neighbors where
set bits (x, y)
| x < 0 || y < 0 || x >= width || y >= height = bits
| otherwise = bits `setBit` ((y * width) + x)
neighbors = [(moveX + i, moveY + j) | i <- [-1..1], j <- [-1..1], abs i + abs j == 1]
--Converts a move into a bitmask
naiveMove :: Int -> Int --Size of the grid
-> Int -> Int --Coordinates of the move
-> Integer --Resulting grid data
naiveMove width height moveX moveY = foldl set 0 $ (moveX, moveY) : neighbors where
set bits (x, y)
| x < 0 || y < 0 || x >= width || y >= height = bits
| otherwise = bits `setBit` ((y * width) + x)
neighbors = [(moveX + i, moveY + j) | i <- [-1..1], j <- [-1..1], abs i + abs j == 1]
--A simple stack-based RPN interpreter
rpnCalc :: String -> Int
rpnCalc expr = if null res then 0 else head res where
res = foldl eval [] expr
eval stack c
| c `elem` ['0'..'9'] = digitToInt c : stack
| otherwise = func c top2 top1 : pop2
where (top1, _) = pop 1 stack
(top2, pop2) = pop 2 stack
--Char to function mappings
mergesort :: Ord a => [a] -> [a]
mergesort [] = []
mergesort [x] = [x]
mergesort (split -> (left, right)) = merge (mergesort left) (mergesort right)
where merge [] rs = rs
merge ls [] = ls
merge (l : ls) (r : rs)
| l < r = l : merge ls (r : rs)
| otherwise = r : merge (l : ls) rs
pacMan :: Int -> [Dependency] -> [Package]
pacMan n deps
| outOfBounds = error "Impossible to resolve"
| Just xs <- try deps = xs ++ top xs
| otherwise = error "Impossible to resolve"
where outOfBounds = any (> n) $ deps >>= (\(x, y) -> [x, y])
try d | null d = Just []
try d = do
let dependedOn = S.fromList $ map snd d
let dependers = S.fromList $ map fst d