Skip to content

Instantly share code, notes, and snippets.

(defun my-replace (regex rep str &optional start)
(save-match-data
(let ((found-pos (string-match regex str (or start 0))))
(if found-pos
(let* ((match (substring str found-pos (match-end 0)))
(replacement (if (stringp rep)
rep
(or (funcall rep match) ""))))
(my-replace regex
rep
(defun my-replace (regex rep str &optional start)
"Replace matches of regex in str with the result of applying
rep function to the match.
This exists because of a quirk in replace-regexp-in-string:
because it calls the REP function in a while loop that uses
string-match and replace-match, the REP function can't use
string-match without messing up the match state in the loop.
If rep returns nil, an empty string is used as the
(defun mask-ssn (num)
(concat "XXX-XX-" (nth 2 (split-string num "-"))))
(replace-regexp-in-string "[0-9]\\{3\\}-[0-9]\\{2\\}-[0-9]\\{4\\}"
'mask-ssn
"Here's a number: 999-22-1111. Here's another: 222-11-9999.")
;; evaluates to "Here's a number: 999-22XXX-XX-11111111. Here's another: 222-11XXX-XX-99999999."
(defun mask-ssn (num)
(save-match-data
(concat "XXX-XX-" (nth 2 (split-string num "-")))))
(replace-regexp-in-string "[0-9]\\{3\\}-[0-9]\\{2\\}-[0-9]\\{4\\}"
'mask-ssn
"Here's a number: 999-22-1111. Here's another: 222-11-9999.")
;; evaluates to "Here's a number: XXX-XX-1111. Here's another: XXX-XX-9999."
;; Hooray!
(defun mask-ssn (num)
(concat "XXX-XX-" (substring num 7)))
(replace-regexp-in-string "[0-9]\\{3\\}-[0-9]\\{2\\}-[0-9]\\{4\\}"
'mask-ssn
"Here's a number: 999-22-1111. Here's another: 222-11-9999.")
;; evaluates to "Here's a number: XXX-XX-1111. Here's another: XXX-XX-9999."
@codeforkjeff
codeforkjeff / intersect.py
Created December 3, 2012 19:01
Comparison of performance of python lists and sets
# Compare the performance of python lists and sets when doing
# intersections.
#
# No real surprises here. Sets are faster than lists, and generating
# sets on-the-fly from lists is slightly slower than using sets to
# begin with.
import timeit
import random
@codeforkjeff
codeforkjeff / fizzbuzz.lisp
Created April 21, 2012 14:17
fizzbuzz in Common Lisp
;; Discovered via http://www.adampetersen.se/articles/fizzbuzz.htm
;; “Write a program that prints the numbers from 1 to 100. But for
;; multiples of three print “Fizz” instead of the number and for the
;; multiples of five print “Buzz”. For numbers which are multiples of
;; both three and five print “FizzBuzz”.”
(defun is-mult-p (n multiple)
(= (rem n multiple) 0))
(defun fizzbuzz (&optional n)
@codeforkjeff
codeforkjeff / sicp-2.5.lisp
Created April 14, 2012 15:49
Exercise 2.5 from SICP, in Common Lisp
;; Exercise 2.5 from SICP, in Common Lisp
(defun x-cons (a b)
(labels ((x-power (x n &optional acc)
(let ((acc (or acc x)))
(if (= n 1)
acc
(x-power x (- n 1) (* acc x))))))
(* (x-power 2 a) (x-power 3 b))))
@codeforkjeff
codeforkjeff / solarize.sh
Created November 27, 2011 06:43
shell script for setting gnome-terminal color palette to use Solarized theme
#!/bin/sh
#
# Shell script that configures gnome-terminal to use solarized theme
# colors. Written for Ubuntu 11.10, untested on anything else.
#
# Solarized theme: http://ethanschoonover.com/solarized
#
# Adapted from these sources:
# https://gist.github.com/1280177
# http://xorcode.com/guides/solarized-vim-eclipse-ubuntu/