Skip to content

Instantly share code, notes, and snippets.

@crvdgc
crvdgc / dr-racket-run-but-keep-focus.rkt
Last active August 23, 2023 20:32
This keybinding file binds `F4` to execute `Indent all`, `Run`, and keep the focus in the editor. It basically follows [the example](https://docs.racket-lang.org/drracket/Keyboard_Shortcuts.html#%28part._defining-shortcuts%29) in the reference. I find it useful for trying out examples in *The Little Prover*.
#lang s-exp framework/keybinding-lang
(require drracket/tool-lib)
(require racket/gui/base)
(module test racket/base)
(define (call-menu menu-item)
(λ (ed evt)
(define canvas (send ed get-canvas))
(when canvas
@crvdgc
crvdgc / math-render-error.md
Last active June 27, 2022 03:16
Showcase and analyze a rendering error of MathJax in GitHub Markdown

GitHub Markdown MathJax rendering error

Two inlines after text will hide the text in between

where $T$ is lookback; $\tau$ is horizon.

renders to

@crvdgc
crvdgc / .bashrc
Created June 9, 2022 14:51
Set up bash prompt to show vi-mode status, nix-shell, (colored) git status, and exit status. Excerpt from https://github.com/crvdgc/home-manager
# download git-prompt.sh from
# https://github.com/git/git/raw/master/contrib/completion/git-prompt.sh
# put it into ~/.git-prompt.sh
source ~/.git-prompt.sh
# Colors
RESET_COLOR='\[\e[0m\]'
BRIGHT_BLACK='\[\e[30;1m\]'
RED='\[\e[31m\]'
@crvdgc
crvdgc / fp101x-chapter7-part2.hs
Last active April 26, 2022 02:53
FP101x monadic parser example
import Data.Char (isDigit)
data Parser a = Parser (String -> [(a, String)])
instance Functor Parser where
fmap f (Parser pa) = Parser $ \inp ->
flip fmap (pa inp) $ \(a, out) -> (f a, out)
instance Applicative Parser where
pure a = Parser $ \inp -> [(a, inp)]
@crvdgc
crvdgc / prism-type.hs
Created October 8, 2021 20:14
Deduce the type of `prism`
prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b
prism bt seta = dimap seta (either pure (fmap bt)) . right'
type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)
prism :~: (b -> t) -> (s -> Either t a) -> p a (f b) -> p s (f t)
class Profunctor p => Choice p where
left' :: p a b -> p (Either a c) (Either b c)
right' :: p a b -> p (Either c a) (Either c b)
@crvdgc
crvdgc / scroll-half.el
Created September 28, 2021 03:49
Scroll half screen in emacs as in vim
(defun scroll-up-half-screen ()
"<C-d> a la Vim"
(interactive)
(scroll-up-command)
(recenter))
(global-set-key (kbd "C-;") 'scroll-up-half-screen)
(defun scroll-down-half-screen ()
"<C-u> a la Vim"
(interactive)
(scroll-down-command)
@crvdgc
crvdgc / reversible.hs
Last active August 1, 2019 17:06
Reversible two-input demultiplexer. The target is to design a reversible circuit, using NOT, CNOT, Toffoli, and Fredkin gates, which acts on the two arbitrary inputs a,b, and the two fixed inputs c=0, d=0, to produce four bits a′, b′, c′, d′ of output, where only the nth output is 1 (the others are all 0), and n=2b+a.
{-# LANGUAGE BinaryLiterals #-}
import Data.Bits (Bits, xor, bit, testBit, complementBit, setBit, clearBit, (.&.), shiftR)
import Data.Array (Array, listArray, (//), (!))
initialState = [0b0000, 0b0001, 0b0010, 0b0011]
targetState = [0b0001, 0b0010, 0b0100, 0b1000]
encodeState = sum . zipWith (*) [4096, 256, 16, 1]
@crvdgc
crvdgc / poorman-pagerank.hs
Created April 26, 2019 03:43
Poorman's PageRank and eigenvector centrality
import Control.Monad
import Data.List (transpose)
import Text.Printf
converge :: Eq a => (a -> a) -> a -> a
converge f v = fst $ until theSame update (v, f v)
where
theSame (x, y) = x == y
update (x, y) = (y, f y)
@crvdgc
crvdgc / memento_solver.py
Last active July 17, 2018 05:53
To the Moon, in-game memento solver
import itertools
def solve_memento():
rowNum = int(input('Row number: '))
colNum = int(input('Col number: '))
best = int(input('Best move: '))
print("Input table, 1 for solved, 0 for unsolved")
table = [list(map(lambda x: True if int(x) == 1 else False, input().split())) for i in range(rowNum)]
def check():
@crvdgc
crvdgc / highlight.html
Created May 6, 2018 08:22
highlightjs add language class to all code blocks
<!-- if you are using sublime, uncomment to get a snippet
<snippet>
<content><![CDATA[
-->
<link rel="stylesheet" href="PATH_TO_YOUR_CSS\highlight\styles\github.css">
<script src=PATH_TO_YOUR_JS\highlight\highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();
let addLangClass = function () {
document.getElementByTagName("code").classList.add("${1:language}");
}