This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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/ |
NewerOlder