Skip to content

Instantly share code, notes, and snippets.

@tj
tj / coros.rb
Last active August 29, 2015 13:55
def receive(co)
let status, val = coroutine.resume(co)
return val
end
def send(x)
coroutine.yield(x)
end
@marcusoftnet
marcusoftnet / url.sh
Created March 29, 2014 12:36
Adding users with CURL
curl -X POST -H "Content-Type: application/json" -d '{"username":"marcusoft.net@gmail.com","password":"xyz"}' http://localhost:3000/user -v
curl -X POST -H "Content-Type: application/json" -d '{"username":"hugo@gmail.com","password":"xyz^2"}' http://localhost:3000/user -v
curl -X POST -H "Content-Type: application/json" -d '{"username":"abbe@gmail.com","password":"zyx"}' http://localhost:3000/user -v
@marcusoftnet
marcusoftnet / koaStorePostedData.js
Created March 29, 2014 12:35
Post and get data from Mongo via Koa
// Create the application
var koa = require('koa');
var route = require('koa-route');
var parse = require('co-body');
var app = module.exports = koa();
// Get monk up an running
var monk = require('monk');
var wrap = require('co-monk');
var db = monk('localhost/koausers');
@thatrubylove
thatrubylove / lispy-ruby-example-1.rb
Created April 30, 2014 04:35
LISPy code as data in ruby
# treating a function as data
putter = ->(message) { puts message }
def deliver_message(message, handler)
handler.(message)
end
deliver_message("Hello, code as data!", putter)
# => Hello, code as data!
class Quick
def self.sort(array)
return array if array.size < 2
lesser = []
greater = []
pivot = array.pop
array.each do |x|
x <= pivot ? lesser << x : greater << x
end
self.sort(lesser) + [pivot]+ self.sort(greater)
module Watchable
attr_reader :watchers
def watchers
@watchers ||= Hash.new{|hsh,k| hsh[k]=[]}
end
def watch(&blk)
b=WatcherBuilder.new self
blk[b] if block_given?
b
@gavinheavyside
gavinheavyside / Invoking
Created May 6, 2012 11:55
strip colons from filenames in an extracted report tarball
# from the top level of an extracted report tarball
$ find . -type d -depth 1 | xargs -I {} ./filename_fixer.sh {}
@franciscolourenco
franciscolourenco / brew info gpg2
Created May 7, 2012 01:02
homebrew gpg2 install error
$ brew info gpg2
gnupg2 2.0.19
http://www.gnupg.org/
Depends on: libgpg-error, libgcrypt, libksba, libassuan, pinentry, pth, gpg-agent, dirmngr, libusb-compat
/usr/local/Cellar/gnupg2/2.0.19 (82 files, 3.2M) *
https://github.com/mxcl/homebrew/commits/master/Library/Formula/gnupg2.rb
# Very (very) naive wordt segmentation algorithm for Chinese
# (or any language with similar characteristics, works at the
# character level.)
class Partitioner
attr_reader :ngrams
# +ngrams+ Enumerable list of ngrams
def initialize(ngrams, lookahead = 6)
@lookahead = lookahead
@ngrams = {}
@Nimster
Nimster / ruby_lazy_enums.rb
Created July 4, 2012 00:52
Lazy enumerator helpers with Ruby
# Motivation:
#
# Using lazy evaluation is the way truly functional (not just 'functional style') programming languages
# achieve performance. Ruby is not there yet for several reasons, but at least some of them can be remedied
# with basic amendments to the standard library.
#
# If you want to find, for instance, the first number n<1000 such that the approximation (1+(1/n))^n to e is
# less than a certain distance, say, eps = 0.01. If you just write
(1..1000).to_enum.map { |d| ((1+(1.0/d))**(d) - (Math::E)).abs }.find_index { |x| x < 0.01 }
# It works, and returns 134. But you calculated 1000-134=876 more values than you had to. With the code below,