Skip to content

Instantly share code, notes, and snippets.

View oantolin's full-sized avatar

Omar Antolín Camarena oantolin

View GitHub Profile
@oantolin
oantolin / consumptive-components.el
Created April 1, 2023 21:03
Component separator for orderless which makes patterns consume their match
(defun partition (pred list)
"Split LIST into elements for which PRED returns true and false."
(let (true false)
(dolist (elt list)
(if (funcall pred elt)
(push elt true)
(push elt false)))
(cons (nreverse true) (nreverse false))))
(defun connected-components (vertices adjacentp)
;; -*- lexical-binding: t; -*-
;; to use run: emacs --batch -l standalone-eshell.el
(require 'em-prompt)
(let ((dir default-directory))
(add-hook 'eshell-directory-change-hook
(lambda () (setq dir default-directory)))
(while t
@oantolin
oantolin / py-docstring.el
Last active March 31, 2021 21:24
Primitive python docstring generator in Emacs Lisp
(defun py-docstring ()
"Primitive Python docstring generator."
(interactive)
(save-excursion
(beginning-of-defun)
(down-list)
(let* ((all-args
(mapcar
(lambda (s) (split-string s "\\s-*=\\s-*"))
(split-string (buffer-substring
@oantolin
oantolin / cicio.lua
Created January 17, 2021 17:46
Lisp dialect that compiles to Lua: no documentation, no error reporting
syntax = {}
for _, kind in ipairs {"vector", "table"} do
local mt = {}
syntax[kind] = function (x) return setmetatable(x, mt) end
syntax[kind .. "_p"] = function (x) return type(x)=="table" and getmetatable(x)==mt
end
end
function syntax.form_p(x)
(fn comprehend [init update ...]
(let [clauses [...] n (length clauses) result (gensym)]
(tset clauses n (update result (. clauses n)))
(for [i (- n 1) 1 -1] (table.insert (. clauses i) (. clauses (+ i 1))))
`(do (var ,result ,init) ,(. clauses 1) ,result)))
(fn tbl [...]
(comprehend {}
(fn [result expr] `(match ,expr (k# v#) (tset ,result k# v#)))
...))
@oantolin
oantolin / contrast.c
Last active August 17, 2023 21:30
Find most luminous contrasting color
#include <math.h>
#include <stdio.h>
double f(int c) {
double x = c / 255.0;
return x<=0.03928 ? x/12.92 : pow((x + 0.055)/1.055, 2.4);
}
double rellum(int r, int g, int b) {
return 0.2126*f(r) + 0.7152*f(g) + 0.0722*f(b);
@oantolin
oantolin / my-smartparens-config.el
Created June 25, 2017 03:58
My smartparens configuration
(use-package smartparens
:diminish smartparens-mode
:init
(smartparens-global-mode)
:config
(require 'smartparens-config)
(add-hook 'eval-expression-minibuffer-setup-hook #'smartparens-mode)
(custom-set-variables
'(sp-base-key-bindings 'sp)
'(sp-override-key-bindings
@oantolin
oantolin / turn-off-openwith-mode.el
Created December 3, 2014 22:20
Turn off openwith-mode for certain commands
(defun turn-off-openwith-mode (orig-fun &rest args)
"Ensure openwtih-mode is off when running dired-mark-files-containing-regexp"
(let ((openwith-mode-on? openwith-mode))
(when openwith-mode-on? (openwith-mode -1))
(apply orig-fun args)
(when openwith-mode-on? (openwith-mode 1))))
(dolist (cmd '(dired-mark-files-containing-regexp message-send-and-exit))
(advice-add cmd :around #'turn-off-openwith-mode))
@oantolin
oantolin / T.java
Created March 28, 2014 02:22
Help me make this Clojure code about as fast as Java, please!
public class T {
static int f(int x, int y, int z, int n) {
return 2*(x*y+y*z+x*z) + 4*(x+y+z+n-2)*(n-1);
}
static int[] g(int n) {
int[] count = new int[n+1];
for (int x=1; f(x,x,x,1)<=n; x++)
for (int y=x; f(x,y,y,1)<=n; y++)
for (int z=y; f(x,y,z,1)<=n; z++)
@oantolin
oantolin / donttakemoney.py
Created March 25, 2014 14:48
DONT + TAKE = MONEY
ds = set(range(2,10))
for t in ds:
e = 11-t
for n in ds - {t,e}:
for x in [0,1]:
k = 10*x+e-n-1
if k in ds - {t,e,n}:
for o in ds - {t,e,n,k}:
for y in [0,1]:
a = 10*y+n-o-x