Skip to content

Instantly share code, notes, and snippets.

View christian-marie's full-sized avatar

Christian Marie christian-marie

View GitHub Profile
@christian-marie
christian-marie / progress.rb
Last active December 19, 2015 09:09
Very simple progress bar for ruby.
def progress p
t=p;p=[p.to_i,100].min;p>>=1
print("[#{'*'*p}#{' '*(50-p)}] #{t.round}%\r")
STDOUT.flush
end
1.upto(100){|i| progress(i); sleep 0.1}
@christian-marie
christian-marie / random_string.rb
Created July 17, 2013 01:48
Simple random string generating lambda in Ruby with a small chance that the length of your string will be one less than you expect.
random_string = -> length, radix=36 { rand(radix ** length).to_s(radix) }
# Random string of length 42, maybe 41.
random_string.call(42)
# Four random digits
random_string.call(4, 10)
# Four random hex digits
random_string.call(4, 16)
@christian-marie
christian-marie / quicksort.rb
Created July 17, 2013 01:51
The most best ever quicksort, obviously Ruby. Super fast.
s=->l{(p=l.pop)?s[l.select{|i|i<=p}]+[p]+s[l.select{|i|i>p}]:[]}
puts s["the quick brown fox jumps over the lazy dog".chars.to_a].join
set textwidth=79
set formatoptions=cqro
set autoindent
if has('gui_running')
set guifont=Terminus
endif
filetype indent plugin on
syntax on
call pathogen#infect()
@christian-marie
christian-marie / shuffle.hs
Created December 2, 2013 22:44
Shuffle a list by selection of a random permutation. Not particularly efficient but easy and readable.
import Data.List
import System.Random
shuffle :: [a] -> IO [a]
shuffle ss = do
i <- getStdRandom $ randomR (0, length ps)
return $ ps !! i
where ps = permutations ss
@christian-marie
christian-marie / c.vim
Last active December 31, 2015 16:49
.vim/plugin/c.vim
fu Select_c_style()
set tabstop=8 "A tab is 8 spaces
set expandtab "Always uses spaces instead of tabs
set softtabstop=8 "Insert 4 spaces when tab is pressed
set shiftwidth=8 "An indent is 4 spaces
set smarttab "Indent instead of tab at start of line
set shiftround "Round spaces to nearest shiftwidth multiple
set foldmethod=syntax
" set nojoinspaces "Don't convert spaces to tabs
endf
#include <zmq.h>
#include <stdlib.h>
#include "assert.h"
int main() {
void *c = zmq_ctx_new();
assert(c);
void *s = zmq_socket(c, ZMQ_PUSH);
assert(s);
@christian-marie
christian-marie / client.c
Created December 20, 2013 04:11
gcc -o poc poc.c -lzmq $ while ./poc; do :; done Assertion failed: get_load () == 0 (poller_base.cpp:31)
#include <zmq.h>
#include <stdlib.h>
#include "assert.h"
int main() {
void *c = zmq_ctx_new();
assert(c);
void *s = zmq_socket(c, ZMQ_PUSH);
assert(s);
import time
import zmq
def result_collector():
context = zmq.Context()
results_receiver = context.socket(zmq.PULL)
results_receiver.bind("tcp://127.0.0.1:1234")
while True:
print results_receiver.recv()
static void *main_thread() {
typedef struct {
void *socket;
timeval timeout;
current_msg;
}
socket sockets[10];
for( 0..10) {