Skip to content

Instantly share code, notes, and snippets.

@viksit
viksit / transduce.py
Created September 19, 2014 18:25
transducers in python via @ colin_
summer = lambda acc, val: acc+val
maxxer = lambda acc, val: max(acc, val)
unioner = lambda acc, val: acc.union({val})
def log(val):
print 'value:', val
return val
;; Non recursive function
(defn fib-nr [f]
(fn [n]
(if (< n 2) 1
(+ ((f f) (- n 1))
((f f) (- n 2))))))
;; A U combinator that allows a generic application function
(defn UM [myapply f]
(defn g [v]
@viksit
viksit / latency.txt
Created January 28, 2014 00:37 — forked from jboner/latency.txt
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 3,000 ns
Send 1K bytes over 1 Gbps network 10,000 ns 0.01 ms
Read 4K randomly from SSD* 150,000 ns 0.15 ms

Setting up Flume NG, listening to syslog over UDP, with an S3 Sink

My goal was to set up Flume on my web instances, and write all events into s3, so I could easily use other tools like Amazon Elastic Map Reduce, and Amazon Red Shift.

I didn't want to have to deal with log rotation myself, so I setup Flume to read from a syslog UDP source. In this case, Flume NG acts as a syslog server, so as long as Flume is running, my web application can simply write to it in syslog format on the specified port. Most languages have plugins for this.

At the time of this writing, I've been able to get Flume NG up and running on 3 ec2 instances, and all writing to the same bucket.

Install Flume NG on instances

@viksit
viksit / java_mysql_password.java
Created September 20, 2013 21:38
Java equivalent of the MySQL PASSWORD() function
/**
* http://stackoverflow.com/questions/868482/simulating-mysqls-password-encryption-using-net-or-ms-sql
* http://dev.mysql.com/doc/refman/5.0/en/encryption-functions.html#function_password
**/
public static String MySQLPassword(String plainText) throws UnsupportedEncodingException {
byte[] utf8 = plainText.getBytes("UTF-8");
return "*" + DigestUtils.shaHex(DigestUtils.sha(utf8)).toUpperCase();
}
@viksit
viksit / uuid.js
Created March 22, 2011 18:06
Javscript generate UUID/GUID
'xxyxxyxyx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
"""
Given a sequence of stock prices, what are the 2 values to buy and sell such that loss is maximized?
"""
prices = [[1,6,0,4,9,0,10],
[1,2,3,4,5,6,7,8],
[15,1,2,3,4,5,6,7,0],
[7,3,9,5,2,1,4,14,16,17],
[291, 592, 116, 480, 861, 438, 333, 541, 505, 272],
[25, 679, 1, 493, 593, 579, 943, 258, 104, 997]]
(def file (File. "/tmp/outfile.dat"))
(def dos (-> file java.io.FileOutputStream. java.io.DataOutputStream.))
(defn write-seqs [#^java.io.DataOutputStream dos]
(for [i (range 0 10)]
(.writeInt dos i)))
(write-seqs dos)
(defn level-order [f tree]
(loop [to-do [tree]]
(if (empty? to-do)
:done
(do (dorun (map (comp f :value) to-do))
(recur (mapcat (fn [{:keys [left right]}] (remove nil? [left right]))
to-do))))))
(level-order println
{:value 1
(def bt {:left {:value 3}
:value 5
:right {:left {:value 2}
:value :foo
:right {:left {:value :bar}
:value :quux
:right {:value 10}}}})
(loop [bt bt
q (clojure.lang.PersistentQueue/EMPTY)]