Skip to content

Instantly share code, notes, and snippets.

@abesto
abesto / coffescript-slotsig.coffee
Created May 28, 2011 22:05
Slots and signals for CoffeeScript
##
# Slots and signals implementation for classes
# (Porting to using simple functions should be almost trivial
##
class Connection
constructor: (@sender, @signal, @receiver, @slot) ->
connections = [] # List of connections
strict = true
@abesto
abesto / bcrypt.php
Created August 3, 2011 12:17 — forked from dzuelke/bcrypt.php
How to use bcrypt in PHP to safely store passwords (PHP 5.3+ only)
<?php
// secure hashing of passwords using bcrypt, needs PHP 5.3+
// see http://codahale.com/how-to-safely-store-a-password/
// salt for bcrypt needs to be 22 base64 characters (but just [./0-9A-Za-z]), see http://php.net/crypt
// 2a is the bcrypt algorithm selector, see http://php.net/crypt
// 12 is the workload factor (around 300ms on a Core i7 machine), see http://php.net/crypt
function bcrypt($message, $salt, $cost=12)
{
if (preg_match('~[./0-9A-Za-z]{22}~', $salt) === 0) throw new RuntimeException('bcrypt expects a salt of 22 digits of the alphabet [./0-9A-Za-z]');
@abesto
abesto / simple_redis_profile.rb
Created March 1, 2012 13:53 — forked from gtd/simple_redis_profile.rb
By sampling keys from your redis databases, this script tries to identify what types of keys are occupying the most memory.
#!/usr/bin/env ruby
# Evaluates a sample of keys/values from each redis database, computing statistics for each key pattern:
# keys: number of keys matching the given pattern
# size: approximation of the associated memory occupied (based on size/length of value)
# percent: the proportion of this 'size' relative to the sample's total
#
# Copyright Weplay, Inc. 2010. Available for use under the MIT license.
#
# Changes in this fork (abesto) by Zoltán Nagy <abesto@abesto.net>
@abesto
abesto / lzw.hs
Created June 29, 2012 08:13
Haskell LZW compression
-- Could use a few rounds of cleanup...
import Data.Char
import Data.List
dictionary :: [String]
dictionary = [[chr c] | c <- [0 .. 127]]
prefixes :: [String] -> String -> [(Int, String)]
prefixes xs y = [(i, xs !! i) | i <- [0 .. (length xs) -1], xs !! i `isPrefixOf` y]
@abesto
abesto / gist:3476594
Created August 26, 2012 09:27
Go: Newton's method for square root
/*
A Tour of Go: page 44
http://tour.golang.org/#44
Exercise: Loops and Functions
As a simple way to play with functions and loops, implement the square root function using Newton's method.
In this case, Newton's method is to approximate Sqrt(x) by picking a starting point z and then repeating: z - (z*z - x) / (2 * z)
@abesto
abesto / curry.bash
Last active February 28, 2024 18:32
Partial application in Bash
function curry() {
exportfun=$1; shift
fun=$1; shift
params=$*
cmd=$"function $exportfun() {
more_params=\$*;
$fun $params \$more_params;
}"
eval $cmd
}
app.post '/tab', (req, res) ->
redis.incr 'tab', (err, id) ->
return res.send 500, err if err
key = tabKey(id)
json = JSON.stringify {id: id, text: req.body.text}
redis.set key, json, (err) ->
return res.send 500, err if err
redis.rpush tabsListKey, id, (err) ->
return res.send 500, err if err
res.send 201, json
log4j.appender.Console.layout.conversionPattern = %d %h %c %p %m using a config from the web%n
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Gy2_1 {
static class Point {
private Integer x, y;
Point(Integer x, Integer y) {
// in app.js:
ss.api.log = winston.info;
console.log = ss.api.log;