Skip to content

Instantly share code, notes, and snippets.

(defn luhn-check-digit [numStr]
(let [char-to-integer (fn [c] (Integer/parseInt (str c)))
prod-tuple (fn [t] (* (t 0) (t 1)))
sum-digits (fn [n] (if (> n 9) (- n 9) n))]
(->> numStr
reverse
(map char-to-integer)
(zip (cycle [2 1]))
(map (comp sum-digits prod-tuple))
(reduce +)
@snt
snt / gist:6808138
Created October 3, 2013 11:12
REXML parsing example. Parse and format recursively under the directory specified as first arg.
require 'rexml/document'
def showxml(fn)
doc = REXML::Document.new(open(fn))
doc.elements.each("//.") { |e|
x=e
ps = []
while x != nil do
ps << x.name
x = x.parent
@snt
snt / add to .bashrc
Created August 24, 2013 07:32
Record a ssh log in Cygwin. Cygwin is slow on creating new process with `$( ... )`, so `tee` and add `$(date)` on each line would be extremely slow. run `sshlog` instead of `ssh`
function sshlog () {
if [ "x$1" = "x" ]; then
echo "simple logging wrapper for ssh."
echo "use as usual ssh"
ssh
else
TARGET=$1
mkdir -p ~/ssh-log
LOGFILE=~/ssh-log/$TARGET.$(date +%Y-%m-%d_%H.%M.%S).log
ssh $@ | python $HOME/timestamp-logger.py $LOGFILE
@snt
snt / findgap.hs
Created July 5, 2013 07:25
gaps larger than 3 seconds of the log in the format: yyyy-mm-dd HH:MM:SS.sss/JST (blah, blah, blah) something goes wrong findgap.py runs in 30 seconds for 612000 lines of log findgap.hs does in 7 minutes... what's wrong? As far as I know (by ghc -prof), regex part is its bottleneck.
import Text.Regex.TDFA
import Data.Time.Format
import Data.Time.Clock
import System.Locale
import Data.Maybe
import qualified Data.ByteString.Lazy.Char8 as B
parseTimestamp :: B.ByteString -> UTCTime
parseTimestamp s = fromJust $ parseTime defaultTimeLocale "%F %T%Q/%Z" (B.unpack s)