Skip to content

Instantly share code, notes, and snippets.

@Jach
Jach / matrixquerything.lisp
Created February 16, 2022 07:17
I hate it...
(defstruct cell
color
value
i
j)
(defun create-cells (matrix)
(let ((cells (list))
(color :white))
(loop for i below (array-dimension matrix 0) do
@Jach
Jach / fmod-test.lisp
Last active October 2, 2021 16:48
Simple hacky test of using cl-autowrap and cffi to load fmod with Lisp and get a song to play
;; Public domain example of using cl-autowrap and cffi to follow using the core api documented at
;; https://fmod.com/resources/documentation-api?version=2.0&page=white-papers-getting-started.html
;; to load and play a sound file with fmod.
;; This file assumes it's in the downloaded fmodstudioapi20009linux/ folder,
;; which contains the C includes and pre-built dynamic libraries.
;; Try it out, evaluating step by step!
(ql:quickload :cl-autowrap)
(defpackage :fmod-test
@Jach
Jach / sdl-intro.lisp
Last active August 8, 2021 03:20
Hello world with SDL2 and Common Lisp. Somewhat explanatory blog post here https://www.thejach.com/view/2020/11/hello_world_to_get_with_the_times_using_sdl2_and_common_lisp
#|Public domain. Does not unwind-protect errors like it should.|#
(in-package #:cl-user)
(defpackage #:sdl-intro
(:use #:common-lisp))
(in-package #:sdl-intro)
(ql:quickload "sdl2")
(defun null-ptr? (alien-val)
(cffi:null-pointer-p (autowrap:ptr alien-val)))
@Jach
Jach / bodyweight.lisp
Last active June 8, 2021 05:54 — forked from stucchio/gist:1403042
Graphs of bodyweight vs time, Lisp version
; quick and dirty adaptation from https://gist.github.com/stucchio/1403042
(ql:quickload :numcl)
(ql:quickload :vgplot)
(defparameter *exercise-level* 1.5)
(defparameter *height-inches* (* 6 12))
(defparameter *a* (/ (* *exercise-level* (+ 66 (* 12.7 *height-inches*)))
3500.0))
(defparameter *b* (/ (* *exercise-level* 6.23)
3500))
// ==UserScript==
// @name Twitter Selected Users
// @version 1
// @grant none
// @namespace local
// @include https://twitter.com/*
// ==/UserScript==
// Brittle bare-minimum works-for-me code ahead!
// License: Public Domain (see bottom)
@Jach
Jach / sieve.py
Created February 7, 2012 18:43
Incremental Sieve of Eratosthenes
'''
Python implementation of Haskell's
sieve xs = sieve' xs Map.empty
where
sieve' [] table = []
sieve' (x:xs) table =
case Map.lookup x table of
Nothing -> x : sieve' xs (Map.insert (x*x) [x] table)
Just facts -> sieve' xs (foldl reinsert (Map.delete x table) facts)
@Jach
Jach / number_hack.py
Created September 10, 2011 11:44
Overrides the Python integer five to be equal to four with ctypes magic
import sys
import ctypes
pyint_p = ctypes.POINTER(ctypes.c_byte*sys.getsizeof(5))
five = ctypes.cast(id(5), pyint_p)
print(2 + 2 == 5) # False
five.contents[five.contents[:].index(5)] = 4
print(2 + 2 == 5) # True (must be sufficiently large values of 2 there...)
;(ql:quickload :cl-ppcre)
;(ql:quickload :cl-interpol)
;(named-readtables:in-readtable :interpol-syntax)
(defun full-match (pattern string)
"Something like Python's re.fullmatch.
Example (with cl-interpol):
(full-match #?/\s/ #?' ') -> T
(full-match #?/\s/ #?'') -> nil"
(equal string (cl-ppcre:scan-to-strings pattern string)))
; union-find.lisp
;; A generic solution for connected components. "I have some number of graphs. Is this element connected to
;; this other element? Or are the graphs/sets each element belongs to disjoint?"
;;
;; A 'backwards' tree with pointers from a node to its parent, which lets you union two separate trees together by
;; just taking the shorter one's root and pointing it at the taller one's (or vice versa, but this way preserves log
;; behavior).
;; The path compression optimization (not done here) seems to just be an extra pass in find(), after you have the result,
;; to re-parent each item along the path to the found root parent so that any future finds() of any of those items
@Jach
Jach / unicode_extract.lisp
Last active January 15, 2019 16:17
extract letter unicodes for js regexes
#|
This file is in the public domain.
I grabbed the data from ucd.all.flat.zip at ftp://ftp.unicode.org/Public/11.0.0/ucdxml/
and use this script to create a UnicodeLetters.txt file
containing a series of unicode chars suitable for using in JavaScript regular expressions
where you want \p{L}.
Note that IE does not support the 'u' flag, you'll need to restrict yourself to the subset of
letters in the BMP (below 0x10000) and use this script to print out a simple "\uABCD" instead of "\\u{ABCDE}".
Also note that reading in the file seems to stress SBCL's default settings, run with
--dynamic-space-size 10000