Skip to content

Instantly share code, notes, and snippets.

@mjwillson
mjwillson / scoped_streams.clj
Last active December 17, 2015 14:59
scoped streams in clojure
(ns streams.core
(:require [clojure.java.io :as io]))
(def END (Object.))
(defprotocol Stream
(with-generator [_ callback]
"Should call callback with a generator function, finally closing any
resources associated with the stream after the callback returns.
@mjwillson
mjwillson / ann.rb
Last active December 29, 2015 16:09
ann -- ultra-basic console-based multiclass text annotation tool
#!/usr/bin/env ruby
require 'optparse'
OPTIONS = {}
PARSER = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [OPTIONS] INPUT_FILE [HOTKEY OUTPUT_FILE]..."
opts.separator(<<END
#{$0} -- ultra-basic console-based multiclass text annotation tool
@mjwillson
mjwillson / ngrams_via_striding.py
Last active August 1, 2017 04:19
Matrix of sliding window ngrams without any copying via numpy striding tricks
from numpy.lib.stride_tricks import as_strided
def ngrams_via_striding(array, order):
itemsize = array.itemsize
assert array.strides == (itemsize,)
return as_strided(array, (max(array.size + 1 - order, 0), order), (itemsize, itemsize))
In [71]: a = numpy.arange(10)
In [72]: ngrams_via_striding(a, 4)
Out[72]:
@mjwillson
mjwillson / merb_restful_rangeable_collections.rb
Created March 6, 2009 17:14
RESTful way of exposing a collection resource in merb in a pageable / sub-range-fetchable way. Supports HTTP Content-Range
# Drop me a line if you wanna see this as a proper merb plugin.
class Merb::Controller
ITEM_RANGE = /^items=(\d+)-(\d+)$/
RANGE = /^(\d+)-(\d+)$/
# Displays a collection resource (using Merb's display method) while supporting requests for sub-ranges of items in a RESTful fashion.
# This supports a subset of the HTTP/1.1 spec for content ranges, using a custom range unit 'items'. eg:
# GET /collection HTTP/1.1
# Range: items 10-20