This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def receive(co) | |
let status, val = coroutine.resume(co) | |
return val | |
end | |
def send(x) | |
coroutine.yield(x) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from the top level of an extracted report tarball | |
$ find . -type d -depth 1 | xargs -I {} ./filename_fixer.sh {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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, |
OlderNewer