View python-to-clojure-guide.clj
;; Regular Expressions
;; import re; re.search(r'ab.*', 'cabbage').group(0) # -> 'abbage'
(re-find #"ab.*" "cabbage") ; "abbage"
;; re.match(r'ab.*', 'cabbage') # -> None
(re-matches #"ab.*" "cabbage") ; nil
;; re.match(r'ab.*', 'abracatabra').group(0) # -> 'abracatabra'
(re-matches #"ab.*" "abracatabra") ; "abracatabra"
;; Sequences & Map/Filter
View mysql-triggers.rb
#!/usr/bin/ruby
require 'rubygems'
require 'optparse'
require 'ostruct'
# >> Helper functions
# deep copy
def dcopy(o)
return Marshal.load(Marshal.dump(o))
View split.sql
DROP FUNCTION IF EXISTS SPLIT;
DROP FUNCTION IF EXISTS _SPLIT;
DROP FUNCTION IF EXISTS RTSPLIT;
DROP FUNCTION IF EXISTS LTSPLIT;
DELIMITER //
-- Simple Split (no trim)
CREATE FUNCTION SPLIT(S CHAR(255), DELIM VARCHAR(30), S_INDEX TINYINT UNSIGNED) RETURNS VARCHAR(255)
BEGIN
RETURN _SPLIT(S, DELIM, S_INDEX, '', 0);
View math_and_random.sql
-- Random integer between 2 signed integers
DROP FUNCTION IF EXISTS RANDINT;
DELIMITER //
CREATE FUNCTION RANDINT(a INT, b INT) RETURNS INT
BEGIN
RETURN FLOOR( ABS(a-b) * RAND() ) + IF(a > b, b, a);
END//
View random_string.py
from sys import maxint
from random import choice, sample, shuffle
from string import ascii_uppercase as U, ascii_lowercase as L, digits as D, punctuation as P
def populate(punctuation, extra):
chars = U + L + D
if isinstance(extra, basestring):
chars += extra
if punctuation:
chars += P
View memory_monitor.py
from resource import getrusage, RUSAGE_SELF
def monitor(unit="M", alert=None):
unit = unit.upper()
unit_transformation = {
"B" : 1./1024.,
"K" : 1.,
"M" : 1024.,
"G" : 1024.*1024.,
}
View string.js
String.prototype.sub = function(regex, replacement){
var s = this.toString();
var matches = s.match(regex);
if ( matches !== null ){
for(i in matches){
var item = matches[i];
if ( !item.escaped() ){
s = s.replace(item, replacement);
}
}
View lighter.js
/*
<div id="editor"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
.clearfix:after {
clear: both;
}
.clearfix:before, .clearfix:after {
content: " ";
display: table;
View probability-counter.py
from random import getrandbits
def probability_counter(counter=None):
if counter is None:
counter = 1
counter += int((getrandbits(counter) + 1) >> counter == 1)
return counter
View redis-transaction.py
import redis
from operator import methodcaller
class RedisTransaction(object):
def __init__(self, **kwargs):
''' Transaction timeout sets TTL for copied keys '''
if 'timeout' in kwargs:
self.TRANSACTION_TIMEOUT = kwargs['timeout']
else:
self.TRANSACTION_TIMEOUT = 10