Skip to content

Instantly share code, notes, and snippets.

@carld
carld / shuffle.scm
Created October 5, 2016 07:51
shuffle list
(define (shuffle deck)
(sort (lambda (x y) (even? (random 2))) deck))
@carld
carld / cart.scm
Created October 7, 2016 00:42
cartesian product of lists
(define (product . args)
(if (null? args)
(list '())
(apply append
(map (lambda (rest)
(map (lambda (first)
(cons first rest))
(car args)))
(apply product (cdr args))))))
@carld
carld / time_converter.awk
Created November 22, 2016 20:28
example use of awk to format a date
function convert_time(t) {
split(t,a,"/");
if (a[3] > 50) {
a[3] = sprintf("19%02d", a[3]);
} else {
a[3] = sprintf("20%02d", a[3]);
}
month = a[1];
day = a[2];
year = a[3];
@carld
carld / bmr.scm
Last active December 22, 2016 08:53
Domain Specific Language for Calculating Basal Metabolic Rate
; Scheme based Domain Specific Language for computing Basal Metabolic Rate
; using the Harris-Benedict formula.
; https://en.wikipedia.org/wiki/Harris–Benedict_equation
"Goal: implement syntax transformers for a program like the following:"
'(basal-metabolic-rate
(metric
(male
(weight 90 kg)
(height 190 cm)
@carld
carld / accounting.sql
Created February 18, 2017 08:59 — forked from NYKevin/accounting.sql
Basic double-entry bookkeeping system, for PostgreSQL.
CREATE TABLE accounts(
id serial PRIMARY KEY,
name VARCHAR(256) NOT NULL
);
CREATE TABLE entries(
id serial PRIMARY KEY,
description VARCHAR(1024) NOT NULL,
amount NUMERIC(20, 2) NOT NULL CHECK (amount > 0.0),
-- Every entry is a credit to one account...
@carld
carld / eliza.js
Last active February 27, 2017 10:44
PAIP, Chapter 5.3, in Javascript
var variablep = function(x) {
// String is a primitive, so x instanceof String does not do what you think
return (typeof x === "string") && (x[0] == "?");
}
var equal = function(x,y) {
if (x === y) return true;
if (x == y) return true;
if (typeof x === 'string' && typeof y === 'string'
&& x.toLowerCase() == y.toLowerCase()) return true;
if (typeof x === 'array' && typeof y === 'array' && x.length == y.length) {
@carld
carld / sjs.scm
Created February 27, 2017 10:30
Converting s-expressions (Lisp/Scheme) into Javascript (JSON)
(define (js-format l)
(cond
((null? l) "")
((not (list? (car l))) (string-append (format #f "'~a'" (car l))
(if (not (null? (cdr l)))
(string-append "," (js-format (cdr l)))
"")))
(else (string-append "\n[" (js-format (car l))
(if (not (null? (cdr l)))
(string-append "]," (js-format (cdr l)))
@carld
carld / 3000lines.sh
Created March 19, 2017 04:38
Find files that have greater than 3000 lines
wc -l data/* | awk '{if ($1 > 3000) print $1, $2}'
@carld
carld / column_count_csv.sh
Created March 19, 2017 22:24
Count number of columns in a CSV file
head -1 returns.csv | sed 's/[^,]//g' | wc -c
@carld
carld / emacs-rename.txt
Created April 4, 2017 02:00
Emacs wgrep renaming in many files
http://stackoverflow.com/questions/37654293/replace-text-in-all-files-within-a-directory-subdirectories-in-emacs/37685982#37685982
I generally do this the way @lawlist suggests. Here it is step-by-step:
Install wgrep from Melpa or some other way. I recommend also wgrep-ag and ag if you use The Silver Searcher.
Use M-x rgrep (or M-x ag) to search the files and get a list of lines to potentially change.
In the *grep* buffer, run M-x wgrep-change-to-wgrep-mode. I bind this to C-x C-q in grep-mode-map.
Use query-replace-regexp (C-M-%), or do any other editing in the *grep* buffer.
Save the *grep* buffer with C-x C-s. Use C-c C-k to abort. The changes exist only in the *grep* buffer until you save it. You can re-enable wgrep mode any number of times.